Class: TimezoneDetection::IpInfoDB

Inherits:
Object
  • Object
show all
Defined in:
lib/timezone_detection/ip_info_db.rb

Defined Under Namespace

Modules: STATUS_CODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIpInfoDB

Returns a new instance of IpInfoDB.



14
15
16
# File 'lib/timezone_detection/ip_info_db.rb', line 14

def initialize
  @api_key = TimezoneDetection.api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/timezone_detection/ip_info_db.rb', line 7

def api_key
  @api_key
end

Instance Method Details

#get_timezone_offset(ip) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/timezone_detection/ip_info_db.rb', line 18

def get_timezone_offset(ip)
  begin
raise if ip == "127.0.0.1"
    response = Net::HTTP.get(URI("http://api.ipinfodb.com/v3/ip-city/?key=#{api_key}&ip=#{ip}&format=json"))
    json = ActiveSupport::JSON.decode(response)
    if json["statusCode"] == STATUS_CODES::SUCCESS
      extract_timezone_offset(json["timeZone"])
    else
      json["statusMessage"]
    end
  rescue => e
    #TODO make output to Rails logger
    puts e.message
    "Request to http://www.ipinfodb.com/ failed. Please try later!"
    Time.new.utc_offset / 60 / 60
  end
end