Initial commit, with real-time condition.
Signed-off-by: imi415 <imi415.public@gmail.com>
This commit is contained in:
commit
02c21e303e
|
@ -0,0 +1,7 @@
|
||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
gemspec
|
||||||
|
|
||||||
|
gem "faraday", "~> 1.4"
|
||||||
|
|
||||||
|
gem "faraday_middleware", "~> 1.0"
|
|
@ -0,0 +1,33 @@
|
||||||
|
PATH
|
||||||
|
remote: .
|
||||||
|
specs:
|
||||||
|
moji_weather (0.0.1)
|
||||||
|
faraday (~> 1.4)
|
||||||
|
|
||||||
|
GEM
|
||||||
|
remote: https://rubygems.org/
|
||||||
|
specs:
|
||||||
|
faraday (1.4.1)
|
||||||
|
faraday-excon (~> 1.1)
|
||||||
|
faraday-net_http (~> 1.0)
|
||||||
|
faraday-net_http_persistent (~> 1.1)
|
||||||
|
multipart-post (>= 1.2, < 3)
|
||||||
|
ruby2_keywords (>= 0.0.4)
|
||||||
|
faraday-excon (1.1.0)
|
||||||
|
faraday-net_http (1.0.1)
|
||||||
|
faraday-net_http_persistent (1.1.0)
|
||||||
|
faraday_middleware (1.0.0)
|
||||||
|
faraday (~> 1.0)
|
||||||
|
multipart-post (2.1.1)
|
||||||
|
ruby2_keywords (0.0.4)
|
||||||
|
|
||||||
|
PLATFORMS
|
||||||
|
x86_64-linux
|
||||||
|
|
||||||
|
DEPENDENCIES
|
||||||
|
faraday (~> 1.4)
|
||||||
|
faraday_middleware
|
||||||
|
moji_weather!
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
2.2.16
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Moji Weather API (for Aliyun Cloud Market)
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
### Condition:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
condition = MojiWeather::Api::Condition.new app_code: "YOUR_APP_CODE"
|
||||||
|
pp condition.query location: {lat: 39.95, lon: 116.36}
|
||||||
|
pp condition.query city_id: 2
|
||||||
|
```
|
|
@ -0,0 +1,11 @@
|
||||||
|
require 'json'
|
||||||
|
require 'uri'
|
||||||
|
|
||||||
|
require 'faraday'
|
||||||
|
require 'faraday_middleware'
|
||||||
|
|
||||||
|
require 'moji_weather/api'
|
||||||
|
require 'moji_weather/error'
|
||||||
|
|
||||||
|
module MojiWeather
|
||||||
|
end
|
|
@ -0,0 +1,13 @@
|
||||||
|
require 'moji_weather/api/alert'
|
||||||
|
require 'moji_weather/api/condition'
|
||||||
|
require 'moji_weather/api/aqi_forecast_5days'
|
||||||
|
require 'moji_weather/api/aqi'
|
||||||
|
require 'moji_weather/api/forecast_15days'
|
||||||
|
require 'moji_weather/api/forecast_24hrs'
|
||||||
|
require 'moji_weather/api/limit'
|
||||||
|
require 'moji_weather/api/live_index'
|
||||||
|
|
||||||
|
module MojiWeather
|
||||||
|
module Api
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,19 @@
|
||||||
|
require 'moji_weather/api/rest_client'
|
||||||
|
|
||||||
|
module MojiWeather
|
||||||
|
module Api
|
||||||
|
class Condition < MojiWeather::Api::RestClient
|
||||||
|
def query(options = {})
|
||||||
|
if !options[:city_id].nil? then
|
||||||
|
response = cityid_request('condition', city_id: options[:city_id], token: '50b53ff8dd7d9fa320d3d3ca32cf8ed1')
|
||||||
|
elsif !options[:location].nil? then
|
||||||
|
response = location_request('condition', location: options[:location], token: 'ff826c205f8f4a59701e64e9e64e01c4')
|
||||||
|
else
|
||||||
|
raise MojiWeather::ClientError.new("Invalid request: either city_id or location needs to be set.")
|
||||||
|
end
|
||||||
|
|
||||||
|
response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,58 @@
|
||||||
|
module MojiWeather
|
||||||
|
module Api
|
||||||
|
class RestClient
|
||||||
|
def initialize(options = {})
|
||||||
|
if options[:app_code].nil?
|
||||||
|
raise MojiWeather::ClientError.new("AppCode not provided.")
|
||||||
|
end
|
||||||
|
@app_code = options[:app_code]
|
||||||
|
|
||||||
|
@cityid_base = "http://aliv18.data.moji.com/whapi/json/alicityweather"
|
||||||
|
@location_base = "http://aliv8.data.moji.com/whapi/json/aliweather"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def cityid_request(api_uri, options = {})
|
||||||
|
conn = Faraday.new do |f|
|
||||||
|
f.request :url_encoded
|
||||||
|
f.response :json
|
||||||
|
f.response :raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
response = conn.post("#{@cityid_base}/#{api_uri}") do |req|
|
||||||
|
req.body = {"cityId" => options[:city_id], "token" => options[:token]}
|
||||||
|
req.headers["Authorization"] = "APPCODE #{@app_code}"
|
||||||
|
end
|
||||||
|
rescue Faraday::ClientError => e
|
||||||
|
raise MojiWeather::ClientError.new("API client error, code: #{response.status}, msg: #{response.body}")
|
||||||
|
rescue Faraday::ServerError => e
|
||||||
|
raise MojiWeather::ServerError.new("API server error, code: #{response.status}, msg: #{response.body}")
|
||||||
|
end
|
||||||
|
|
||||||
|
response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def location_request(api_uri, options = {})
|
||||||
|
conn = Faraday.new do |f|
|
||||||
|
f.request :url_encoded
|
||||||
|
f.response :json
|
||||||
|
f.response :raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
response = conn.post("#{@location_base}/#{api_uri}") do |req|
|
||||||
|
req.body = {"lat" => options[:location][:lat], "lon" => options[:location][:lon], "token" => options[:token]}
|
||||||
|
req.headers["Authorization"] = "APPCODE #{@app_code}"
|
||||||
|
end
|
||||||
|
rescue Faraday::ClientError => e
|
||||||
|
raise MojiWeather::ClientError.new("API client error, code: #{response.status}, msg: #{response.body}")
|
||||||
|
rescue Faraday::ServerError => e
|
||||||
|
raise MojiWeather::ServerError.new("API server error, code: #{response.status}, msg: #{response.body}")
|
||||||
|
end
|
||||||
|
|
||||||
|
response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
module MojiWeather
|
||||||
|
# Exception bash class.
|
||||||
|
class Error < StandardError; end
|
||||||
|
|
||||||
|
class ClientError < Error; end
|
||||||
|
class ServerError < Error; end
|
||||||
|
end
|
|
@ -0,0 +1,13 @@
|
||||||
|
Gem::Specification.new do |s|
|
||||||
|
s.name = 'moji_weather'
|
||||||
|
s.version = '0.0.1'
|
||||||
|
s.summary = "Moji Weather"
|
||||||
|
s.description = "Moji Weather API provided by Aliyun"
|
||||||
|
s.authors = ["imi415"]
|
||||||
|
s.email = "imi415.public@gmail.com"
|
||||||
|
s.files = ["lib/moji_weather.rb"]
|
||||||
|
s.license = "MIT"
|
||||||
|
|
||||||
|
s.add_dependency "faraday", "~> 1.4"
|
||||||
|
s.add_dependency "faraday_middleware", "~> 1.0"
|
||||||
|
end
|
Loading…
Reference in New Issue