locotimezone 0.4.1 → 1.0.0

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: e657574099f1b13af081ca48ef623040afa09d37
4
- data.tar.gz: 259c4a4b9276418d3444e96dfe59dd4e44aa5f86
3
+ metadata.gz: 783972c9c5361402295db42561c18b992da83d99
4
+ data.tar.gz: ab45f48e2876e9f0767cab6791e40bff9051d8e6
5
5
  SHA512:
6
- metadata.gz: 65f6917a871bdfc316b91457130b76cf94cdaa6d716fefceda5dd23b7960c5982366b801e7f00681e13ab27c85e81dd23c3182cd95e8fa591e6b05c7aade9293
7
- data.tar.gz: f4963c6a2c9da0446e48d861e72bac011b389e3305bd7e927588ad5e4b22c12689d7f6d9bd63e65b830b858b06fe74a7ffc2e35226b05b0529deaf676fbf1826
6
+ metadata.gz: fb49e000537228660584ecf4c8bf29dd5cdd8bd4c27df8d9c2c3a2025d65257722bac5579ffa316992822f4306520501a4753a69aff0666ab2a2d003730431fb
7
+ data.tar.gz: 61f5013bfdaccdd8a8962b5772ba19b05b42b252a77d02332d1e67f5aa34d81c0c19220801008c05f083c56e8fb1f784f33d1c5fdeea2531271aafb15e7a9111
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /pkg/
9
9
  /spec/reports/
10
10
  /tmp/
