locotimezone 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 337171fec57c496eedd9e382daeba4613d28d119
4
- data.tar.gz: 5e2f61e63b6e3cc818a781fe5f29fce5b7542043
3
+ metadata.gz: c0e7631eda8f0cda4fc507c70ea07b8a14bf04ea
4
+ data.tar.gz: 70c038d85f1bf0305ef1cb2210b3a205bcf38566
5
5
  SHA512:
6
- metadata.gz: 37ac2f7f62e90c0b9d27426e0f6cbbfcec54904e33f4f3005aaef66105ed17ca9bf003e4987eca89643e783c34b55932107a05d14aba8945c45278a6b8249517
7
- data.tar.gz: 5e07f8f7c83cc98d304a74ae92fe50d62e45444c68f0108b02af12db93f6305609a906dd57c5a9111a7e42016c95b35a093de8b630122470037254f2ff14020e
6
+ metadata.gz: b1db75e3ffc577a0b95135a01d2d380eb2a8772d463261ddb5e9e999148be1d6be91d2f5addccd37aeb9eff9c17f61f2f22ef3b47bc9da19bf22a33bfe40498f
7
+ data.tar.gz: 76feec5b03b369128fd3681b07c947b90dd0b5db4f9e783f7b1298d3791c19cc40fefaa1d81f51a2c22e2081c1c217189b1b2e79450c3964a510271e555869cb
data/lib/locotimezone.rb CHANGED
@@ -2,7 +2,10 @@ require 'locotimezone/version'
2
2
  require 'locotimezone/locotime'
3
3
  require 'locotimezone/geolocate'
4
4
  require 'locotimezone/timezone'
5
+ require 'locotimezone/errors'
6
+ require 'locotimezone/error_logger'
5
7
  require 'locotimezone/configuration'
8
+ require 'locotimezone/results_formatter'
6
9
  require 'locotimezone/active_record_helper'
7
10
  require 'locotimezone/railtie' if defined?(Rails)
8
11
 
@@ -12,10 +15,10 @@ module Locotimezone
12
15
  end
13
16
 
14
17
  def self.locotime(options = {})
15
- set_default_configuration if configuration.nil?
16
- Locotime.new(location: options.fetch(:location, nil),
17
- address: options.fetch(:address, nil),
18
- skip: options.fetch(:skip, nil)).call
18
+ configure_with_defaults if configuration.nil?
19
+ Locotime.new(location: options[:location],
20
+ address: options[:address],
21
+ skip: options[:skip]).call
19
22
  end
20
23
 
21
24
  def self.configure
@@ -26,10 +29,10 @@ module Locotimezone
26
29
 
27
30
  def self.reset_configuration
28
31
  self.configuration = Configuration.new
29
- set_default_configuration
32
+ configure_with_defaults
30
33
  end
31
34
 
32
- def self.set_default_configuration
35
+ def self.configure_with_defaults
33
36
  Locotimezone.configure { |config| config.google_api_key = '' }
34
37
  end
35
38
  end
@@ -1,38 +1,22 @@
1
1
  module Locotimezone
2
2
  module ActiveRecordHelper
3
+ def locotime(options = {})
4
+ data = Locotimezone.locotime(options)
5
+ attr_writers = Locotimezone.configuration.attr_writers
3
6
 
4
- def locotime(options = {})
5
- data = Locotimezone.locotime(options)
6
- geolocation_attributes data[:geo] unless data[:geo].nil?
7
- timezone_attribute data[:timezone] unless data[:timezone].nil?
8
- end
7
+ unless data[:geo].nil? || data[:geo].empty?
8
+ data[:geo][:location].each do |key, value|
9
+ attr_key = (key == :lat ? :latitude : :longitude)
9
10
 
10
- def geolocation_attributes(geolocation_data)
11
- return nil if geolocation_data.empty?
12
- geolocation_data[:location].each do |key, value|
13
- attribute = :latitude
14
- attribute = :longitude if key == :lng
15
- save_attribute(attribute, value)
16
- end
17
- end
11
+ next unless respond_to?(attr_writers[attr_key])
12
+ send(attr_writers[attr_key], value)
13
+ end
14
+ end
18
15
 
19
- def timezone_attribute(timezone_data)
20
- return nil if timezone_data.empty?
21
- save_attribute(:timezone_id, timezone_data[:timezone_id])
22
- end
23
-
24
- def save_attribute(attribute, value)
25
- if self.respond_to? attr_writers[attribute]
26
- send attr_writers[attribute], value
27
- end
28
- end
29
-
30
- def attr_writers
31
- attrs = {}
32
- Locotimezone.configuration.attributes.each do |key, value|
33
- attrs[key] = "#{value}="
34
- end
35
- attrs
36
- end
16
+ unless data[:timezone].nil? || data[:timezone].empty?
17
+ tz_writer = attr_writers[:timezone_id]
18
+ send(tz_writer, data[:timezone][:timezone_id]) if respond_to?(tz_writer)
19
+ end
20
+ end
37
21
  end
