simple_geolocator 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/lib/simple_geolocator.rb +81 -58
  4. metadata +10 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 735ce151f51274d7b644bbdb54cd11e56a42877e
4
- data.tar.gz: 4e6f837aa611e4c49cd00d2565830b1e7c631cb7
3
+ metadata.gz: 162ba0a2fb8e881b8bf09679250b3afdf4f4d13b
4
+ data.tar.gz: b18874e63807a865eb41283fad44cce504c2980a
5
5
  SHA512:
6
- metadata.gz: 440b2b06c50c04d14550c6e424e54eaf4db98d38a61259dd8e7c745920536f78fc7e693b6e5820e3744c06d6e54f7fb12704a8cc2e3fdc113f45f60112fa8698
7
- data.tar.gz: b1bf2d26356ea53703dac86fe7334d436905aeb9433b350f0aba1123b3274ed526ec4e497622cd4f1cc88a24f318bdbcf78f0ab3656be7ed85fa0b4c5f748e03
6
+ metadata.gz: 2a68ab25452b990e7be28a1cb7bae870d370922e43eab33391a677c3e3750ce1ca41f569aa506efd269985e5e18b6de01eb855e93a60376b27f61861d57102fb
7
+ data.tar.gz: dba071b5f272ef33cfcf928fa8003afc7773bb037a44444be9b1860faef8791fee119c343533216c56a4bd4b7183852266a06c1ce24f8711263ca5bbc8141763
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
1
  # Changelog
2
2
  ## Version 1
3
+ ### Version 1.1.0
4
+ * Fix error method causing NoMethodError.
5
+ * Better documentation for all methods, including adding the missing documentation for the private error method.
6
+ * Case/When statement instead of if conditional in request_successful?
7
+ * Better code in all the getter methods, improving how values are returned and how error is called.
8
+ * New get_error_description for getting the description to go with the ambiguous error message.
9
+ * Better error code, which is now dependent on request_successful?, instead of having getter methods being dependent on request_successful?. This greatly reduces the length of the methods.
10
+ * HTTPClient dependency is no longer open-eneded.
11
+
3
12
  ### Version 1.0.0
4
13
  * Initial release version. Contains methods for most everything available with IP-API.com. The only API that I am not utilizing is the DNS API. That should come in a future release.
@@ -3,6 +3,7 @@ require 'json'
3
3
 
4
4
  module SimpleGeolocator
5
5
  extend self
6
+
6
7
  @client = HTTPClient.new
7
8
 
8
9
  # Gets the full JSON response, useful for getting multiple pieces of data in
@@ -13,18 +14,19 @@ module SimpleGeolocator
13
14
  url = "http://ip-api.com/json/#{ip}"
14
15
  uri = URI.parse(url)
15
16
  response = @client.get(uri)
16
- return JSON.parse(response.body)
17
+ JSON.parse(response.body)
17
18
  end
18
19
 
19
20
  # Gets whether the request failed or not.
20
- # @param response [String] The response body (gotten by #get_full_response)
21
- # to check.
21
+ # @param response [JSON] The parsed response body (#get_full_response) to
22
+ # check.
22
23
  # @return [Boolean] True if successful, false if errored.
23
24
  def request_successful?(response)
24
- if response['status'] == 'success'
25
- return true
26
- else
27
- return false
25
+ case response['status']
26
+ when 'success'
27
+ true
28
+ when 'fail'
29
+ false
28
30
  end
29
31
  end
30
32
 
@@ -32,115 +34,136 @@ module SimpleGeolocator
32
34
  # @param ip [String] See #get_full_response
33
35
  # @return [Hash] A hash containing data formatted as
34
36
  # { :name => 'United States', :code => 'US' }
37
+ # @return [String] A string containing the error message.
35
38
  def country(ip)
36
39
  response = get_full_response(ip)
37
- if request_successful?(response)
38
- ret = {
39
- :name => response['country'],
40
- :code => response['countryCode']
41
- }
42
- return ret
43
- else
44
- return error(response)
45
- end
40
+ err = error(response)
41
+ return err unless err.nil?
42
+ ret = {
43
+ name: response['country'],
44
+ code: response['countryCode']
45
+ }
46
+ ret
46
47
  end
47
48
 
48
49
  # Gets the region data for the IP.
49
50
  # @param ip [String] See #get_full_response
50
51
  # @return [Hash] A hash containing data formatted as
51
52
  # { :name => 'Oregon', :code => 'OR'}
53
+ # @return [String] A string containing the error message.
52
54
  def region(ip)
53
55
  response = get_full_response(ip)
54
- if request_successful?(response)
55
- ret = {
56
- :name => response['regionName'],
57
- :code => response['region']
58
- }
59
- return ret
60
- else
61
- return error(response)
62
- end
56
+ err = error(response)
57
+ return err unless err.nil?
58
+ ret = {
59
+ name: response['regionName'],
60
+ code: response['region']
61
+ }
62
+ ret
63
63
  end
