locotimezone 0.4.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +71 -13
- data/Rakefile +5 -0
- data/lib/locotimezone/active_record_helper.rb +38 -0
- data/lib/locotimezone/configuration.rb +20 -0
- data/lib/locotimezone/{location.rb → geolocate.rb} +7 -7
- data/lib/locotimezone/loco_time.rb +5 -8
- data/lib/locotimezone/railtie.rb +12 -0
- data/lib/locotimezone/timezone.rb +14 -14
- data/lib/locotimezone/version.rb +1 -1
- data/lib/locotimezone.rb +27 -3
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 783972c9c5361402295db42561c18b992da83d99
|
4
|
+
data.tar.gz: ab45f48e2876e9f0767cab6791e40bff9051d8e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb49e000537228660584ecf4c8bf29dd5cdd8bd4c27df8d9c2c3a2025d65257722bac5579ffa316992822f4306520501a4753a69aff0666ab2a2d003730431fb
|
7
|
+
data.tar.gz: 61f5013bfdaccdd8a8962b5772ba19b05b42b252a77d02332d1e67f5aa34d81c0c19220801008c05f083c56e8fb1f784f33d1c5fdeea2531271aafb15e7a9111
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
[](https://www.codetriage.com/apmiller108/locotimezone)
|
2
2
|
|
3
|
-
|
3
|
+
[](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
|
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
|
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.
|
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 `
|
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
@@ -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
|
3
|
-
attr_reader :
|
3
|
+
class Geolocate
|
4
|
+
attr_reader :address
|
4
5
|
|
5
|
-
def initialize(address
|
6
|
+
def initialize(address)
|
6
7
|
@address = address
|
7
|
-
@key = key
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
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' + '?
|
22
|
-
'&
|
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
|
6
|
+
attr_reader :skip, :address
|
7
7
|
attr_accessor :location
|
8
8
|
|
9
|
-
def initialize(address:, location:, skip
|
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 =
|
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
|
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 :
|
3
|
+
attr_reader :location
|
4
4
|
|
5
|
-
def initialize(location
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
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=' +
|
28
|
-
|
26
|
+
'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' +
|
27
|
+
Locotimezone.configuration.google_api_key + '&location=' +
|
28
|
+
latitude_longitude + '×tamp=' + timestamp
|
29
29
|
end
|
30
30
|
|
31
31
|
def latitude_longitude
|
32
|
-
|
33
|
-
|
34
|
-
|
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['
|
42
|
+
return {} if response['timeZoneId'].nil?
|
43
43
|
Hash[
|
44
44
|
timezone_id: response['timeZoneId'],
|
45
45
|
timezone_name: response['timeZoneName']
|
data/lib/locotimezone/version.rb
CHANGED
data/lib/locotimezone.rb
CHANGED
@@ -1,17 +1,41 @@
|
|
1
1
|
require 'locotimezone/version'
|
2
2
|
require 'locotimezone/loco_time'
|
3
|
-
require 'locotimezone/
|
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
|
+
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-
|
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/
|
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
|