Class: WorldTimeEngine::Client

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/worldtimeengine/client.rb

Overview

Wrapper for the WorldTimeEngine API

Instance Attribute Summary

Attributes included from Configurable

#api_key, #endpoint, #timeout

Instance Method Summary collapse

Methods included from Configurable

#configure, #credentials?, keys, #reset!

Constructor Details

#initialize(options = {}) ⇒ WorldTimeEngine::Client

Initializes a new Client object



14
15
# File 'lib/worldtimeengine/client.rb', line 14

def initialize(options={})
end

Instance Method Details

#api(ip) ⇒ Hashi::Mash

Fetch information from WorldTimeEngine about IP address

Raises:

  • Error raised when supplied user credentials are not valid.

Parameters:

  • address [String]

Returns:

  • IP info



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/worldtimeengine/client.rb', line 27

def api(ip)
  endpoint = WorldTimeEngine.instance_variable_get(:@endpoint)
  api_key = WorldTimeEngine.instance_variable_get(:@api_key)
  timeout = WorldTimeEngine.instance_variable_get(:@timeout)

  raise ArgumentError.new(":api_key wasn't set in configuration block") if api_key.nil?

  hash = HTTParty.get("#{endpoint}/api/ip/#{api_key}/#{ip}", :format => :xml, :timeout => timeout.to_i).to_hash

  if hash.keys.include?('error')
    raise "[WorldTimeEngine] Invalid or Expired API Key used; Error code: 10001"
  end

  timezone_hash = hash['timezone']

  timezone_hash.delete('xmlns:xsi')
  timezone_hash.delete('xsi:noNamespaceSchemaLocation')

  Hashie::Mash.new(timezone_hash).tap do |mash|
    mash.time.utc = Time.parse "#{mash.time.utc} UTC"
    mash.time.local = Time.parse "#{mash.time.local} #{mash.time.zone.current.abbreviation} #{mash.time.zone.current.utcoffset}"

    mash.location.latitude = mash.location.latitude.to_f
    mash.location.longitude = mash.location.longitude.to_f

    mash.time.zone.has_dst = to_boolean mash.time.zone.hasDST

    mash.time.zone.current.is_dst = to_boolean mash.time.zone.current.isdst

    mash.time.zone.current.utc_offset = to_formatted_offset mash.time.zone.current.utcoffset

    # could be missing for not recognized IPs
    unless mash.time.zone.current.effectiveUntil.nil?
      mash.time.zone.current.effective_until = Time.parse "#{mash.time.zone.current.effectiveUntil} #{mash.time.zone.current.abbreviation} #{mash.time.zone.current.utcoffset}"
    end

    if mash.time.zone.has_dst
      mash.time.zone.next.is_dst = to_boolean mash.time.zone.next.isdst
      mash.time.zone.next.utc_offset = to_formatted_offset mash.time.zone.next.utcoffset
      mash.time.zone.next.effective_until = Time.parse "#{mash.time.zone.next.effectiveUntil} #{mash.time.zone.next.abbreviation} #{mash.time.zone.next.utcoffset}"
    end
  end
end

#get(path, params = {}, options = {}) ⇒ Object

Perform an HTTP GET request



18
19
20
# File 'lib/worldtimeengine/client.rb', line 18

def get(path, params={}, options={})
  request(:get, path, params, options)
end