Class: PostalCoder::GeocodingAPI::Query

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ Query

Returns a new instance of Query.



21
22
23
24
25
26
27
28
# File 'lib/postalcoder/geocoding_api.rb', line 21

def initialize(query, options = {})
  unless query.is_a?(String)
    raise ArgumentError, "query must be a String, not: #{query.class}"
  end
  @config = Config.merge(options)
  @query = query
  validate_state!
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



15
16
17
# File 'lib/postalcoder/geocoding_api.rb', line 15

def query
  @query
end

Class Method Details

.get(query, options = {}) ⇒ Object



17
18
19
# File 'lib/postalcoder/geocoding_api.rb', line 17

def self.get(query, options = {})
  new(query, options).to_hash
end

Instance Method Details

#paramsObject



35
36
37
# File 'lib/postalcoder/geocoding_api.rb', line 35

def params
  BASE_PARAMS.merge(:key => @config[:gmaps_api_key], :q => query)
end

#to_hashObject



30
31
32
33
# File 'lib/postalcoder/geocoding_api.rb', line 30

def to_hash
  return tidy_response if @response
  parse_json_response(http_get)
end

#to_paramsObject



39
40
41
42
43
# File 'lib/postalcoder/geocoding_api.rb', line 39

def to_params
  # No need to escape the keys and values because (so far) they do not
  # contain escapable characters. -- CKN
  params.inject([]) { |a, (k, v)| a << "#{k}=#{v}" }.join('&')
end

#uriObject



45
46
47
# File 'lib/postalcoder/geocoding_api.rb', line 45

def uri
  [BASE_URI, '?', to_params].join
end