Class: GCoder::Geocoder::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/gcoder/geocoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, opts = {}) ⇒ Request

Returns a new instance of Request.



21
22
23
24
# File 'lib/gcoder/geocoder.rb', line 21

def initialize(query, opts = {})
  @config = GCoder.config.merge(opts)
  detect_and_set_query(query)
end

Class Method Details

.stub(uri, body) ⇒ Object



17
18
19
# File 'lib/gcoder/geocoder.rb', line 17

def self.stub(uri, body)
  stubs[uri] = body
end

.stubsObject



13
14
15
# File 'lib/gcoder/geocoder.rb', line 13

def self.stubs
  @stubs ||= {}
end

.to_query(params) ⇒ Object



9
10
11
# File 'lib/gcoder/geocoder.rb', line 9

def self.to_query(params)
  URI.escape(params.map { |key, val| "#{key}=#{val}" }.join('&'))
end

Instance Method Details

#getObject



45
46
47
48
49
50
51
# File 'lib/gcoder/geocoder.rb', line 45

def get
  Timeout.timeout(@config[:timeout]) do
    Response.new(uri, http_get)
  end
rescue Timeout::Error
  raise TimeoutError, "Query timeout after #{@config[:timeout]} second(s)"
end

#paramsObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/gcoder/geocoder.rb', line 26

def params
  p = { :sensor => 'false' }
  p[:address]   = address             if @address
  p[:latlng]    = latlng              if @latlng
  p[:language]  = @config[:language]  if @config[:language]
  p[:region]    = @config[:region]    if @config[:region]
  p[:bounds]    = bounds              if @config[:bounds]
  p[:client]    = @config[:client]    if @config[:client]
  p
end

#pathObject



37
38
39
# File 'lib/gcoder/geocoder.rb', line 37

def path
  "#{PATH}?#{self.class.to_query(params)}#{"&signature=#{sign_key(@config[:key])}" if @config[:key]}"
end

#uriObject



41
42
43
# File 'lib/gcoder/geocoder.rb', line 41

def uri
  "http://#{HOST}#{path}"
end