class DarkskyRubyClient::Client

Public Class Methods

new(key) click to toggle source

Initialize @param [String] key Secret key of the api.

# File lib/darksky_ruby_client/client.rb, line 27
def initialize(key)
  @secret_key = key
  @parallel_requests = false
  @responses = {}
end

Public Instance Methods

base_url() click to toggle source

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

# File lib/darksky_ruby_client/client.rb, line 16
def base_url
  @@base_url
end
base_url=(value) click to toggle source

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

# File lib/darksky_ruby_client/client.rb, line 21
def base_url=(value)
  @@base_url = value
end
init_client() click to toggle source

Init client for typhoeus

# File lib/darksky_ruby_client/client.rb, line 164
def init_client
  @client = Typhoeus::Request.new(@request_url)
end
run() click to toggle source

Run a request.

# File lib/darksky_ruby_client/client.rb, line 169
def run
  @res = @client.run
end
some_weather_forecast(locations, **options) click to toggle source

Parallel request for the forecast api. @param [Hash] locations Must set it in two dimensional hash and must follow the specified format. {

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

} @param [String] options Use if you want to specify “explicit, extend, lang, units”. Please check darksky.net/dev/docs#forecast-request for details. @option options [String] exclude 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

@option options [String] extend 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. @option options [String] lang 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. @option options [String] units 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.

# File lib/darksky_ruby_client/client.rb, line 141
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
        @@logger.error("HTTP request failed: " + response.code.to_s)
      end
    end
    hydra.queue @client
    @client
  }
  hydra.run
  return @responses
end
weather_forecast(lat, long, **options) click to toggle source

Single request for the forecast api. @param [String] lat The latitude of a location (in decimal degrees). @param [String] long The longitude of a location (in decimal degrees). @param [String] options Use if you want to specify “explicit, extend, lang, units”. Please check darksky.net/dev/docs#forecast-request for details. @option options [String] exclude 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

@option options [String] extend 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. @option options [String] lang 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. @option options [String] units 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.

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