38
22
  end
@@ -2,19 +2,30 @@ module Locotimezone
2
2
  class Configuration
3
3
  attr_accessor :google_api_key
4
4
  attr_reader :attributes
5
-
5
+
6
6
  def initialize
7
- @attributes = {
8
- latitude: :latitude,
9
- longitude: :longitude,
10
- timezone_id: :timezone_id
11
- }
7
+ @attributes = default_attributes
12
8
  end
13
9
 
14
10
  def attributes=(value)
15
- if value.respond_to? :has_key?
16
- @attributes = attributes.merge value
11
+ return unless value.respond_to? :has_key?
12
+ @attributes = attributes.merge value
13
+ end
14
+
15
+ def attr_writers
16
+ attributes.each_with_object({}) do |(key, value), hash|
17
+ hash[key] = "#{value}="
17
18
  end
18
19
  end
20
+
21
+ private
22
+
23
+ def default_attributes
24
+ {
25
+ latitude: :latitude,
26
+ longitude: :longitude,
27
+ timezone_id: :timezone_id
28
+ }
29
+ end
19
30
  end
20
31
  end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ module Locotimezone
4
+ class ErrorLogger
5
+ def self.stdout_log_for(error, severity: :warn)
6
+ logger = Logger.new(STDOUT)
7
+ logger.send(severity, 'locotimezone') do
8
+ 'Unable to complete API request. Server responded '\
9
+ "with: #{error.message}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Locotimezone
2
+ class LocoError < StandardError; end
3
+
4
+ class InvalidOptionsError < LocoError
5
+ def initialize(message = 'address or location is required')
6
+ super(message)
7
+ end
8
+ end
9
+ end
@@ -7,30 +7,21 @@ module Locotimezone
7
7
  end
8
8
 
9
9
  def call
10
- response = open(geolocation_query_url) { |f| JSON.parse f.read }
11
- rescue OpenURI::HTTPError
10
+ ResultsFormatter.build_geolocation_hash_for geolocation
11
+ rescue OpenURI::HTTPError => e
12
+ ErrorLogger.stdout_log_for(e, severity: :error)
12
13
  {}
13
- else
14
- format_results response
15
14
  end
16
15
 
17
16
  private
18
17
 
18
+ def geolocation
19
+ open(geolocation_query_url) { |f| JSON.parse f.read }
20
+ end
21
+
19
22
  def geolocation_query_url
20
23
  "https://maps.googleapis.com/maps/api/geocode/json?address="\
21
24
  "#{address}&key=#{Locotimezone.configuration.google_api_key}"
22
25
  end
23
-
24
- def format_results(response)
25
- return {} if response['results'].empty?
26
- Hash[
27
- formatted_address: response['results'][0]['formatted_address'],
28
- location: symbolize_keys(response['results'][0]['geometry']['location'])
29
- ]
30
- end
31
-
32
- def symbolize_keys(response)
33
- response.map { |k, v| [k.to_sym, v] }.to_h
34
- end
35
26
  end
36
27
  end
@@ -7,41 +7,36 @@ module Locotimezone
7
7
  attr_accessor :location
8
8
 
9
9
  def initialize(address:, location:, skip:)
10
- @location = location
11
- @address = address
12
- @skip = location ? :location : skip
10
+ @location = location
11
+ @address = address
12
+ @skip = location ? :location : skip
13
13
  end
14
14
 
15
15
  def call
16
16
  validate_options
17
- location_data = get_location unless skip == :location
18
- timezone_data = get_timezone unless skip == :timezone
19
- build_hash(location_data, timezone_data)
17
+ ResultsFormatter.build_hash_for(geolocation, timezone)
20
18
  end
21
19
 
22
20
  private
23
21
 
24
22
  def validate_options
25
- if address.nil? && (skip == :timezone || skip.nil?)
26
- raise ArgumentError, 'locotimezone is missing address or location.'
27
- end
23
+ raise InvalidOptionsError if options_invalid?
28
24
  end
29
25
 
30
- def get_location
26
+ def options_invalid?
27
+ address.nil? && (skip == :timezone || skip.nil?)
28
+ end
29
+
30
+ def geolocation
31
+ return if skip == :location
31
32
  results = Geolocate.new(address).call
32
- self.location = results[:location] || {}
33
+ @location = results[:location] || {}
33
34
  results
