simple_geolocator 1.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +4 -0
  3. data/lib/simple_geolocator.rb +146 -0
  4. metadata +64 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 735ce151f51274d7b644bbdb54cd11e56a42877e
4
+ data.tar.gz: 4e6f837aa611e4c49cd00d2565830b1e7c631cb7
5
+ SHA512:
6
+ metadata.gz: 440b2b06c50c04d14550c6e424e54eaf4db98d38a61259dd8e7c745920536f78fc7e693b6e5820e3744c06d6e54f7fb12704a8cc2e3fdc113f45f60112fa8698
7
+ data.tar.gz: b1bf2d26356ea53703dac86fe7334d436905aeb9433b350f0aba1123b3274ed526ec4e497622cd4f1cc88a24f318bdbcf78f0ab3656be7ed85fa0b4c5f748e03
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+ ## Version 1
3
+ ### Version 1.0.0
4
+ * 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.
@@ -0,0 +1,146 @@
1
+ require 'httpclient'
2
+ require 'json'
3
+
4
+ module SimpleGeolocator
5
+ extend self
6
+ @client = HTTPClient.new
7
+
8
+ # Gets the full JSON response, useful for getting multiple pieces of data in
9
+ # a single request.
10
+ # @param ip [String] The IP to get data for.
11
+ # @return [JSON] A parsed JSON object containing the response.
12
+ def get_full_response(ip)
13
+ url = "http://ip-api.com/json/#{ip}"
14
+ uri = URI.parse(url)
15
+ response = @client.get(uri)
16
+ return JSON.parse(response.body)
17
+ end
18
+
19
+ # Gets whether the request failed or not.
20
+ # @param response [String] The response body (gotten by #get_full_response)
21
+ # to check.
22
+ # @return [Boolean] True if successful, false if errored.
23
+ def request_successful?(response)
24
+ if response['status'] == 'success'
25
+ return true
26
+ else
27
+ return false
28
+ end
29
+ end
30
+
31
+ # Gets the country data for the IP.
32
+ # @param ip [String] See #get_full_response
33
+ # @return [Hash] A hash containing data formatted as
34
+ # { :name => 'United States', :code => 'US' }
35
+ def country(ip)
36
+ 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
46
+ end
47
+
48
+ # Gets the region data for the IP.
49
+ # @param ip [String] See #get_full_response
50
+ # @return [Hash] A hash containing data formatted as
51
+ # { :name => 'Oregon', :code => 'OR'}
52
+ def region(ip)
53
+ 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
63
+ end
64
+
65
+ # Gets the city name for the IP.
66
+ # @param ip [String] See #get_full_response
67
+ # @return [String] The name of the city that the IP is located in.
68
+ def city(ip)
69
+ response = get_full_response(ip)
70
+ if request_successful?(response)
71
+ return response['city']
72
+ else
73
+ return error(response)
74
+ end
75
+ end
76
+
77
+ # Gets the zip code for the IP.
78
+ # @param ip [String] See #get_full_response
79
+ # @return [Int] The zip code that the IP is located in.
80
+ def zip(ip)
81
+ response = get_full_response(ip)
82
+ if request_successful?(response)
83
+ return response['zip'].to_i
84
+ else
85
+ return error(response)
86
+ end
87
+ end
88
+
89
+ # Gets the latitude, longitude for the IP.
90
+ # @param ip [String] See #get_full_response
91
+ # @return [Array] An array of Floats formatted as lat, lon
92
+ def ll(ip)
93
+ 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
103
+ end
104
+
105
+ # Gets the timezone for the IP.
106
+ # @param ip [String] See #get_full_response
107
+ # @return [String] The timezone (UTC, PST, etc.) that the IP is in.
108
+ def timezone(ip)
109
+ response = get_full_response(ip)
110
+ if request_successful?(response)
111
+ return response['timezone']
112
+ else
113
+ return error(response)
114
+ end
115
+ end
116
+
117
+ # Gets the name of the IP's Internet Service Provider.
118
+ # @param ip [String] See #get_full_response
119
+ # @return [String] The ISP name, such as Comcast Cable.
120
+ def isp_name(ip)
121
+ response = get_full_response(ip)
122
+ if request_successful?(response)
123
+ return response['isp']
124
+ else
125
+ return error(response)
126
+ end
127
+ end
128
+
129
+ # Gets the name of the IP's organization. For most people, this is identical
130
+ # to their ISP name.
131
+ # @param ip [String] See #get_full_response
132
+ # @return [String] The organization name, such as Google.
133
+ def organization_name(ip)
134
+ response = get_full_response(ip)
135
+ if request_successful?(response)
136
+ return response['org']
137
+ else
138
+ return error(response)
139
+ end
140
+ end
141
+
142
+ private
143
+ def error(response)
144
+ return response['message']
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_geolocator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Accessing the IP API through HTTPClient. I found that many, if not all,
28
+ Geolocation gems were very annoying and overly-complex to use. Thus, this gem was
29
+ born. It does not use anything like Google or Yahoo! Geolocation because I have
30
+ found that those APIs are unpredictable and often-times broken. This Gem has been
31
+ made to be as simple to use as possible.
32
+ email: elifosterwy@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - CHANGELOG.md
38
+ - lib/simple_geolocator.rb
39
+ homepage: https://github.com/elifoster/simple_geolocator
40
+ licenses:
41
+ - CC-BY-NC-ND-4.0
42
+ metadata:
43
+ issue_tracker: https://github.com/elifoster/simple_geolocator/issues
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.5.1
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A Ruby gem for easily using the IP-API.com API to perform IP geolocation.
64
+ test_files: []