Class: WeatherRandom::WeatherApi

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, key) ⇒ WeatherApi

Returns a new instance of WeatherApi.

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/weather_random.rb', line 9

def initialize(url, key)
  raise(ImproperApiURL) if !(/\#key/.match url)

  @uri=url
  @key=key


  @uri = @uri.gsub(/\#key/, @key)
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



19
20
21
# File 'lib/weather_random.rb', line 19

def key
  @key
end

#uriObject

Returns the value of attribute uri.



19
20
21
# File 'lib/weather_random.rb', line 19

def uri
  @uri
end

Instance Method Details

#fetch_uriObject



21
22
23
24
# File 'lib/weather_random.rb', line 21

def fetch_uri
  data = open @uri
  @json_data = JSON.parse (data.readlines.join "")
end

#make_seedObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/weather_random.rb', line 32

def make_seed
  # Returns a string that can be used as the seed for Rand

  # Concatenate a pre-selected list of keys from the feed. This is specific to Wunderground - it needs to be configurable
  string = (['temp_f', 'pressure_mb', 'pressure_in', 'wind_degrees', 'wind_mph', 'wind_gust_mph'].map do |key|
    @json_data['current_observation'][key]
            end).join("")

  seed = (string.split("").map do |char|
    char.unpack('C')[0].to_s
          end).join ""

  seed.to_i
end

#set_data(inp_str) ⇒ Object



27
28
29
30
# File 'lib/weather_random.rb', line 27

def set_data inp_str
  # Parse a JSON string
  @json_data = JSON.parse inp_str
end