Class: JMAFeed::Api
- Inherits:
-
Object
- Object
- JMAFeed::Api
- Defined in:
- lib/jma_feed/api.rb
Instance Method Summary collapse
- #build_request(verb, uri, query: nil, body: nil, headers: {}) ⇒ Object
- #execute(verb, uri, query: nil, body: nil, headers: {}) ⇒ Object
- #get_feed_extra(if_modified_since: nil, &block) ⇒ Object
- #get_feed_regular(if_modified_since: nil, &block) ⇒ Object
-
#initialize ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize ⇒ Api
Returns a new instance of Api.
4 5 |
# File 'lib/jma_feed/api.rb', line 4 def initialize end |
Instance Method Details
#build_request(verb, uri, query: nil, body: nil, headers: {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jma_feed/api.rb', line 36 def build_request(verb, uri, query: nil, body: nil, headers: {}) if query uri.query = URI.encode_www_form(query) end request = "Net::HTTP::#{verb.to_s.camelize}".constantize.new(uri) if body request.body = body end request end |
#execute(verb, uri, query: nil, body: nil, headers: {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jma_feed/api.rb', line 25 def execute(verb, uri, query: nil, body: nil, headers: {}) uri = uri.is_a?(URI::HTTP) ? uri : URI.parse(uri) request = build_request(verb, uri, query: query, body: body) headers.each {|k,v| request[k] = v} res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| yield(request, http) if block_given? # http.read_timeout = 30 # sec http.request(request) end end |
#get_feed_extra(if_modified_since: nil, &block) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/jma_feed/api.rb', line 16 def get_feed_extra(if_modified_since: nil, &block) res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/extra.xml", headers: { "If-Modified-Since" => if_modified_since ? if_modified_since.httpdate : nil }, &block) JMAFeed::Result.new(res) end |
#get_feed_regular(if_modified_since: nil, &block) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/jma_feed/api.rb', line 7 def get_feed_regular(if_modified_since: nil, &block) res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/regular.xml", headers: { "If-Modified-Since" => if_modified_since ? if_modified_since.httpdate : nil }, &block) JMAFeed::Result.new(res) end |