Class: UserTimezone::TimezoneDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/user_timezone/timezone_detector.rb

Overview

The timezone detector class takes in smoe options, then when given an object (user, contact, account, whatever) that has address information returns back the timezone for that person.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TimezoneDetector

Check out github.com/jayelkaake/user_timezone for more information on options

Parameters:

  • options (Hash) (defaults to: {})

    (optional) Options hash… that’s a bit weird to say.



17
18
19
20
21
# File 'lib/user_timezone/timezone_detector.rb', line 17

def initialize(options = {})
  @options = default_options.merge(options)
  @request_cache = {}
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/user_timezone/timezone_detector.rb', line 12

def options
  @options
end

Instance Method Details

#api_request_url(object) ⇒ String

Gets the API request URL with the search parameters

Parameters:

  • object (Hash)

    What is the subject that we are detecting the timezone for? or alternatively, what is the hash? This can be a hash or object, as long as it contains the attributes that we want (like street, city, country, zip, etc) or has the attributes mapped by the options array “using” key (see github.com/jayelkaake/user_timezone)

Returns:

  • (String)

    api request URL



69
70
71
72
73
74
75
# File 'lib/user_timezone/timezone_detector.rb', line 69

def api_request_url(object)
  api_uri = 'http://timezonedb.wellfounded.ca/api/v1'
  api_request_url = "#{api_uri}/timezones?"
  api_request_url << get_filters(object).join('&')
  log "Making request to #{api_request_url} for timezone."
  api_request_url
end

#default_optionsHash

Returns default options for the timezone detector class.

Returns:

  • (Hash)

    default options for the timezone detector class



49
50
51
52
53
54
55
# File 'lib/user_timezone/timezone_detector.rb', line 49

def default_options
  {
      using: [:city, :state, :country, :zip],
      raise_errors: false,
      as: :timezone
  }
end

#detect(object, what_to_detect = 'timezone') ⇒ String

Returns Timezone value or nil if no timezone was found for the given object.

Parameters:

  • object (Hash)

    What is the subject that we are detecting the timezone for? or alternatively, what is the hash? This can be a hash or object, as long as it contains the attributes that we want (like street, city, country, zip, etc) or has the attributes mapped by the options array “using” key (see github.com/jayelkaake/user_timezone)

  • what_to_detect (String) (defaults to: 'timezone')

    What should be detected? (default: ‘timezone’) Also ‘current_timestamp’ also acceptable.

Returns:

  • (String)

    Timezone value or nil if no timezone was found for the given object.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/user_timezone/timezone_detector.rb', line 32

def detect(object, what_to_detect='timezone')
  request_url = api_request_url(object)
  results = @request_cache[request_url] ? @request_cache[request_url] : HTTParty.get(request_url)
  if results.empty?
    nil
  else
    results.first[what_to_detect]
  end
rescue StandardException => e
  err e.inspect
  raise e if @options[:raise_errors]
  nil
end

#err(e) ⇒ Object

Log out an error using the system’s logger.

Parameters:

  • e (Exception)

    exception or error encountered to send to the logger



140
141
142
# File 'lib/user_timezone/timezone_detector.rb', line 140

def err(e)
  @logger.error(e)
end

#gen_array_attribute_filters(object, attributes) ⇒ String

Gets the API request URL with the search parameters

Parameters:

  • object (Hash)

    What is the subject that we are detecting the timezone for? or alternatively, what is the hash? This can be a hash or object, as long as it contains the attributes that we want (like street, city, country, zip, etc) or has the attributes mapped by the options array “using” key (see github.com/jayelkaake/user_timezone)

  • attributes (Hash)

    Attributes to use in generating filters

Returns:

  • (String)

    api request URL



108
109
110
# File 'lib/user_timezone/timezone_detector.rb', line 108

def gen_array_attribute_filters(object, attributes)
  attributes.map { |filter_name| gen_object_filter(object, filter_name, filter_name) }
end

#gen_map_attribute_filters(object, map) ⇒ Object



112
113
114
# File 'lib/user_timezone/timezone_detector.rb', line 112

def gen_map_attribute_filters(object, map)
  map.map { |local_name, filter_name| gen_object_filter(object, local_name, filter_name) }
end

#gen_object_filter(object, local_name, filter_name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/user_timezone/timezone_detector.rb', line 117

def gen_object_filter(object, local_name, filter_name)
  if object.is_a?(Hash)
    filter_val = object[local_name]
    ("#{filter_name}=" << URI::encode(filter_val)) unless filter_val.nil?
  elsif object.respond_to?(local_name)
    filter_val = object.send(local_name)
    ("#{filter_name}=" << URI::encode(filter_val)) unless filter_val.nil?
  else
    ''
  end
end

#get_filters(object) ⇒ Array

Gets the API request filter parts

Parameters:

  • object (Hash)

    What is the subject that we are detecting the timezone for? or alternatively, what is the hash? This can be a hash or object, as long as it contains the attributes that we want (like street, city, country, zip, etc) or has the attributes mapped by the options array “using” key (see github.com/jayelkaake/user_timezone)

Returns:

  • (Array)

    Gets an array of field_name=value filters for the search



89
90
91
92
93
94
95
# File 'lib/user_timezone/timezone_detector.rb', line 89

def get_filters(object)
  if @options[:using].is_a?(Array)
    gen_array_attribute_filters(object, @options[:using])
  else
    gen_map_attribute_filters(object, @options[:using])
  end
end

#log(msg) ⇒ Object

Logs out as ‘info’ level using whatever logger the system is using



132
133
134
# File 'lib/user_timezone/timezone_detector.rb', line 132

def log(msg)
  @logger.info("UserTimezone::TimezoneDetector - #{msg}") if @options[:log]
end