class OpenWeather::QueryBuilder

Attributes

arguments[R]
query_options[R]

Public Class Methods

new(args, use_id=true) click to toggle source
# File lib/open_weather/query_builder.rb, line 9
def initialize(args, use_id=true)
  clean_args = Array(args)
  @query_options = clean_args.extract_options!
  @arguments = clean_args
end

Public Instance Methods

build() click to toggle source
# File lib/open_weather/query_builder.rb, line 15
def build
  query = _from_array(arguments)
  query.merge(query_options)
end

Private Instance Methods

_fetch_array(obj, meth) click to toggle source
# File lib/open_weather/query_builder.rb, line 81
def _fetch_array(obj, meth)
  _from_array(Array(obj.send(meth))) if obj.respond_to?(meth)
end
_fetch_value(obj, meth) click to toggle source
# File lib/open_weather/query_builder.rb, line 77
def _fetch_value(obj, meth)
  obj.send(meth) if obj.respond_to?(meth)
end
_from_array(args) click to toggle source
# File lib/open_weather/query_builder.rb, line 22
def _from_array(args)
  query_keys = [:lat, :lon]
  args.size > 1 ? Hash[query_keys.zip(args)] : _from_object(args[0])
end
_from_coordinates(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 65
def _from_coordinates(obj)
  _from_array(obj.to_coordinates) if obj.respond_to?(:to_coordinates)
end
_from_lat_lon(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 60
def _from_lat_lon(obj)
  lat, lon = _get_lat(obj), _get_lon(obj)
  (lat && lon) ? _from_array([ lat, lon ]) : nil
end
_from_methods(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 85
def _from_methods(obj)
  _from_lat_lon(obj) ||
    _from_coordinates(obj) ||
    _from_other(obj) ||
    raise(QueryError, "Don't know how to obtain weather from #{obj.class.name}")
end
_from_object(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 32
def _from_object(obj)
  case obj
  when NilClass
    raise QueryError, 'you should provide at least one location'
  when Array
    _from_array(obj)
  when Hash
    _from_methods(OpenStruct.new(obj))
  when String
    _from_string(obj)
  when Integer
    { id: obj }
  else
    _from_methods(obj)
  end
end
_from_other(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 69
def _from_other(obj)
  _fetch_array(obj, :location) ||
    _fetch_array(obj, :address) ||
    _fetch_array(obj, :region) ||
    _fetch_array(obj, :city) ||
    _fetch_array(obj, :attributes)
end
_from_string(str) click to toggle source
# File lib/open_weather/query_builder.rb, line 27
def _from_string(str)
  key = str =~ /\A\d+\Z/ ? :id : :q
  { key => str }
end
_get_lat(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 49
def _get_lat(obj)
  _fetch_value(obj, :lat) ||
    _fetch_value(obj, :latitude)
end
_get_lon(obj) click to toggle source
# File lib/open_weather/query_builder.rb, line 54
def _get_lon(obj)
  _fetch_value(obj, :lon) ||
    _fetch_value(obj, :lng) ||
    _fetch_value(obj, :latitude)
end