locotimezone 0.1.2 → 0.2.2

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: 28cb02568726beb3990d3f2db77c6798eeda2a50
4
- data.tar.gz: ad80389e97773612905352c1eb75a25f359698f1
3
+ metadata.gz: b87b061e997bc3ae7a367d1765c501b30422075a
4
+ data.tar.gz: a47d4a027ab9e915d07be5c4e5a23ebd1e50bf3b
5
5
  SHA512:
6
- metadata.gz: c210abb22926a9ce7a3e05aaa632d78455a29529e60c89cf6a62de0b8b19902cfc116af9b94ea0b86f5d4ac7d3969114bc30d94b012d0dfa42fb8cdf6914bc75
7
- data.tar.gz: 3aa7871ebb8951d63df6642c043e85bae40e82f58fc30c8d712141a7e3f5c54ebc6d2742709291a447f9cfb11f9f07b4dd4d444c9603ea470cde42f6a67bda4f
6
+ metadata.gz: 475227bb475696f5964b56ff35fc93ed7e7b27666563064ca209c3dca90734c185f44fa5f6b1a42e4da69ee5a9cf24372b4423ef1a8d27fdd3786c0534ebda87
7
+ data.tar.gz: 890d77907597f2a0275f2f8f92a6a5df1864fe8e3d0fe7ea3554b64974b0d026e6662638f9296c0e0c08c22bd3c843eb56e292ae4c639952e4da031e2771344e
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Code Triagers Badge](https://www.codetriage.com/apmiller108/locotimezone/badges/users.svg)](https://www.codetriage.com/apmiller108/locotimezone)
2
+
1
3
  # Locotimezone
2
4
 
3
5
  Transform a street address into geoloction and timezone data. Essentially, this
@@ -60,6 +62,15 @@ Locotimezone.locotime location: location, timezone_only: true
60
62
  # {:timezone=>
61
63
  # {:timezone_id=>"America/New_York", :timezone_name=>"Eastern Daylight Time"}}
62
64
  ```
65
+
66
+ If the address or location cannot be resolved, an empty hash will be returned.
67
+
68
+ ```ruby
69
+ Locotimezone.locotime address: '1234 Fake Address'
70
+ # =>
71
+ # {:geo=>{}, :timezone=>{}}
72
+ ```
73
+
63
74
  ## Options and Setup
64
75
 
65
76
  `Locotimezone.locotime` can take the following option hash keys:
@@ -8,6 +8,9 @@ class Location
8
8
 
9
9
  def geolocate
10
10
  response = open(geolocation_query_url) { |f| JSON.parse f.read }
11
+ rescue OpenURI::HTTPError
12
+ {}
13
+ else
11
14
  format_results response
12
15
  end
13
16
 
@@ -19,6 +22,7 @@ class Location
19
22
  end
20
23
 
21
24
  def format_results(response)
25
+ return {} if response['results'].empty?
22
26
  Hash[
23
27
  formatted_address: response['results'][0]['formatted_address'],
24
28
  location: symbolize_keys(response['results'][0]['geometry']['location'])
@@ -28,4 +32,5 @@ class Location
28
32
  def symbolize_keys(response)
29
33
  response.map { |k,v| [k.to_sym, v] }.to_h
30
34
  end
35
+
31
36
  end
@@ -1,5 +1,6 @@
1
1
  require 'open-uri'
2
2
  require 'json'
3
+ require 'pry'
3
4
 
4
5
  class LocoTime
5
6
  attr_reader :location_only, :timezone_only, :address, :key
@@ -14,7 +15,7 @@ class LocoTime
14
15
  end
15
16
 
16
17
  def transform
17
- return nil if location_only && timezone_only
18
+ validate_options
18
19
  location_data = get_location unless timezone_only
19
20
  timezone_data = get_timezone unless location_only
20
21
  build_hash(location_data, timezone_data)
@@ -22,9 +23,19 @@ class LocoTime
22
23
 
23
24
  private
24
25
 
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'
33
+ end
34
+ end
35
+
25
36
  def get_location
26
37
  results = Location.new(address, key).geolocate
27
- self.location = results[:location]
38
+ self.location = results[:location] || {}
28
39
  results
29
40
  end
30
41
 
@@ -34,7 +45,7 @@ class LocoTime
34
45
 
35
46
  def build_hash(location_data, timezone_data)
36
47
  data = Hash.new
37
- data[:geo] = location_data unless location_data.nil?
48
+ data[:geo] = location_data unless location_data.nil?
38
49
  data[:timezone] = timezone_data unless timezone_data.nil?
39
50
  data
40
51
  end
@@ -7,21 +7,29 @@ class Timezone
7
7
  end
8
8
 
9
9
  def timezone
10
+ return {} if location_invalid?
10
11
  response = open(timezone_query_url) { |f| JSON.parse f.read }
11
- format_results response
12
+ rescue OpenURI::HTTPError
13
+ {}
14
+ else
15
+ format_results response
12
16
  end
13
17
 
14
18
  private
15
19
 
20
+ def location_invalid?
21
+ location.empty? || !location.respond_to?(:has_key?)
22
+ end
23
+
16
24
  def timezone_query_url
17
25
  'https://maps.googleapis.com/maps/api/timezone/json' + '?key=' + key +
18
26
  '&location=' + latitude_longitude + '&timestamp=' + timestamp
19
27
  end
20
28
 
21
29
  def latitude_longitude
22
- lat_lng = Array.new
23
- location.each { |k, v| lat_lng.push v.to_s }
24
- lat_lng.join(',')
30
+ lat_lng = Array.new
31
+ location.each { |k, v| lat_lng.push v.to_s }
32
+ lat_lng.join(',')
25
33
  end
26
34
 
27
35
  def timestamp
@@ -1,3 +1,3 @@
1
1
  module Locotimezone
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.2"
3
3
  end
data/locotimezone.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.description = %q{Transform a street address into geoloction and timezone data.}
14
14
  spec.homepage = "https://github.com/apmiller108/locotimezone"
15
15
  spec.license = "MIT"
16
+ spec.required_ruby_version = '>= 2.2.1'
16
17
 
17
18
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
19
  # delete this section to allow pushing this gem to any host.
@@ -30,4 +31,5 @@ Gem::Specification.new do |spec|
30
31
  spec.add_development_dependency "bundler", "~> 1.11"
31
32
  spec.add_development_dependency "rake", "~> 10.0"
32
33
  spec.add_development_dependency "minitest", "~> 5.0"
34
+ spec.add_development_dependency "pry", "~> 0.10.4"
33
35
  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.1.2
4
+ version: 0.2.2
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-17 00:00:00.000000000 Z
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
55
69
  description: Transform a street address into geoloction and timezone data.
56
70
  email:
57
71
  - apmiller108@yahoo.com
@@ -86,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: '0'
103
+ version: 2.2.1
90
104
  required_rubygems_version: !ruby/object:Gem::Requirement
91
105
  requirements:
92
106
  - - ">="