Class: SolarTerms24::SolarTerms

Inherits:
Object
  • Object
show all
Defined in:
lib/solar_terms_24/solar_terms.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, timezone: 'UTC', lang: :en) ⇒ SolarTerms

Returns a new instance of SolarTerms.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/solar_terms_24/solar_terms.rb', line 11

def initialize(year, timezone: 'UTC', lang: :en)
  @year = year
  @timezone = timezone
  @lang = lang

  if SolarTerms24::Cache.has?(year)
    data = SolarTerms24::Cache.load(year)
  else
    data = SolarTerms24::Horizons.find_solar_terms_times(year)
    SolarTerms24::Cache.save(year, data)
  end

  @solar_terms = data.each_key.each_with_object({}) do |key, h|
    h[key.to_sym] = SolarTerms24::SolarTerm.new(key.to_sym, data[key], timezone: timezone, lang: lang)
  end
end

Instance Attribute Details

#langObject

Returns the value of attribute lang.



8
9
10
# File 'lib/solar_terms_24/solar_terms.rb', line 8

def lang
  @lang
end

#solar_termsObject (readonly)

Returns the value of attribute solar_terms.



9
10
11
# File 'lib/solar_terms_24/solar_terms.rb', line 9

def solar_terms
  @solar_terms
end

#timezoneObject

Returns the value of attribute timezone.



8
9
10
# File 'lib/solar_terms_24/solar_terms.rb', line 8

def timezone
  @timezone
end

#yearObject

Returns the value of attribute year.



8
9
10
# File 'lib/solar_terms_24/solar_terms.rb', line 8

def year
  @year
end

Instance Method Details

#change_lang(lang) ⇒ Object



39
40
41
42
43
44
# File 'lib/solar_terms_24/solar_terms.rb', line 39

def change_lang(lang)
  @lang = lang
  @solar_terms.each_value do |solar_term|
    solar_term.lang = lang
  end
end

#change_timezone(timezone) ⇒ Object



32
33
34
35
36
37
# File 'lib/solar_terms_24/solar_terms.rb', line 32

def change_timezone(timezone)
  @timezone = timezone
  @solar_terms.each_value do |solar_term|
    solar_term.timezone = timezone
  end
end

#datesObject



46
47
48
# File 'lib/solar_terms_24/solar_terms.rb', line 46

def dates
  @solar_terms.map { |_, v| v.date }.sort
end

#to_jsonObject



50
51
52
53
54
55
56
57
# File 'lib/solar_terms_24/solar_terms.rb', line 50

def to_json
  JSON.pretty_generate(
    year: @year,
    timezone: @timezone,
    lang: @lang,
    solar_terms: @solar_terms.transform_values(&:as_json)
  )
end