11
+ /rails_locotimezone
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  [![Code Triagers Badge](https://www.codetriage.com/apmiller108/locotimezone/badges/users.svg)](https://www.codetriage.com/apmiller108/locotimezone)
2
2
 
3
- # Locotimezone
3
+ [![Build Status](https://travis-ci.org/apmiller108/locotimezone.svg?branch=active-model-integration)](https://travis-ci.org/apmiller108/locotimezone)
4
4
 
5
+ # Locotimezone
5
6
  Transform a street address into geoloction and timezone data. Essentially, this
6
7
  is an adapter for the [Google Maps Time Zone API](https://developers.google.com/maps/documentation/timezone/intro) and the [The Google Maps Geolocation API](https://developers.google.com/maps/documentation/geolocation/intro).
7
8
 
8
9
  All requests to the Google APIs are done over SSL.
9
10
 
10
11
  ## Installation
11
-
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
@@ -23,7 +23,61 @@ Or install it yourself as:
23
23
 
24
24
  $ gem install locotimezone
25
25
 
26
- ## Usage
26
+ ## Rails Usage
27
+ Setup an initializer. It might look something like this:
28
+
29
+ ```ruby
30
+ # config/initializers/locotimezone.rb
31
+
32
+ Locotimezone.configure do |config|
33
+ config.google_api_key = ENV['GOOGLE_API_KEY']
34
+ end
35
+ ```
36
+
37
+ You can use a callback to set the following attributes on your model:
38
+ `:latitude`, `:longitude`, and `:timezone_id`. Most likely, the address data
39
+ will be in stored separate fields, so create method that aggregates the address
40
+ information into a single string. For more options, see [detailed usage](#detailed-usage) below.
41
+
42
+ ```ruby
43
+ # app/models/user.rb
44
+
45
+ class User < ApplicationRecord
46
+ after_validation -> { locotime address: address }
47
+
48
+ def address
49
+ [street, city, region, postal_code, country].join(' ')
50
+ end
51
+ end
52
+ ```
53
+
54
+ The default model attributes are `:latitude`, `:longitude`, and `:timezone_id`.
55
+ You can override the defaults and setup your own default attribute names in the
56
+ configuration block.
57
+
58
+ ```ruby
59
+ # config/initializers/locotimezone.rb
60
+
61
+ Locotimezone.configure do |config|
62
+ config.google_api_key = ENV['GOOGLE_API_KEY']
63
+ config.attributes = {
64
+ latitude: :lat,
65
+ longitude: :lng,
66
+ timezone_id: :tz_id
67
+ }
68
+ end
69
+ ```
70
+
71
+
72
+ ## Detailed Usage
73
+ First, set your [Google API
74
+ key](https://developers.google.com/maps/documentation/geocoding/get-api-key):
75
+
76
+ ```ruby
77
+ Locotimezone.configure do |config|
78
+ config.google_api_key = 'YOUR_API_KEY'
79
+ end
80
+ ```
27
81
  Geolocate and get timezone details:
28
82
 
29
83
  ```ruby
@@ -52,7 +106,7 @@ Locotimezone.locotime address: address, skip: :timezone
52
106
  # :location=>{:lat=>26.1288238, :lng=>-80.1449743}}}
53
107
  ```
54
108
 
55
- If you alredy have latitude and longitude, and you're only interested in getting
109
+ If you already have latitude and longitude, and you're only interested in getting
56
110
  the timezone data, just pass your own location hash like so:
57
111
 
58
112
  ```ruby
@@ -80,17 +134,22 @@ Locotimezone.locotime location: { lat: 0, lng: 0 }
80
134
  # {:timezone=>{}}
81
135
  ```
82
136
 
83
- ## Options and Setup
137
+ ## Options
84
138
 
85
139
  `Locotimezone.locotime` can take the following option hash keys:
86
- * `:address` is a string representation of a street address. This is required except when `timezone_only: true`.
140
+ * `:address` is a string representation of a street address.
87
141
  * `:location` is a hash containing the latitude and longitude: `{ lat: 26.1288238, lng: -80.1449743 }`. When passing a location hash, the call to Google Maps Geolocation API is skipped.
88
- * `:key` is for your Google API key. This is not required but recommended if you
89
- want higher API quota. Create an API key and enable APIs in your [Google
90
- Developer Console](https://console.developers.google.com). As an alternative
91
- to passing the `:key` everytime, simply set `GOOGLE_API_KEY` environment variable.
92
142
  * `skip: :timezone` skips the call to Google Maps Timezone API. For geolocation,
93
- only.
143
+ only
144
+
145
+ ## Setup
146
+
147
+ `Locotimezone.configuration` takes a block where the following can be setup:
148
+ * `google_api_key`. Create an API key and enable APIs in your [Google
149
+ Developer Console](https://console.developers.google.com).
150
+ * `attributes`. For overriding the default attribute names used for Rails models.
151
+ The defaults are `:latitude`, `:longitude`, and `:timezone_id`. See example
152
+ above under [Rails usage](#rails-usage)
94
153
 
95
154
  ## Command Line Utility
96
155
 
@@ -117,7 +176,7 @@ It has been tested on the following versions of Ruby:
117
176
 
118
177
  ## Development
119
178
 
120
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
179
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `rake run` for an interactive prompt that will allow you to experiment. `rake run` will automatically run the configuration block which sets the `google_api_key` to `ENV['GOOGLE_API_KEY']`.
121
180
 
122
181
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
182
 
@@ -129,4 +188,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/apmill
129
188
  ## License
130
189
 
131
190
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
132
-
data/Rakefile CHANGED
@@ -7,4 +7,9 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
+ task :run do
11
+ exec 'pry -r locotimezone -I ./lib -e '\
12
+ '"Locotimezone.configure { |c| c.google_api_key = ENV[\'GOOGLE_API_KEY\'] }"'
13
+ end
14
+
10
15
  task :default => :test
@@ -0,0 +1,38 @@
1
+ module Locotimezone
2
+ module ActiveRecordHelper
3
+
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
9
+
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
18
+
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
37
+ end
38
+ end
@@ -0,0 +1,20 @@
1
+ module Locotimezone
2
+ class Configuration
3
+ attr_accessor :google_api_key
4
+ attr_reader :attributes
5
+
6
+ def initialize
7
+ @attributes = {
8
+ latitude: :latitude,
9
+ longitude: :longitude,
10
+ timezone_id: :timezone_id
11
+ }
12
+ end
13
+
14
+ def attributes=(value)
15
+ if value.respond_to? :has_key?
16
+ @attributes = attributes.merge value
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,13 +1,13 @@
1
+ require 'pry'
1
2
  module Locotimezone
2
- class Location
3
- attr_reader :key, :address
3
+ class Geolocate
4
+ attr_reader :address
4
5
 
5
- def initialize(address, key)
6
+ def initialize(address)
6
7
  @address = address
7
- @key = key
8
8
  end
9
9
 
10
- def geolocate
10
+ def get_geo
11
11
  response = open(geolocation_query_url) { |f| JSON.parse f.read }
12
12
  rescue OpenURI::HTTPError
13
13
  {}
@@ -18,8 +18,8 @@ module Locotimezone
18
18
  private
19
19
 
20
20
  def geolocation_query_url
21
- 'https://maps.googleapis.com/maps/api/geocode/json' + '?key=' + key +
22
- '&address=' + address.to_s
21
+ 'https://maps.googleapis.com/maps/api/geocode/json' + '?address=' +
22
+ address.to_s + '&key=' + Locotimezone.configuration.google_api_key
23
23
  end
24
24
 
25
25
  def format_results(response)
@@ -3,14 +3,13 @@ require 'json'
3
3
 
4
4
  module Locotimezone
5
5
  class LocoTime
6
- attr_reader :skip, :address, :key
6
+ attr_reader :skip, :address
7
7
  attr_accessor :location
8
8
 
9
- def initialize(address:, location:, skip:, key:)
9
+ def initialize(address:, location:, skip:)
10
10
  @location = location
11
11
  @address = address
12
12
  @skip = location ? :location : skip
13
- @key = key || ''
14
13
  end
15
14
 
16
15
  def transform
@@ -24,19 +23,18 @@ module Locotimezone
24
23
 
25
24
  def validate_options
26
25
  if address.nil? && (skip == :timezone || skip.nil?)
27
- raise ArgumentError,
28
- 'locotimezone: missing address or location.'
26
+ raise ArgumentError, 'locotimezone is missing address or location.'
29
27
  end
30
28
  end
31
29
 
32
30
  def get_location
33
- results = Location.new(address, key).geolocate
31
+ results = Geolocate.new(address).get_geo
34
32
  self.location = results[:location] || {}
35
33
  results
36
34
  end
37
35
 
38
36
  def get_timezone
39
- Timezone.new(location, key).timezone
37
+ Timezone.new(location).timezone
40
38
  end
41
39
 
42
40
  def build_hash(location_data, timezone_data)
@@ -45,6 +43,5 @@ module Locotimezone
45
43
  data[:timezone] = timezone_data unless timezone_data.nil?
46
44
  data
47
45
  end
48
-
49
46
  end
50
47
  end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ require 'locotimezone/active_record_helper'
3
+
4
+ module Locotimezone
5
+ class Railtie < Rails::Railtie
6
+ initializer 'locotimezone.active_record_helper' do
7
+ ActiveSupport.on_load(:active_record) do
8
+ include Locotimezone::ActiveRecordHelper
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,37 +1,37 @@
1
1
  module Locotimezone
2
2
  class Timezone
3
- attr_reader :key, :location
3
+ attr_reader :location
4
4
 
5
- def initialize(location, key)
5
+ def initialize(location)
6
6
  @location = location
7
- @key = key
8
7
  end
9
8
 
10
9
  def timezone
11
10
  return {} if location_invalid?
12
11
  response = open(timezone_query_url) { |f| JSON.parse f.read }
13
- rescue OpenURI::HTTPError
14
- {}
15
- else
16
- format_results response
12
+ rescue OpenURI::HTTPError
13
+ {}
14
+ else
15
+ format_results response
17
16
  end
18
17
 
19
18
  private
20
19
 
21
20
  def location_invalid?
22
- return true if !location.respond_to? :has_key?
21
+ return true unless location.respond_to? :has_key?
23
22
  location.empty?
24
23
  end
25
24
 
26
25
  def timezone_query_url
27
- 'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' + key +
28
- '&location=' + latitude_longitude + '&timestamp=' + timestamp
26
+ 'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' +
27
+ Locotimezone.configuration.google_api_key + '&location=' +
28
+ latitude_longitude + '&timestamp=' + timestamp
29
29
  end
30
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
+ lat_lng = Array.new
33
+ location.each { |k, v| lat_lng.push v.to_s }
34
+ lat_lng.join(',')
35
35
  end
36
36
 
37
37
  def timestamp
@@ -39,7 +39,7 @@ module Locotimezone
39
39
  end
40
40
 
41
41
  def format_results(response)
42
- return {} if response['status'] == 'ZERO_RESULTS'
42
+ return {} if response['timeZoneId'].nil?
43
43
  Hash[
44
44
  timezone_id: response['timeZoneId'],
45
45
  timezone_name: response['timeZoneName']
@@ -1,3 +1,3 @@
1
1
  module Locotimezone
2
- VERSION = "0.4.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/locotimezone.rb CHANGED
@@ -1,17 +1,41 @@
1
1
  require 'locotimezone/version'
2
2
  require 'locotimezone/loco_time'
3
- require 'locotimezone/location'
3
+ require 'locotimezone/geolocate'
4
4
  require 'locotimezone/timezone'
5
+ require 'locotimezone/configuration'
6
+ require 'locotimezone/active_record_helper'
7
+ require 'locotimezone/railtie' if defined?(Rails)
5
8
 
6
9
  module Locotimezone
7
10
 
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
8
15
  def self.locotime(options = {})
16
+ set_default_configuration
9
17
  LocoTime.new(
10
18
  location: options.fetch(:location, nil),
11
19
  address: options.fetch(:address, nil),
12
- skip: options.fetch(:skip, nil),
13
- key: options.fetch(:key, ENV['GOOGLE_API_KEY'])
20
+ skip: options.fetch(:skip, nil)
14
21
  ).transform
15
22
  end
16
23
 
24
+ def self.configure
25
+ self.configuration ||= Configuration.new
26
+ yield configuration if block_given?
27
+ self
28
+ end
29
+
30
+ def self.reset_configuration
31
+ self.configuration = Configuration.new
32
+ end
33
+
34
+ def self.set_default_configuration
35
+ if Locotimezone.configuration.nil?
36
+ Locotimezone.configure do |config|
37
+ config.google_api_key = ''
38
+ end
39
+ end
40
+ end
17
41
  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: 0.4.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Miller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-16 00:00:00.000000000 Z
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,8 +84,11 @@ files:
84
84
  - bin/locotimezone
85
85
  - bin/setup
86
86
  - lib/locotimezone.rb
87
- - lib/locotimezone/location.rb
87
+ - lib/locotimezone/active_record_helper.rb
88
+ - lib/locotimezone/configuration.rb
89
+ - lib/locotimezone/geolocate.rb
88
90
  - lib/locotimezone/loco_time.rb
91
+ - lib/locotimezone/railtie.rb
89
92
  - lib/locotimezone/timezone.rb
90
93
  - lib/locotimezone/version.rb
91
94
  - locotimezone.gemspec