Class: WeatherJp::City

Inherits:
Object
  • Object
show all
Defined in:
lib/weather_jp/city.rb

Constant Summary collapse

KEYS =

Canonical key names for attributes.

%i(name full_name code latitude longitude)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = {}) ⇒ City

Returns a new instance of City.

Parameters:

  • name (String)
  • attrs (Hash) (defaults to: {})


11
12
13
14
# File 'lib/weather_jp/city.rb', line 11

def initialize(name, attrs = {})
  @name = name
  @attrs = attrs
end

Instance Attribute Details

#nameString (readonly) Also known as: to_s

Returns the current value of name.

Returns:

  • (String)

    the current value of name



3
4
5
# File 'lib/weather_jp/city.rb', line 3

def name
  @name
end

Instance Method Details

#codeString Also known as: area_code, location_code

i.e. ‘JAXX0085’

Returns:

  • (String)


23
24
25
26
27
28
29
# File 'lib/weather_jp/city.rb', line 23

def code
  if str = @attrs['weatherlocationcode']
    str.split(':').last
  else
    nil
  end
end

#full_nameString

Returns:

  • (String)


17
18
19
# File 'lib/weather_jp/city.rb', line 17

def full_name
  @attrs['weatherlocationname']
end

#latitudeFloat Also known as: lat

Returns:

  • (Float)


34
35
36
# File 'lib/weather_jp/city.rb', line 34

def latitude
  @attrs['lat'].try(:to_f)
end

#longitudeFloat Also known as: long

Returns:

  • (Float)


40
41
42
# File 'lib/weather_jp/city.rb', line 40

def longitude
  @attrs['long'].try(:to_f)
end

#to_hashHash

Returns:

  • (Hash)


48
49
50
# File 'lib/weather_jp/city.rb', line 48

def to_hash
  Hash[KEYS.map {|k| [k, public_send(k)] }]
end