Class: BLS_API::Client
- Inherits:
-
Object
- Object
- BLS_API::Client
- Includes:
- Destringify, RawRequest
- Defined in:
- lib/bls_api/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#request_annual_averages ⇒ Object
Returns the value of attribute request_annual_averages.
-
#request_calculations ⇒ Object
Returns the value of attribute request_calculations.
-
#request_catalog ⇒ Object
Returns the value of attribute request_catalog.
-
#use_floats ⇒ Object
Returns the value of attribute use_floats.
Instance Method Summary collapse
-
#get(options = {}) ⇒ Object
Public: Request a batch of data from the BLS API.
-
#initialize(api_key = nil) ⇒ Client
constructor
A new instance of Client.
Methods included from RawRequest
Methods included from Destringify
#destringify, #destringify_calculations, #destringify_month, #destringify_series
Constructor Details
#initialize(api_key = nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bls_api/client.rb', line 17 def initialize(api_key = nil) @api_key = ENV.fetch("BLS_API_KEY", nil) @api_key = api_key unless api_key.nil? @api_key = nil if @api_key.is_a?(String) && @api_key.empty? if @api_key.nil? = " You must provide an API key as an argument to BLS_API::Client.new or\n as the BLS_API_KEY environment variable. If you do not have an API\n key, register for one at http://data.bls.gov/registrationEngine/.\n EOF\n raise BLS_API::Errors::ConfigurationError, missing_key_message\n end\n\n @request_annual_averages = true\n @request_catalog = true\n @request_calculations = true\n @use_floats = false\nend\n".gsub(/^ */, "").gsub(/\r?\n/, " ").strip |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/bls_api/client.rb', line 11 def api_key @api_key end |
#request_annual_averages ⇒ Object
Returns the value of attribute request_annual_averages.
12 13 14 |
# File 'lib/bls_api/client.rb', line 12 def request_annual_averages @request_annual_averages end |
#request_calculations ⇒ Object
Returns the value of attribute request_calculations.
14 15 16 |
# File 'lib/bls_api/client.rb', line 14 def request_calculations @request_calculations end |
#request_catalog ⇒ Object
Returns the value of attribute request_catalog.
13 14 15 |
# File 'lib/bls_api/client.rb', line 13 def request_catalog @request_catalog end |
#use_floats ⇒ Object
Returns the value of attribute use_floats.
15 16 17 |
# File 'lib/bls_api/client.rb', line 15 def use_floats @use_floats end |
Instance Method Details
#get(options = {}) ⇒ Object
Public: Request a batch of data from the BLS API.
By default, raises BLS_API::Errors::APIError if the request is unsuccessful. (You can catch this with an IOError, if that’s more your thing.)
options - A Hash with three required arguments and four optional
arguments.
Required arguments include:
:series_ids - An Array of String series IDs for which to
request data. If a String is provided instead, it
is assumed to be a single series ID.
:start_year - An Integer representing the earliest year for
which to request data.
:end_year - An Integer representing the latest year for which
to request data. Note that the BLS API will
return an error if you specify a year for which
no data exists; for example, an :end_year of 2016
will raise an error during January of 2016 when
no 2016 data has yet been released.
Optional arguments include:
:catch_errors - A Boolean whether to raise an
APIError if the request is unsuccessful
(default: true).
:catalog - A Boolean whether to include
catalog data in the response
(default: true).
:calculations - A Boolean whether to include
net-change and percent-change calculations
in the response (default: true).
:annual_averages - A Boolean whether to include
annual averages in the response
(default: true).
Returns a Hash with the given String series IDs as keys and
BLS_API::Series instances as values.
72 73 74 75 76 77 78 |
# File 'lib/bls_api/client.rb', line 72 def get( = {}) raw_response = self.make_api_request() destringified = self.destringify(raw_response, @use_floats) series = Hash[destringified["Results"]["series"].map do |raw_series| [raw_series["seriesID"], BLS_API::Series.new(raw_series)] end] end |