class BeaApi::Request
> BeaApi::Request
¶ ↑
Constants
- BEA_URL
- InvalidKey
Attributes
notes[RW]
response[RW]
Public Class Methods
find(options = {})
click to toggle source
# File lib/bea_api/request.rb, line 12 def self.find(options = {}) uri = "#{BEA_URL}?#{self.to_params(options)}" response = Request.new(uri) end
new(uri)
click to toggle source
# File lib/bea_api/request.rb, line 17 def initialize(uri) @response = RestClient.get(uri.to_s) _parse_response end
Protected Class Methods
to_params(options)
click to toggle source
# File lib/bea_api/request.rb, line 33 def self.to_params(options) options.map { |k,v| "#{k}=#{v}" }.join("&") end
Protected Instance Methods
_parse_data(response)
click to toggle source
# File lib/bea_api/request.rb, line 56 def _parse_data(response) h = response["BEAAPI"]["Results"]["Data"] h ||= response["BEAAPI"]["Data"] h ||= response["BEAAPI"]["Results"].first[1] end
_parse_notes(response)
click to toggle source
# File lib/bea_api/request.rb, line 62 def _parse_notes(response) if (!response["BEAAPI"]["Results"]["Notes"].nil?) notes = response["BEAAPI"]["Results"]["Notes"] elsif (!response["BEAAPI"]["Notes"].nil?) notes = response["BEAAPI"]["Notes"] end notes end
_parse_response()
click to toggle source
# File lib/bea_api/request.rb, line 24 def _parse_response case @response.code when 200 _response_html_success(@response) else _response_html_error(@response) end end
_response_error(r)
click to toggle source
# File lib/bea_api/request.rb, line 71 def _response_error(r) if (!r["BEAAPI"]["Error"].nil?) err = r["BEAAPI"]["Error"] else err = r["BEAAPI"]["Results"].first.last end if (err["APIErrorCode"].to_i == InvalidKey) fail InvalidKeyError, "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at http://www.bea.gov/api/" end fail ParameterError, err["APIErrorDescription"] end
_response_html_error(response)
click to toggle source
# File lib/bea_api/request.rb, line 83 def _response_html_error(response) fail StandardError, response.code + "\n" + response.body end
_response_html_success(response)
click to toggle source
# File lib/bea_api/request.rb, line 37 def _response_html_success(response) r = JSON.parse(response) if (!r["BEAAPI"]["Error"].nil? || r["BEAAPI"]["Results"].first.first.to_s.downcase == 'error') @response = _response_error(r) else @response = _response_success(r) end end
_response_success(response)
click to toggle source
# File lib/bea_api/request.rb, line 46 def _response_success(response) h = [] if (response.length > 0) h = _parse_data(response) h = [h] unless (h.kind_of?(Array)) @notes = _parse_notes(response) end h end