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



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

def initialize(key)
  @secret_key = key
  @client = Typhoeus::Request.new(@@base_url)
  @options = {}
  @params = {:params => {}}
end

Instance Method Details

#base_urlObject

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



13
14
15
# File 'lib/darksky_ruby_client/client.rb', line 13

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.



18
19
20
# File 'lib/darksky_ruby_client/client.rb', line 18

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

#init_clientObject

Init client for typhoeus



93
94
95
# File 'lib/darksky_ruby_client/client.rb', line 93

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

#runObject

Run a request.



98
99
100
# File 'lib/darksky_ruby_client/client.rb', line 98

def run
  @client.run
end

#some_weather_forecast(options = {}, locations:) ⇒ Object

TODO Parallel requests



88
89
90
# File 'lib/darksky_ruby_client/client.rb', line 88

def some_weather_forecast(options = {}, locations:)

end

#weather_forecast(lat, long, options = {}) ⇒ Object

A single request for forecast request Please check darksky.net/dev/docs#forecast-request for details. exlude= e.g. 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. 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. 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. 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.

Options Hash (options):

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


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

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)
        p @request_url[-1]
        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
end