Class: WeatherApiAssignment::WeatherAPIResponse

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/weather_api_assignment/models/weather_api_response.rb

Overview

Weather API overall response model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#to_hash, #to_json

Constructor Details

#initialize(coord = nil, weather = nil, base = nil, main = nil, wind = nil, clouds = nil, dt = nil, sys = nil, id = nil, name = nil, cod = nil, visibility = nil, timezone = nil) ⇒ WeatherAPIResponse

Returns a new instance of WeatherAPIResponse.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 80

def initialize(coord = nil,
               weather = nil,
               base = nil,
               main = nil,
               wind = nil,
               clouds = nil,
               dt = nil,
               sys = nil,
               id = nil,
               name = nil,
               cod = nil,
               visibility = nil,
               timezone = nil)
  @coord = coord
  @weather = weather
  @base = base
  @main = main
  @visibility = visibility
  @wind = wind
  @clouds = clouds
  @dt = dt
  @sys = sys
  @id = id
  @name = name
  @cod = cod
  @timezone = timezone
end

Instance Attribute Details

#baseString

This field contains the base

Returns:



19
20
21
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 19

def base
  @base
end

#cloudsClouds

This field contains the information about clouds

Returns:



35
36
37
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 35

def clouds
  @clouds
end

#codInteger

This information is about cod

Returns:

  • (Integer)


55
56
57
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 55

def cod
  @cod
end

#coordCoord

This field contains information of longitude and latitude

Returns:



11
12
13
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 11

def coord
  @coord
end

#dtLong

This is current date and time

Returns:

  • (Long)


39
40
41
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 39

def dt
  @dt
end

#idLong

This is an identifier

Returns:

  • (Long)


47
48
49
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 47

def id
  @id
end

#mainMain

This field contains the digital stats of current weather

Returns:



23
24
25
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 23

def main
  @main
end

#nameString

This is the name of city

Returns:



51
52
53
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 51

def name
  @name
end

#sysSys

This field contains information about country, sunrise and sunset

Returns:



43
44
45
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 43

def sys
  @sys
end

#timezoneInteger

This field contains information of time zone Id of city

Returns:

  • (Integer)


59
60
61
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 59

def timezone
  @timezone
end

#visibilityLong

This field contains the visibility value

Returns:

  • (Long)


27
28
29
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 27

def visibility
  @visibility
end

#weatherList of Weather

This field contains list of weather information object

Returns:



15
16
17
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 15

def weather
  @weather
end

#windWind

This field contains the wind information of current weather

Returns:



31
32
33
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 31

def wind
  @wind
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 109

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  coord = Coord.from_hash(hash['coord']) if hash['coord']
  # Parameter is an array, so we need to iterate through it
  weather = nil
  unless hash['weather'].nil?
    weather = []
    hash['weather'].each do |structure|
      weather << (Weather.from_hash(structure) if structure)
    end
  end
  base = hash['base']
  main = Main.from_hash(hash['main']) if hash['main']
  wind = Wind.from_hash(hash['wind']) if hash['wind']
  clouds = Clouds.from_hash(hash['clouds']) if hash['clouds']
  dt = hash['dt']
  sys = Sys.from_hash(hash['sys']) if hash['sys']
  id = hash['id']
  name = hash['name']
  cod = hash['cod']
  visibility = hash['visibility']
  timezone = hash['timezone']

  # Create object from extracted values.
  WeatherAPIResponse.new(coord,
                         weather,
                         base,
                         main,
                         wind,
                         clouds,
                         dt,
                         sys,
                         id,
                         name,
                         cod,
                         visibility,
                         timezone)
end

.namesObject

A mapping from model property names to API property names.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/weather_api_assignment/models/weather_api_response.rb', line 62

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['coord'] = 'coord'
  @_hash['weather'] = 'weather'
  @_hash['base'] = 'base'
  @_hash['main'] = 'main'
  @_hash['visibility'] = 'visibility'
  @_hash['wind'] = 'wind'
  @_hash['clouds'] = 'clouds'
  @_hash['dt'] = 'dt'
  @_hash['sys'] = 'sys'
  @_hash['id'] = 'id'
  @_hash['name'] = 'name'
  @_hash['cod'] = 'cod'
  @_hash['timezone'] = 'timezone'
  @_hash
end