34
35
  end
35
36
 
36
- def get_timezone
37
+ def timezone
38
+ return if skip == :timezone
37
39
  Timezone.new(location).call
38
40
  end
39
-
40
- def build_hash(location_data, timezone_data)
41
- data = Hash.new
42
- data[:geo] = location_data unless location_data.nil?
43
- data[:timezone] = timezone_data unless timezone_data.nil?
44
- data
45
- end
46
41
  end
47
42
  end
@@ -0,0 +1,30 @@
1
+ module Locotimezone
2
+ module ResultsFormatter
3
+ def self.build_hash_for(location, timezone)
4
+ {
5
+ geo: location,
6
+ timezone: timezone
7
+ }
8
+ end
9
+
10
+ def self.build_timezone_hash_for(timezone)
11
+ return {} if timezone['timeZoneId'].nil?
12
+ {
13
+ timezone_id: timezone['timeZoneId'],
14
+ timezone_name: timezone['timeZoneName']
15
+ }
16
+ end
17
+
18
+ def self.build_geolocation_hash_for(location)
19
+ return {} if location['results'].empty?
20
+ {
21
+ formatted_address: location['results'][0]['formatted_address'],
22
+ location: symbolize_keys(location['results'][0]['geometry']['location'])
23
+ }
24
+ end
25
+
26
+ def self.symbolize_keys(hash)
27
+ hash.map { |key, value| [key.to_sym, value] }.to_h
28
+ end
29
+ end
30
+ end
@@ -8,14 +8,15 @@ module Locotimezone
8
8
 
9
9
  def call
10
10
  return {} if location_invalid?
11
- response = open(timezone_query_url) { |f| JSON.parse f.read }
12
- rescue OpenURI::HTTPError
11
+ ResultsFormatter.build_timezone_hash_for timezone_response
12
+ rescue OpenURI::HTTPError => e
13
+ ErrorLogger.stdout_log_for(e, severity: :error)
13
14
  {}
14
- else
15
- format_results response
16
15
  end
17
16
 
18
- private
17
+ def timezone_response
18
+ open(timezone_query_url) { |response| JSON.parse response.read }
19
+ end
19
20
 
20
21
  def location_invalid?
21
22
  return true unless location.respond_to? :has_key?
@@ -23,27 +24,17 @@ module Locotimezone
23
24
  end
24
25
 
25
26
  def timezone_query_url
26
- 'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' +
27
- Locotimezone.configuration.google_api_key + '&location=' +
28
- latitude_longitude + '&timestamp=' + timestamp
27
+ "https://maps.googleapis.com/maps/api/timezone/json?key="\
28
+ "#{Locotimezone.configuration.google_api_key}&location="\
29
+ "#{comma_delimited_location}&timestamp=#{timestamp}"
29
30
  end
30
31
 
31
- def latitude_longitude
32
- lat_lng = Array.new
33
- location.each { |k, v| lat_lng.push v.to_s }
34
- lat_lng.join(',')
32
+ def comma_delimited_location
33
+ location.values.join(',')
35
34
  end
36
35
 
37
36
  def timestamp
38
37
  Time.now.to_i.to_s
39
38
  end
40
-
41
- def format_results(response)
42
- return {} if response['timeZoneId'].nil?
43
- Hash[
44
- timezone_id: response['timeZoneId'],
45
- timezone_name: response['timeZoneName']
46
- ]
47
- end
48
39
  end
49
40
  end
@@ -1,3 +1,3 @@
1
1
  module Locotimezone
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.0.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locotimezone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Miller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-20 00:00:00.000000000 Z
11
+ date: 2017-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -147,9 +147,12 @@ files:
147
147
  - lib/locotimezone.rb
148
148
  - lib/locotimezone/active_record_helper.rb
149
149
  - lib/locotimezone/configuration.rb
150
+ - lib/locotimezone/error_logger.rb
151
+ - lib/locotimezone/errors.rb
150
152
  - lib/locotimezone/geolocate.rb
151
153
  - lib/locotimezone/locotime.rb
152
154
  - lib/locotimezone/railtie.rb
155
+ - lib/locotimezone/results_formatter.rb
153
156
  - lib/locotimezone/timezone.rb
154
157
  - lib/locotimezone/version.rb
155
158
  - locotimezone.gemspec
@@ -174,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
177
  version: '0'
175
178
  requirements: []
176
179
  rubyforge_project:
177
- rubygems_version: 2.5.1
180
+ rubygems_version: 2.6.13
178
181
  signing_key:
179
182
  specification_version: 4
180
183
  summary: Get timezone and gelocation data for a street address.