Class: UserTimezone::TimezoneDetector
- Inherits:
-
Object
- Object
- UserTimezone::TimezoneDetector
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#api_request_url(object) ⇒ String
Gets the API request URL with the search parameters.
-
#default_options ⇒ Hash
Default options for the timezone detector class.
-
#detect(object, what_to_detect = 'timezone') ⇒ String
Timezone value or nil if no timezone was found for the given object.
-
#err(e) ⇒ Object
Log out an error using the system’s logger.
-
#gen_array_attribute_filters(object, attributes) ⇒ String
Gets the API request URL with the search parameters.
- #gen_map_attribute_filters(object, map) ⇒ Object
- #gen_object_filter(object, local_name, filter_name) ⇒ Object
-
#get_filters(object) ⇒ Array
Gets the API request filter parts.
-
#initialize(options = {}) ⇒ TimezoneDetector
constructor
Check out github.com/jayelkaake/user_timezone for more information on options.
-
#log(msg) ⇒ Object
Logs out as ‘info’ level using whatever logger the system is using.
Constructor Details
#initialize(options = {}) ⇒ TimezoneDetector
Check out github.com/jayelkaake/user_timezone for more information on options
17 18 19 20 21 |
# File 'lib/user_timezone/timezone_detector.rb', line 17 def initialize( = {}) @options = .merge() @request_cache = {} @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/user_timezone/timezone_detector.rb', line 12 def @options end |
Instance Method Details
#api_request_url(object) ⇒ String
Gets the API request URL with the search parameters
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_options ⇒ Hash
Returns default options for the timezone detector class.
49 50 51 52 53 54 55 |
# File 'lib/user_timezone/timezone_detector.rb', line 49 def { 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.
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.
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
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
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 |