locotimezone 0.2.2 → 0.3.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/README.md +13 -4
- data/lib/locotimezone/location.rb +30 -28
- data/lib/locotimezone/loco_time.rb +39 -42
- data/lib/locotimezone/timezone.rb +47 -43
- data/lib/locotimezone/version.rb +1 -1
- data/lib/locotimezone.rb +4 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c86c7b42877e96caf8e4448343121a107ba9f2f0
|
4
|
+
data.tar.gz: f0a5aa2dafb18f4e66d0255cee959a9e16f2c1e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e55cf04e424a7c6b594ab96f259071dc880274b744999f94cc0de709541583c993d92e99bd79b62d9b642ae4c4c46521bfbb69a9356b2eb097eff6a764752f3
|
7
|
+
data.tar.gz: 5aee7cd402ef6db9d71d01043b01bd6a7e75b3f55bbbf6a54f51d3de764350e6862caecd0e1f4567a0f9beb277100f7a1965d96130c2116502ebcea1aa783733
|
data/README.md
CHANGED
@@ -40,24 +40,25 @@ Locotimezone.locotime address: address
|
|
40
40
|
# {:timezone_id=>"America/New_York", :timezone_name=>"Eastern Daylight Time"}}
|
41
41
|
```
|
42
42
|
|
43
|
-
Not interested in timezone? Skip it.
|
43
|
+
Not interested in timezone? Skip it to just get geocoded results.
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
require 'locotimezone'
|
47
47
|
|
48
|
-
Locotimezone.locotime address: address,
|
48
|
+
Locotimezone.locotime address: address, skip: :timezone
|
49
49
|
# =>
|
50
50
|
# {:geo=>
|
51
51
|
# {:formatted_address=>"525 NW 1st Ave, Fort Lauderdale, FL 33301, USA",
|
52
52
|
# :location=>{:lat=>26.1288238, :lng=>-80.1449743}}}
|
53
53
|
```
|
54
54
|
|
55
|
-
|
55
|
+
If you alredy have latitude and longitude, and you're only interested in getting
|
56
|
+
the timezone data, just pass your own location hash like so:
|
56
57
|
|
57
58
|
```ruby
|
58
59
|
location = { lat: 26.1288238, lng: -80.1449743 }
|
59
60
|
|
60
|
-
Locotimezone.locotime location: location
|
61
|
+
Locotimezone.locotime location: location
|
61
62
|
# =>
|
62
63
|
# {:timezone=>
|
63
64
|
# {:timezone_id=>"America/New_York", :timezone_name=>"Eastern Daylight Time"}}
|
@@ -69,6 +70,14 @@ If the address or location cannot be resolved, an empty hash will be returned.
|
|
69
70
|
Locotimezone.locotime address: '1234 Fake Address'
|
70
71
|
# =>
|
71
72
|
# {:geo=>{}, :timezone=>{}}
|
73
|
+
|
74
|
+
Locotimezone.locotime address: '1234 Fake Address', skip: :timezone
|
75
|
+
# =>
|
76
|
+
# {:geo=>{}}
|
77
|
+
|
78
|
+
Locotimezone.locotime location: { lat: 0, lng: 0 }
|
79
|
+
# =>
|
80
|
+
# {:timezone=>{}}
|
72
81
|
```
|
73
82
|
|
74
83
|
## Options and Setup
|
@@ -1,36 +1,38 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Locotimezone
|
2
|
+
class Location
|
3
|
+
attr_reader :key, :address
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(address, key)
|
6
|
+
@address = address
|
7
|
+
@key = key
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
def geolocate
|
11
|
+
response = open(geolocation_query_url) { |f| JSON.parse f.read }
|
12
|
+
rescue OpenURI::HTTPError
|
13
|
+
{}
|
14
|
+
else
|
15
|
+
format_results response
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
private
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def geolocation_query_url
|
21
|
+
'https://maps.googleapis.com/maps/api/geocode/json' + '?key=' + key +
|
22
|
+
'&address=' + address.to_s
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
def format_results(response)
|
26
|
+
return {} if response['results'].empty?
|
27
|
+
Hash[
|
28
|
+
formatted_address: response['results'][0]['formatted_address'],
|
29
|
+
location: symbolize_keys(response['results'][0]['geometry']['location'])
|
30
|
+
]
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def symbolize_keys(response)
|
34
|
+
response.map { |k,v| [k.to_sym, v] }.to_h
|
35
|
+
end
|
35
36
|
|
37
|
+
end
|
36
38
|
end
|
@@ -1,53 +1,50 @@
|
|
1
1
|
require 'open-uri'
|
2
2
|
require 'json'
|
3
|
-
require 'pry'
|
4
|
-
|
5
|
-
class LocoTime
|
6
|
-
attr_reader :location_only, :timezone_only, :address, :key
|
7
|
-
attr_accessor :location
|
8
|
-
|
9
|
-
def initialize(location_only:, timezone_only:, address:, location:, key:)
|
10
|
-
@location_only = location_only
|
11
|
-
@timezone_only = timezone_only
|
12
|
-
@location = location
|
13
|
-
@address = address
|
14
|
-
@key = key || ''
|
15
|
-
end
|
16
3
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
build_hash(location_data, timezone_data)
|
22
|
-
end
|
4
|
+
module Locotimezone
|
5
|
+
class LocoTime
|
6
|
+
attr_reader :skip, :address, :key
|
7
|
+
attr_accessor :location
|
23
8
|
|
24
|
-
|
9
|
+
def initialize(address:, location:, skip:, key:)
|
10
|
+
@location = location
|
11
|
+
@address = address
|
12
|
+
@skip = location ? :location : skip
|
13
|
+
@key = key || ''
|
14
|
+
end
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
raise ArgumentError,
|
32
|
-
'locotimezone: location is required when timezone_only'
|
16
|
+
def transform
|
17
|
+
validate_options
|
18
|
+
location_data = get_location unless skip == :location
|
19
|
+
timezone_data = get_timezone unless skip == :timezone
|
20
|
+
build_hash(location_data, timezone_data)
|
33
21
|
end
|
34
|
-
end
|
35
22
|
|
36
|
-
|
37
|
-
results = Location.new(address, key).geolocate
|
38
|
-
self.location = results[:location] || {}
|
39
|
-
results
|
40
|
-
end
|
23
|
+
private
|
41
24
|
|
42
|
-
|
43
|
-
|
44
|
-
|
25
|
+
def validate_options
|
26
|
+
if address.nil? && (skip == :timezone || skip.nil?)
|
27
|
+
raise ArgumentError,
|
28
|
+
'locotimezone: missing address or location.'
|
29
|
+
end
|
30
|
+
end
|
45
31
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
32
|
+
def get_location
|
33
|
+
results = Location.new(address, key).geolocate
|
34
|
+
self.location = results[:location] || {}
|
35
|
+
results
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_timezone
|
39
|
+
Timezone.new(location, key).timezone
|
40
|
+
end
|
52
41
|
|
42
|
+
def build_hash(location_data, timezone_data)
|
43
|
+
data = Hash.new
|
44
|
+
data[:geo] = location_data unless location_data.nil?
|
45
|
+
data[:timezone] = timezone_data unless timezone_data.nil?
|
46
|
+
data
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
53
50
|
end
|
@@ -1,45 +1,49 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
1
|
+
module Locotimezone
|
2
|
+
class Timezone
|
3
|
+
attr_reader :key, :location
|
4
|
+
|
5
|
+
def initialize(location, key)
|
6
|
+
@location = location
|
7
|
+
@key = key
|
8
|
+
end
|
9
|
+
|
10
|
+
def timezone
|
11
|
+
return {} if location_invalid?
|
12
|
+
response = open(timezone_query_url) { |f| JSON.parse f.read }
|
13
|
+
rescue OpenURI::HTTPError
|
14
|
+
{}
|
15
|
+
else
|
16
|
+
format_results response
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def location_invalid?
|
22
|
+
return true if !location.respond_to? :has_key?
|
23
|
+
location.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def timezone_query_url
|
27
|
+
'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' + key +
|
28
|
+
'&location=' + latitude_longitude + '×tamp=' + timestamp
|
29
|
+
end
|
30
|
+
|
31
|
+
def latitude_longitude
|
32
|
+
lat_lng = Array.new
|
33
|
+
location.each { |k, v| lat_lng.push v.to_s }
|
34
|
+
lat_lng.join(',')
|
35
|
+
end
|
36
|
+
|
37
|
+
def timestamp
|
38
|
+
Time.now.to_i.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def format_results(response)
|
42
|
+
return {} if response['status'] == 'ZERO_RESULTS'
|
43
|
+
Hash[
|
44
|
+
timezone_id: response['timeZoneId'],
|
45
|
+
timezone_name: response['timeZoneName']
|
46
|
+
]
|
47
|
+
end
|
44
48
|
end
|
45
49
|
end
|
data/lib/locotimezone/version.rb
CHANGED
data/lib/locotimezone.rb
CHANGED
@@ -6,18 +6,11 @@ require 'locotimezone/timezone'
|
|
6
6
|
module Locotimezone
|
7
7
|
|
8
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
9
|
LocoTime.new(
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
key: key
|
10
|
+
location: options.fetch(:location, nil),
|
11
|
+
address: options.fetch(:address, nil),
|
12
|
+
skip: options.fetch(:skip, nil),
|
13
|
+
key: options.fetch(:key, ENV['GOOGLE_API_KEY'])
|
21
14
|
).transform
|
22
15
|
end
|
23
16
|
|
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: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Miller
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|