Module: BLS_API::Destringify

Included in:
Client, Series
Defined in:
lib/bls_api/destringify.rb

Instance Method Summary collapse

Instance Method Details

#destringify(raw_response, use_floats = false) ⇒ Object

Public: Convert numeric data in a BLS API response from Strings to instances of more useful Numeric subclasses for the user’s convenience.

(My guess is BLS sends data as strings in order to maintain precision and simplify the JSON conversion on their end.)

raw_response - A Hash of parsed JSON data from an API response, such as

that returned by BLS_API::RawRequest#make_api_request.

use_floats - An optional Boolean specifying whether to express values

as Floats (for simplicity) instead of as BigDecimals
(for precision) (default: false).

Returns a Hash similar to raw_response but with some keys and values

converted to Numerics.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bls_api/destringify.rb', line 19

def destringify(raw_response, use_floats = false)
  output = {}

  raw_response.keys.reject { |key| key == "Results" }.each do |key|
    output[key] = raw_response[key]
  end

  output_results = {}
  output["Results"] = output_results
  raw_results = raw_response["Results"]
  raw_results.keys.reject { |key| key == "series" }.each do |key|
    output_results[key] = raw_results[key]
  end

  output_results["series"] = raw_results["series"].map do |raw_series|
    self.destringify_series(raw_series, use_floats)
  end

  output
end

#destringify_calculations(raw_calcs, use_floats = false) ⇒ Object

Internal: Convert the keys and values in a calculations object (change in an indicator over the past 1,3,6,12 months) from Strings to Numerics.

raw_calcs - A Hash with String keys (specifying the number of months

over which a given change was calculated) and String values
(specifying the change over that period of time).

use_floats - An optional Boolean specifying whether to express values

as Floats (for simplicity) instead of as BigDecimals
(for precision) (default: false).

Returns a Hash with Integer keys and BigDecimal values (to preserve

precision).


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bls_api/destringify.rb', line 52

def destringify_calculations(raw_calcs, use_floats = false)
  Hash[raw_calcs.each_pair.map do |key, value|
    [
      key.to_i,

      case use_floats
      when true then value.to_f
      else BigDecimal.new(value)
      end
    ]
  end]
end

#destringify_month(raw_month, use_floats = false) ⇒ Object

Internal: Convert all quantitative keys and values in a month object (statistics and optional changes corresponding to a particular series/month combination) from Strings to Numerics.

raw_month - A Hash for a given month’s data point. Should contain

"year", "period" and "value" properties, at least.

use_floats - An optional Boolean specifying whether to express values

as Floats (for simplicity) instead of as BigDecimals
(for precision) (default: false).

Returns a Hash with all of the same keys as ‘raw_month`.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bls_api/destringify.rb', line 76

def destringify_month(raw_month, use_floats = false)
  output = {}

  output["year"] = raw_month["year"].to_i
  output["value"] = case use_floats
                    when true then raw_month["value"].to_f
                    else BigDecimal.new(raw_month["value"])
                    end

  if raw_month.include?("calculations")
    output["calculations"] = Hash[
      raw_month["calculations"].each_pair.map do |name, calcs|
        [name, self.destringify_calculations(calcs, use_floats)]
      end
    ]
  end

  keys_to_pass_through = raw_month.keys.reject do |key|
    ["year", "value", "calculations"].include?(key)
  end
  keys_to_pass_through.each { |key| output[key] = raw_month[key] }

  output
end

#destringify_series(raw_series, use_floats = false) ⇒ Object

Internal: Convert numeric data in a BLS API series from Strings to Numerics.

raw_series - An Array of month Hashes from a BLS API response. use_floats - An optional Boolean specifying whether to express values

as Floats (for simplicity) instead of as BigDecimals
(for precision) (default: false).

Returns an Array of month Hashes after conversion by #destringify_month.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bls_api/destringify.rb', line 110

def destringify_series(raw_series, use_floats = false)
  output = {}

  raw_series.keys.reject { |key| key == "data" }.each do |key|
    output[key] = raw_series[key]
  end

  output["data"] = raw_series["data"].map do |raw_month|
    self.destringify_month(raw_month, use_floats)
  end

  output
end