Refactored API.
This commit is contained in:
parent
d4b3af2379
commit
93ebdfcb98
13
README.md
13
README.md
|
@ -2,10 +2,13 @@
|
|||
|
||||
## 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
|
||||
client = MojiWeather::Api::Client.new app_code: "YOUR_APP_CODE"
|
||||
|
||||
result = client.query MojiWeather::Api::ApiType::AQI, location: {lat: 39.95, lon: 116.36}
|
||||
result = client.query MojiWeather::Api::ApiType::AQI, city_id: 2
|
||||
```
|
||||
`ApiType` has the following possible values (more is WIP):
|
||||
* `MojiWeather::Api::ApiType::CONDITION`: Real-time condition
|
||||
* `MojiWeather::Api::ApiType::FORECAST_24HRS`: 24-hour forecast
|
||||
* `MojiWeather::Api::ApiType::AQI`: Air quality index(AQI)
|
|
@ -1,13 +1,8 @@
|
|||
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'
|
||||
require 'moji_weather/api/rest_client'
|
||||
|
||||
module MojiWeather
|
||||
module Api
|
||||
class Client < MojiWeather::Api::RestClient
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,19 +0,0 @@
|
|||
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,26 @@
|
|||
module MojiWeather
|
||||
module Api
|
||||
module ApiType
|
||||
CONDITION = 'condition'
|
||||
FORECAST_24HRS = 'forecast24hours'
|
||||
AQI = 'aqi'
|
||||
end
|
||||
|
||||
module Constants
|
||||
API_TOKENS = {
|
||||
MojiWeather::Api::ApiType::CONDITION => {
|
||||
city_id: '50b53ff8dd7d9fa320d3d3ca32cf8ed1',
|
||||
location: 'ff826c205f8f4a59701e64e9e64e01c4'
|
||||
},
|
||||
MojiWeather::Api::ApiType::FORECAST_24HRS => {
|
||||
city_id: '008d2ad9197090c5dddc76f583616606',
|
||||
location: '1b89050d9f64191d494c806f78e8ea36'
|
||||
},
|
||||
MojiWeather::Api::ApiType::AQI => {
|
||||
city_id: '8b36edf8e3444047812be3a59d27bab9',
|
||||
location: '6e9a127c311094245fc1b2aa6d0a54fd'
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,3 +1,5 @@
|
|||
require 'moji_weather/api/constants'
|
||||
|
||||
module MojiWeather
|
||||
module Api
|
||||
class RestClient
|
||||
|
@ -11,6 +13,18 @@ module MojiWeather
|
|||
@location_base = "http://aliv8.data.moji.com/whapi/json/aliweather"
|
||||
end
|
||||
|
||||
def query(api_type, options = {})
|
||||
if !options[:city_id].nil? then
|
||||
response = cityid_request(api_type, city_id: options[:city_id], token: MojiWeather::Api::Constants::API_TOKENS[api_type][:city_id])
|
||||
elsif !options[:location].nil? then
|
||||
response = location_request(api_type, location: options[:location], token: MojiWeather::Api::Constants::API_TOKENS[api_type][:location])
|
||||
else
|
||||
raise MojiWeather::ClientError.new("Invalid request: either city_id or location needs to be set.")
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
def cityid_request(api_uri, options = {})
|
||||
conn = Faraday.new do |f|
|
||||
|
|
Loading…
Reference in New Issue