locotimezone 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/locotimezone/location.rb +31 -0
- data/lib/locotimezone/loco_time.rb +42 -0
- data/lib/locotimezone/timezone.rb +37 -0
- data/lib/locotimezone/version.rb +3 -0
- data/lib/locotimezone.rb +24 -0
- data/locotimezone.gemspec +33 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60188c3aec6a698ff7cebff12072275d7b3beb58
|
4
|
+
data.tar.gz: 9d4ed88b196ab185d1add6bcb038494bbac0e0fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6bf3368d3f39c4f5b457ec4d540270c1d83503c1589b50d27cbd103df37566d7bccf4bb3e8236d8ebe8656cedec964b5cae9de68b64c23580976193e7905ada
|
7
|
+
data.tar.gz: d2efc12f85835b121b799c13ef98f4cda6cf5ab811e3578627e5e919c2bc457dd5ee7f58358079eb6d49936084eb130fe86080f18f8e2ff3ca4c7a3d1ec17fb6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alex Miller
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Locotimezone
|
2
|
+
|
3
|
+
Transform a street address into geoloction and timezone data. Essentially, this
|
4
|
+
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).
|
5
|
+
|
6
|
+
All requests to the Google APIs are done over SSL.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'locotimezone'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install locotimezone
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
Geolocate and get timezone details:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'locotimezone'
|
29
|
+
|
30
|
+
address = '525 NW 1st Ave Fort Lauderdale, FL 33301'
|
31
|
+
|
32
|
+
Locotimezone.locotime address: address
|
33
|
+
# =>
|
34
|
+
# {:geo=>
|
35
|
+
# {:formatted_address=>"525 NW 1st Ave, Fort Lauderdale, FL 33301, USA",
|
36
|
+
# :location=>{:lat=>26.1288238, :lng=>-80.1449743}},
|
37
|
+
# :timezone=>
|
38
|
+
# {:timezone_id=>"America/New_York", :timezone_name=>"Eastern Daylight Time"}}
|
39
|
+
```
|
40
|
+
|
41
|
+
Not interested in timezone? Skip it.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require 'locotimezone'
|
45
|
+
|
46
|
+
Locotimezone.locotime address: address, location_only: true
|
47
|
+
# =>
|
48
|
+
# {:geo=>
|
49
|
+
# {:formatted_address=>"525 NW 1st Ave, Fort Lauderdale, FL 33301, USA",
|
50
|
+
# :location=>{:lat=>26.1288238, :lng=>-80.1449743}}}
|
51
|
+
```
|
52
|
+
|
53
|
+
Only want timezone? No problem, but you'll need include a location hash.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
location = { lat: 26.1288238, lng: -80.1449743 }
|
57
|
+
|
58
|
+
Locotimezone.locotime location: location, timezone_only: true
|
59
|
+
# =>
|
60
|
+
# {:timezone=>
|
61
|
+
# {:timezone_id=>"America/New_York", :timezone_name=>"Eastern Daylight Time"}}
|
62
|
+
```
|
63
|
+
## Options and Setup
|
64
|
+
|
65
|
+
`Locotimezone.locotime` can take the following option hash keys:
|
66
|
+
* `:address` is a string representation of a street address. This is required except when `timezone_only: true`.
|
67
|
+
* `:location` is required only when `timezone_only: true`. The corresponding
|
68
|
+
value is a hash containing the latitude and longitude: `{ lat: 26.1288238, lng: -80.1449743 }`.
|
69
|
+
* `:key` is for your Google API key. This is not required but recommended if you
|
70
|
+
want higher API quota. Create an API key and enable APIs in your [Google
|
71
|
+
Developer Console](https://console.developers.google.com). As an alternative
|
72
|
+
to passing the `:key` everytime, simply set `GOOGLE_API_KEY` environment variable.
|
73
|
+
* `location_only: true` skips the call to Google Maps Timezone API. Geolocation only.
|
74
|
+
* `timezone_only: true` skips the call to Google Maps Geolocation API. Gets only the
|
75
|
+
timezone data for the specified `:location`. When `timezone_only: true`, you
|
76
|
+
must inlcude the `:location` hash (see above).
|
77
|
+
|
78
|
+
## Versions of Ruby
|
79
|
+
|
80
|
+
This library should work with Ruby 2.1 and higher versions.
|
81
|
+
|
82
|
+
It has been tested on the following versions of Ruby:
|
83
|
+
* Ruby 2.3
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
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.
|
88
|
+
|
89
|
+
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).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/apmiller108/locotimezone.
|
94
|
+
|
95
|
+
|
96
|
+
## License
|
97
|
+
|
98
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
99
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "locotimezone"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class Location
|
2
|
+
attr_reader :key, :address
|
3
|
+
|
4
|
+
def initialize(address, key)
|
5
|
+
@address = address
|
6
|
+
@key = key
|
7
|
+
end
|
8
|
+
|
9
|
+
def geolocate
|
10
|
+
response = open(geolocation_query_url) { |f| JSON.parse f.read }
|
11
|
+
format_results response
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def geolocation_query_url
|
17
|
+
'https://maps.googleapis.com/maps/api/geocode/json' + '?key=' + key +
|
18
|
+
'&address=' + address
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_results(response)
|
22
|
+
Hash[
|
23
|
+
formatted_address: response['results'][0]['formatted_address'],
|
24
|
+
location: symbolize_keys(response['results'][0]['geometry']['location'])
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
def symbolize_keys(response)
|
29
|
+
response.map { |k,v| [k.to_sym, v] }.to_h
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class LocoTime
|
5
|
+
attr_reader :location_only, :timezone_only, :address, :key
|
6
|
+
attr_accessor :location
|
7
|
+
|
8
|
+
def initialize(location_only:, timezone_only:, address:, location:, key:)
|
9
|
+
@location_only = location_only
|
10
|
+
@timezone_only = timezone_only
|
11
|
+
@location = location
|
12
|
+
@address = address
|
13
|
+
@key = key
|
14
|
+
end
|
15
|
+
|
16
|
+
def transform
|
17
|
+
return nil if location_only && timezone_only
|
18
|
+
location_data = get_location unless timezone_only
|
19
|
+
timezone_data = get_timezone unless location_only
|
20
|
+
build_hash(location_data, timezone_data)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def get_location
|
26
|
+
results = Location.new(address, key).geolocate
|
27
|
+
self.location = results[:location]
|
28
|
+
results
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_timezone
|
32
|
+
Timezone.new(location, key).timezone
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_hash(location_data, timezone_data)
|
36
|
+
data = Hash.new
|
37
|
+
data[:geo] = location_data unless location_data.nil?
|
38
|
+
data[:timezone] = timezone_data unless timezone_data.nil?
|
39
|
+
data
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Timezone
|
2
|
+
attr_reader :key, :location
|
3
|
+
|
4
|
+
def initialize(location, key)
|
5
|
+
@location = location
|
6
|
+
@key = key
|
7
|
+
end
|
8
|
+
|
9
|
+
def timezone
|
10
|
+
response = open(timezone_query_url) { |f| JSON.parse f.read }
|
11
|
+
format_results response
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def timezone_query_url
|
17
|
+
'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' + key +
|
18
|
+
'&location=' + latitude_longitude + '×tamp=' + timestamp
|
19
|
+
end
|
20
|
+
|
21
|
+
def latitude_longitude
|
22
|
+
lat_lng = Array.new
|
23
|
+
location.each { |k, v| lat_lng.push v.to_s }
|
24
|
+
lat_lng.join(',')
|
25
|
+
end
|
26
|
+
|
27
|
+
def timestamp
|
28
|
+
Time.now.to_i.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_results(response)
|
32
|
+
Hash[
|
33
|
+
timezone_id: response['timeZoneId'],
|
34
|
+
timezone_name: response['timeZoneName']
|
35
|
+
]
|
36
|
+
end
|
37
|
+
end
|
data/lib/locotimezone.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'locotimezone/version'
|
2
|
+
require 'locotimezone/loco_time'
|
3
|
+
require 'locotimezone/location'
|
4
|
+
require 'locotimezone/timezone'
|
5
|
+
|
6
|
+
module Locotimezone
|
7
|
+
|
8
|
+
def self.locotime(options = {})
|
9
|
+
location_only = options.fetch(:location_only, false)
|
10
|
+
timezone_only = options.fetch(:timezone_only, false)
|
11
|
+
location = options.fetch(:location, nil)
|
12
|
+
address = options.fetch(:address, nil)
|
13
|
+
key = options.fetch(:key, ENV['GOOGLE_API_KEY'])
|
14
|
+
|
15
|
+
LocoTime.new(
|
16
|
+
location_only: location_only,
|
17
|
+
timezone_only: timezone_only,
|
18
|
+
location: location,
|
19
|
+
address: address,
|
20
|
+
key: key
|
21
|
+
).transform
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'locotimezone/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "locotimezone"
|
8
|
+
spec.version = Locotimezone::VERSION
|
9
|
+
spec.authors = ["Alex Miller"]
|
10
|
+
spec.email = ["apmiller108@yahoo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Get timezone and gelocation data for a street address.}
|
13
|
+
spec.description = %q{Transform a street address into geoloction and timezone data.}
|
14
|
+
spec.homepage = "https://github.com/apmiller108/locotimezone"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: locotimezone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Transform a street address into geoloction and timezone data.
|
56
|
+
email:
|
57
|
+
- apmiller108@yahoo.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/locotimezone.rb
|
71
|
+
- lib/locotimezone/location.rb
|
72
|
+
- lib/locotimezone/loco_time.rb
|
73
|
+
- lib/locotimezone/timezone.rb
|
74
|
+
- lib/locotimezone/version.rb
|
75
|
+
- locotimezone.gemspec
|
76
|
+
homepage: https://github.com/apmiller108/locotimezone
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Get timezone and gelocation data for a street address.
|
101
|
+
test_files: []
|