Class: DarkskyRubyClient::Client

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

Constant Summary collapse

@@base_url =
'https://api.darksky.net/forecast/'

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Initialize

Parameters:

  • key (String)

    Secret key of the api.



26
27
28
29
30
# File 'lib/darksky_ruby_client/client.rb', line 26

def initialize(key)
  @secret_key = key
  @parallel_requests = false
  @responses = {}
end

Instance Method Details

#base_urlObject

Don’t need this method to use until the base_url of darksky api has changed.



15
16
17
# File 'lib/darksky_ruby_client/client.rb', line 15

def base_url
  @@base_url
end

#base_url=(value) ⇒ Object

Don’t need this method to use until the base_url of darksky api has changed.



20
21
22
# File 'lib/darksky_ruby_client/client.rb', line 20

def base_url=(value)
  @@base_url = value
end

#init_clientObject

Init client for typhoeus



161
162
163
# File 'lib/darksky_ruby_client/client.rb', line 161

def init_client
  @client = Typhoeus::Request.new(@request_url)
end

#runObject

Run a request.



166
167
168
# File 'lib/darksky_ruby_client/client.rb', line 166

def run
  @res = @client.run
end

#some_weather_forecast(locations, **options) ⇒ Object

Parallel request for the forecast api. and must follow the specified format. {

:place_name => {
  :latitude => "latitude value",
  :longitude => "longitude value"
},
:place_name => {
  :latitude => "latitude value",
  :longitude => "longitude value"
}

} Please check darksky.net/dev/docs#forecast-request for details. exlude= e.g. exclude: “currently,flags” Exclude some number of data blocks from the API response. This is useful for reducing latency and saving cache space. The value blocks should be a comma-delimeted list (without spaces) of any of the following:

  • currently

  • minutely

  • hourly

  • daily

  • alerts

  • flags

extend= e.g. extend: “24” When present, return hour-by-hour data for the next 168 hours, instead of the next 48. When using this option, we strongly recommend enabling HTTP compression. e.g. lang: “en” Return summary properties in the desired language. (Note that units in the summary will be set according to the units parameter, so be sure to set both parameters appropriately.) Please check with darksky.net/dev/docs#forecast-request for available languages. units= e.g. units: “auto” Return weather conditions in the requested units.

units

should be one of the following:

  • auto: automatically select units based on geographic location

  • ca: same as si, except that windSpeed and windGust are in kilometers per hour

  • uk2: same as si, except that nearestStormDistance and visibility are in miles,

    and windSpeed and windGust in miles per hour
    
  • us: Imperial units (the default)

  • si: SI units

Please check with darksky.net/dev/docs#forecast-request for available SI units.

Parameters:

  • locations (Hash)

    Must set it in two dimensional hash

  • options (String)

    Use if you want to specify “explicit, extend, lang, units”.

Options Hash (**options):

  • exclude (String)
  • extend (String)
  • lang (String)
  • units (String)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/darksky_ruby_client/client.rb', line 138

def some_weather_forecast(locations, **options)
  @parallel_requests = true
  hydra = Typhoeus::Hydra.new
  locations.with_indifferent_access
  locations.each {|place_name, location|
    latitude = location[:latitude] if location.include?(:latitude)
    longitude = location[:longitude] if location.include?(:longitude)
    weather_forecast(latitude, longitude, options)
    @client.on_complete do |response|
      if response.code == 200
        @responses[place_name] = JSON.parse(response.body)
      else
        @log.error("HTTP request failed: " + response.code.to_s)
      end
    end
    hydra.queue @client
    @client
  }
  hydra.run
  return @responses
end

#weather_forecast(lat, long, **options) ⇒ Object

Single request for the forecast api. Please check darksky.net/dev/docs#forecast-request for details. exlude= e.g. exclude: “currently,flags” Exclude some number of data blocks from the API response. This is useful for reducing latency and saving cache space. The value blocks should be a comma-delimeted list (without spaces) of any of the following:

  • currently

  • minutely

  • hourly

  • daily

  • alerts

  • flags

extend= e.g. extend: “24” When present, return hour-by-hour data for the next 168 hours, instead of the next 48. When using this option, we strongly recommend enabling HTTP compression. e.g. lang: “en” Return summary properties in the desired language. (Note that units in the summary will be set according to the units parameter, so be sure to set both parameters appropriately.) Please check with darksky.net/dev/docs#forecast-request for available languages. units= e.g. units: “auto” Return weather conditions in the requested units.

units

should be one of the following:

  • auto: automatically select units based on geographic location

  • ca: same as si, except that windSpeed and windGust are in kilometers per hour

  • uk2: same as si, except that nearestStormDistance and visibility are in miles,

    and windSpeed and windGust in miles per hour
    
  • us: Imperial units (the default)

  • si: SI units

Please check with darksky.net/dev/docs#forecast-request for available SI units.

Parameters:

  • lat (String)

    The latitude of a location (in decimal degrees).

  • long (String)

    The longitude of a location (in decimal degrees).

  • options (String)

    Use if you want to specify “explicit, extend, lang, units”.

Options Hash (**options):

  • exclude (String)
  • extend (String)
  • lang (String)
  • units (String)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/darksky_ruby_client/client.rb', line 69

def weather_forecast(lat, long, **options)
  @request_url = @@base_url + @secret_key + '/' + lat + ',' + long + '/'
  unless options.empty? then
    options.each {|key, value|
      if [:exclude, :extend, :lang, :units].include?(key)
        if @request_url[-1] == '/'
          @request_url << '?' + key.to_s + '=' + value
        else
          @request_url << '&' + key.to_s + '=' + value
        end
      else
        @log.warn("invalid options key \"#{key}\".")
      end
    }
  end
  init_client
  unless @parallel_requests
    run
    JSON.parse(@res.body) if @res.code == 200
  end
end