commit 02c21e303eaf77bec1191dae3e6bd545db676441 Author: imi415 Date: Wed May 5 20:20:31 2021 +0800 Initial commit, with real-time condition. Signed-off-by: imi415 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ed2def2 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec + +gem "faraday", "~> 1.4" + +gem "faraday_middleware", "~> 1.0" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..007eab1 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..1ba2e9f --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/lib/moji_weather.rb b/lib/moji_weather.rb new file mode 100644 index 0000000..93a1759 --- /dev/null +++ b/lib/moji_weather.rb @@ -0,0 +1,11 @@ +require 'json' +require 'uri' + +require 'faraday' +require 'faraday_middleware' + +require 'moji_weather/api' +require 'moji_weather/error' + +module MojiWeather +end \ No newline at end of file diff --git a/lib/moji_weather/api.rb b/lib/moji_weather/api.rb new file mode 100644 index 0000000..5f4ff5c --- /dev/null +++ b/lib/moji_weather/api.rb @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/api/alert.rb b/lib/moji_weather/api/alert.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/aqi.rb b/lib/moji_weather/api/aqi.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/aqi_forecast_5days.rb b/lib/moji_weather/api/aqi_forecast_5days.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/condition.rb b/lib/moji_weather/api/condition.rb new file mode 100644 index 0000000..0879e55 --- /dev/null +++ b/lib/moji_weather/api/condition.rb @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/api/forecast_15days.rb b/lib/moji_weather/api/forecast_15days.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/forecast_24hrs.rb b/lib/moji_weather/api/forecast_24hrs.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/limit.rb b/lib/moji_weather/api/limit.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/live_index.rb b/lib/moji_weather/api/live_index.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/moji_weather/api/rest_client.rb b/lib/moji_weather/api/rest_client.rb new file mode 100644 index 0000000..cf082b5 --- /dev/null +++ b/lib/moji_weather/api/rest_client.rb @@ -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 \ No newline at end of file diff --git a/lib/moji_weather/error.rb b/lib/moji_weather/error.rb new file mode 100644 index 0000000..1b70e2c --- /dev/null +++ b/lib/moji_weather/error.rb @@ -0,0 +1,7 @@ +module MojiWeather + # Exception bash class. + class Error < StandardError; end + + class ClientError < Error; end + class ServerError < Error; end +end \ No newline at end of file diff --git a/moji_weather.gemspec b/moji_weather.gemspec new file mode 100644 index 0000000..7cab8f0 --- /dev/null +++ b/moji_weather.gemspec @@ -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 \ No newline at end of file