Class: BLS_API::Month
- Inherits:
-
Object
- Object
- BLS_API::Month
- Defined in:
- lib/bls_api/month.rb
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #footnotes ⇒ Object
-
#initialize(raw_month) ⇒ Month
constructor
A new instance of Month.
- #month ⇒ Object
- #net_change(timeframe) ⇒ Object
- #percent_change(timeframe) ⇒ Object
- #value ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(raw_month) ⇒ Month
Returns a new instance of Month.
5 6 7 |
# File 'lib/bls_api/month.rb', line 5 def initialize(raw_month) @raw_month = raw_month end |
Instance Method Details
#<=>(other) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/bls_api/month.rb', line 56 def <=>(other) year_comparison = self.year <=> other.year return year_comparison unless year_comparison == 0 month_comparison = self.month <=> other.month end |
#footnotes ⇒ Object
21 22 23 24 25 26 |
# File 'lib/bls_api/month.rb', line 21 def footnotes non_empty_footnotes = @raw_month["footnotes"].reject { |x| x.empty? } Hash[non_empty_footnotes.map do |footnote| [footnote["code"], footnote["text"]] end] end |
#month ⇒ Object
13 14 15 |
# File 'lib/bls_api/month.rb', line 13 def month @raw_month["period"].slice(/^M(\d+)$/, 1).to_i end |
#net_change(timeframe) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bls_api/month.rb', line 28 def net_change(timeframe) unless @raw_month.include?("calculations") raise BLS_API::NotRetrievedError, "Calculations not retrieved" end net_changes = @raw_month["calculations"].fetch("net_changes") do raise BLS_API::NotRetrievedError, ( "Net-change calculations not available from BLS") end net_changes.fetch(timeframe) do raise BLS_API::NotRetrievedError, ( "#{timeframe}-month net changes not available from BLS") end end |
#percent_change(timeframe) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bls_api/month.rb', line 42 def percent_change(timeframe) unless @raw_month.include?("calculations") raise BLS_API::NotRetrievedError, "Calculations not retrieved" end percent_changes = @raw_month["calculations"].fetch("pct_changes") do raise BLS_API::NotRetrievedError, ( "Percent-change calculations not available from BLS") end percent_changes.fetch(timeframe) do raise BLS_API::NotRetrievedError, ( "#{timeframe}-month percent changes not available from BLS") end end |
#value ⇒ Object
17 18 19 |
# File 'lib/bls_api/month.rb', line 17 def value @raw_month["value"] end |
#year ⇒ Object
9 10 11 |
# File 'lib/bls_api/month.rb', line 9 def year @raw_month["year"] end |