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.



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

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

Class Method Details

.stub(uri, body) ⇒ Object



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

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

.stubsObject



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

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

.to_query(params) ⇒ Object



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

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

Instance Method Details

#getObject



48
49
50
51
52
53
54
# File 'lib/gcoder/geocoder.rb', line 48

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



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

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



38
39
40
41
42
# File 'lib/gcoder/geocoder.rb', line 38

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

#uriObject



44
45
46
# File 'lib/gcoder/geocoder.rb', line 44

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