diff --git a/README.md b/README.md index 1ba2e9f..887ed2e 100644 --- a/README.md +++ b/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) \ No newline at end of file diff --git a/lib/moji_weather/api.rb b/lib/moji_weather/api.rb index 5f4ff5c..8eb4348 100644 --- a/lib/moji_weather/api.rb +++ b/lib/moji_weather/api.rb @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/api/alert.rb b/lib/moji_weather/api/alert.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/aqi.rb b/lib/moji_weather/api/aqi.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/aqi_forecast_5days.rb b/lib/moji_weather/api/aqi_forecast_5days.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/condition.rb b/lib/moji_weather/api/condition.rb deleted file mode 100644 index 0879e55..0000000 --- a/lib/moji_weather/api/condition.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/api/constants.rb b/lib/moji_weather/api/constants.rb new file mode 100644 index 0000000..35fd9a6 --- /dev/null +++ b/lib/moji_weather/api/constants.rb @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/api/forecast_15days.rb b/lib/moji_weather/api/forecast_15days.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/forecast_24hrs.rb b/lib/moji_weather/api/forecast_24hrs.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/limit.rb b/lib/moji_weather/api/limit.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/live_index.rb b/lib/moji_weather/api/live_index.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/moji_weather/api/rest_client.rb b/lib/moji_weather/api/rest_client.rb index cf082b5..1d863a3 100644 --- a/lib/moji_weather/api/rest_client.rb +++ b/lib/moji_weather/api/rest_client.rb @@ -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|