Class: Timezone::Lookup::Google

Inherits:
Basic
  • Object
show all
Defined in:
lib/timezone/lookup/google.rb

Instance Attribute Summary

Attributes inherited from Basic

#config

Instance Method Summary collapse

Methods inherited from Basic

#client

Constructor Details

#initialize(config) ⇒ Google

Returns a new instance of Google.



12
13
14
15
16
17
# File 'lib/timezone/lookup/google.rb', line 12

def initialize(config)
  if config.google_api_key.nil?
    raise(::Timezone::Error::InvalidConfig, 'missing api key')
  end
  super
end

Instance Method Details

#lookup(lat, lng) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/timezone/lookup/google.rb', line 19

def lookup(lat,lng)
  response = client.get(url(lat,lng))

  if response.code == '403'
    raise(Timezone::Error::Google, '403 Forbidden')
  end

  return unless response.code =~ /^2\d\d$/
  data = JSON.parse(response.body)

  if data['status'] != 'OK'
    raise(Timezone::Error::Google, data['errorMessage'])
  end

  data['timeZoneId']
rescue => e
  raise(Timezone::Error::Google, e.message)
end