64
64
 
65
65
  # Gets the city name for the IP.
66
66
  # @param ip [String] See #get_full_response
67
67
  # @return [String] The name of the city that the IP is located in.
68
+ # @return [String] A string containing the error message.
68
69
  def city(ip)
69
70
  response = get_full_response(ip)
70
- if request_successful?(response)
71
- return response['city']
72
- else
73
- return error(response)
74
- end
71
+ err = error(response)
72
+ return err unless err.nil?
73
+
74
+ response['city']
75
75
  end
76
76
 
77
77
  # Gets the zip code for the IP.
78
78
  # @param ip [String] See #get_full_response
79
79
  # @return [Int] The zip code that the IP is located in.
80
+ # @return [String] A string containing the error message.
80
81
  def zip(ip)
81
82
  response = get_full_response(ip)
82
- if request_successful?(response)
83
- return response['zip'].to_i
84
- else
85
- return error(response)
86
- end
83
+ err = error(response)
84
+ return err unless err.nil?
85
+
86
+ response['zip'].to_i
87
87
  end
88
88
 
89
89
  # Gets the latitude, longitude for the IP.
90
90
  # @param ip [String] See #get_full_response
91
91
  # @return [Array] An array of Floats formatted as lat, lon
92
+ # @return [String] A string containing the error message.
92
93
  def ll(ip)
93
94
  response = get_full_response(ip)
94
- if request_successful?(response)
95
- ret = [
96
- response['lat'],
97
- response['lon']
98
- ]
99
- return ret
100
- else
101
- return error(response)
102
- end
95
+ err = error(response)
96
+ return err unless err.nil?
97
+
98
+ ret = [response['lat'], response['lon']]
99
+ ret
103
100
  end
104
101
 
105
102
  # Gets the timezone for the IP.
106
103
  # @param ip [String] See #get_full_response
107
104
  # @return [String] The timezone (UTC, PST, etc.) that the IP is in.
105
+ # @return [String] A string containing the error message.
108
106
  def timezone(ip)
109
107
  response = get_full_response(ip)
110
- if request_successful?(response)
111
- return response['timezone']
112
- else
113
- return error(response)
114
- end
108
+ err = error(response)
109
+ return err unless err.nil?
110
+
111
+ response['timezone']
115
112
  end
116
113
 
117
114
  # Gets the name of the IP's Internet Service Provider.
118
115
  # @param ip [String] See #get_full_response
119
116
  # @return [String] The ISP name, such as Comcast Cable.
117
+ # @return [String] A string containing the error message.
120
118
  def isp_name(ip)
121
119
  response = get_full_response(ip)
122
- if request_successful?(response)
123
- return response['isp']
124
- else
125
- return error(response)
126
- end
120
+ err = error(response)
121
+ return err unless err.nil?
122
+
123
+ response['isp']
127
124
  end
128
125
 
129
126
  # Gets the name of the IP's organization. For most people, this is identical
130
127
  # to their ISP name.
131
128
  # @param ip [String] See #get_full_response
132
129
  # @return [String] The organization name, such as Google.
130
+ # @return [String] A string containing the error message.
133
131
  def organization_name(ip)
134
132
  response = get_full_response(ip)
135
- if request_successful?(response)
136
- return response['org']
133
+ err = error(response)
134
+ return err unless err.nil?
135
+
136
+ response['org']
137
+ end
138
+
139
+ # Gets the according description for the semi-ambiguous error returned by the
140
+ # API.
141
+ # @param error [String] The error message returned by #error
142
+ # @return [String] The error description.
143
+ # @return [Nil] If you provided an invalid error message.
144
+ def get_error_description(error)
145
+ case error
146
+ when 'private range'
147
+ return 'The IP address is part of a private range.'
148
+ when 'reserved range'
149
+ return 'The IP address is part of a reserved range.'
150
+ when 'invalid query'
151
+ return 'The IP address or domain name is invalid.'
152
+ when 'quota'
153
+ return 'You have reached the quota.'
137
154
  else
138
- return error(response)
155
+ return nil
139
156
  end
140
157
  end
141
158
 
142
159
  private
160
+
161
+ # Gets the error message from a response.
162
+ # @param response [JSON] See #request_successful?
163
+ # @return [String] The error message.
164
+ # @return [Nil] If there was no error message to begin with.
143
165
  def error(response)
144
- return response['message']
166
+ return response['message'] unless request_successful?(response)
167
+ nil
145
168
  end
146
169
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_geolocator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
- version: '0'
22
+ version: 2.6.0.1
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.6'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
- version: '0'
32
+ version: 2.6.0.1
27
33
  description: Accessing the IP API through HTTPClient. I found that many, if not all,
28
34
  Geolocation gems were very annoying and overly-complex to use. Thus, this gem was
29
35
  born. It does not use anything like Google or Yahoo! Geolocation because I have