locotimezone 0.2.2 → 0.3.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: b87b061e997bc3ae7a367d1765c501b30422075a
4
- data.tar.gz: a47d4a027ab9e915d07be5c4e5a23ebd1e50bf3b
3
+ metadata.gz: c86c7b42877e96caf8e4448343121a107ba9f2f0
4
+ data.tar.gz: f0a5aa2dafb18f4e66d0255cee959a9e16f2c1e1
5
5
  SHA512:
6
- metadata.gz: 475227bb475696f5964b56ff35fc93ed7e7b27666563064ca209c3dca90734c185f44fa5f6b1a42e4da69ee5a9cf24372b4423ef1a8d27fdd3786c0534ebda87
7
- data.tar.gz: 890d77907597f2a0275f2f8f92a6a5df1864fe8e3d0fe7ea3554b64974b0d026e6662638f9296c0e0c08c22bd3c843eb56e292ae4c639952e4da031e2771344e
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, location_only: true
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
- Only want timezone? No problem, but you'll need include a location hash.
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, timezone_only: true
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
- class Location
2
- attr_reader :key, :address
1
+ module Locotimezone
2
+ class Location
3
+ attr_reader :key, :address
3
4
 
4
- def initialize(address, key)
5
- @address = address
6
- @key = key
7
- end
5
+ def initialize(address, key)
6
+ @address = address
7
+ @key = key
8
+ end
8
9
 
9
- def geolocate
10
- response = open(geolocation_query_url) { |f| JSON.parse f.read }
11
- rescue OpenURI::HTTPError
12
- {}
13
- else
14
- format_results response
15
- end
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
- private
18
+ private
18
19
 
19
- def geolocation_query_url
20
- 'https://maps.googleapis.com/maps/api/geocode/json' + '?key=' + key +
21
- '&address=' + address
22
- end
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
- 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
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
- def symbolize_keys(response)
33
- response.map { |k,v| [k.to_sym, v] }.to_h
34
- end
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
- def transform
18
- validate_options
19
- location_data = get_location unless timezone_only
20
- timezone_data = get_timezone unless location_only
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
- private
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
- def validate_options
27
- if address.nil? && !timezone_only
28
- raise ArgumentError,
29
- 'locotimezone: address is required unless timezone_only'
30
- elsif location.nil? && timezone_only
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
- def get_location
37
- results = Location.new(address, key).geolocate
38
- self.location = results[:location] || {}
39
- results
40
- end
23
+ private
41
24
 
42
- def get_timezone
43
- Timezone.new(location, key).timezone
44
- end
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
- def build_hash(location_data, timezone_data)
47
- data = Hash.new
48
- data[:geo] = location_data unless location_data.nil?
49
- data[:timezone] = timezone_data unless timezone_data.nil?
50
- data
51
- end
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
- 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
- return {} if location_invalid?
11
- response = open(timezone_query_url) { |f| JSON.parse f.read }
12
- rescue OpenURI::HTTPError
13
- {}
14
- else
15
- format_results response
16
- end
17
-
18
- private
19
-
20
- def location_invalid?
21
- location.empty? || !location.respond_to?(:has_key?)
22
- end
23
-
24
- def timezone_query_url
25
- 'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' + key +
26
- '&location=' + latitude_longitude + '&timestamp=' + timestamp
27
- end
28
-
29
- def latitude_longitude
30
- lat_lng = Array.new
31
- location.each { |k, v| lat_lng.push v.to_s }
32
- lat_lng.join(',')
33
- end
34
-
35
- def timestamp
36
- Time.now.to_i.to_s
37
- end
38
-
39
- def format_results(response)
40
- Hash[
41
- timezone_id: response['timeZoneId'],
42
- timezone_name: response['timeZoneName']
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 + '&timestamp=' + 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
@@ -1,3 +1,3 @@
1
1
  module Locotimezone
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
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
- location_only: location_only,
17
- timezone_only: timezone_only,
18
- location: location,
19
- address: address,
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.2.2
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-23 00:00:00.000000000 Z
11
+ date: 2016-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler