ip_info 0.3.2 → 0.4.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 +2 -3
- data/lib/ip_info/api.rb +8 -7
- data/lib/ip_info/parser.rb +15 -7
- data/lib/ip_info/request.rb +16 -8
- data/lib/ip_info/version.rb +1 -1
- data/spec/ip_info/api_spec.rb +4 -4
- data/spec/ip_info/parser_spec.rb +1 -1
- data/spec/ip_info/request_spec.rb +1 -1
- 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: 5f526a57b67936ef68324a637a79510a5b908d8e
|
|
4
|
+
data.tar.gz: 97047dbf56a678255d482f53bb3488a06358f9d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8a9d9218022eef92084e724318c5780085955ff87bc4c34a0447bca8cb54c1ac38314e0447fe8dc04dcee71dc690b25f22e8d96e1b52894231bcf246b11c318e
|
|
7
|
+
data.tar.gz: 849569ac1a4b0bd33786b72ae70f84e50ec23252027e770335b8aff6d30848d9a3cfdbe2a63147b4384060544546e9ac9c2313d1b3114621c71e5178e1cb1211
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# IpInfo Gem
|
|
2
2
|
|
|
3
|
-
[](https://travis-ci.org/max-si-m/ip_info)
|
|
4
4
|
|
|
5
5
|
Find location from an IP address.
|
|
6
6
|
This's simple gem for [http://ipinfodb.com/](http://ipinfodb.com/)
|
|
@@ -23,9 +23,8 @@ Or install it yourself as:
|
|
|
23
23
|
|
|
24
24
|
## Usage
|
|
25
25
|
Use your api key for site, you can get it [here](http://ipinfodb.com/account.php)
|
|
26
|
-
Api key also can be stored in environment variable `ENV['IP_INFO_KEY']`.
|
|
27
26
|
```ruby
|
|
28
|
-
ip_info = IpInfo::API.new()
|
|
27
|
+
ip_info = IpInfo::API.new(api_key: "<YOUR_API_KEY>")
|
|
29
28
|
```
|
|
30
29
|
And use `#lookup` for geting information about *ip*:
|
|
31
30
|
|
data/lib/ip_info/api.rb
CHANGED
|
@@ -12,22 +12,23 @@ module IpInfo
|
|
|
12
12
|
format :json
|
|
13
13
|
|
|
14
14
|
attr_accessor :api_key
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
# Using for adding adding api_key for requests
|
|
17
17
|
# API_KEY you can get from: http://ipinfodb.com/account.php
|
|
18
18
|
#
|
|
19
|
-
#
|
|
19
|
+
# Constructor yields a block to allow more complicated object configuration.
|
|
20
20
|
#
|
|
21
|
-
# It takes
|
|
21
|
+
# It takes options with single optional key:
|
|
22
22
|
# * +api_key+: string of api_key
|
|
23
23
|
#
|
|
24
24
|
# ==== Example:
|
|
25
25
|
# ip_info = IpInfo::API.new()
|
|
26
|
-
def initialize()
|
|
27
|
-
self
|
|
28
|
-
|
|
26
|
+
def initialize(options = {})
|
|
27
|
+
yield self if block_given?
|
|
28
|
+
|
|
29
|
+
self.api_key ||= options.fetch(:api_key) { fail ApiKeyError.new("API key is missing.") }
|
|
29
30
|
end
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
# Retreive the remote location of a given ip address.
|
|
32
33
|
#
|
|
33
34
|
# It takes two optional arguments:
|
data/lib/ip_info/parser.rb
CHANGED
|
@@ -5,17 +5,25 @@ module IpInfo
|
|
|
5
5
|
module Parser
|
|
6
6
|
class InvalidParamsError < ArgumentError; end
|
|
7
7
|
|
|
8
|
-
def parse_response(response)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
def parse_response(response)
|
|
9
|
+
convert_keys(response).tap do |result|
|
|
10
|
+
raise InvalidParamsError.new(result[:status_message]) if result[:status_code] == "ERROR"
|
|
11
|
+
end
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
private
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
|
|
16
|
+
def convert_keys(response)
|
|
17
|
+
raise InvalidParamsError.new("Params must be present") if response.nil?
|
|
18
|
+
|
|
19
|
+
response.each_with_object({}) do |(key, value), hsh|
|
|
20
|
+
hsh[to_snake_case(key)] = value
|
|
18
21
|
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def to_snake_case(key)
|
|
25
|
+
key.to_s.gsub(/[A-Z]/) { |s| '_' + s.downcase }.gsub(/^_/, '').to_sym
|
|
26
|
+
end
|
|
19
27
|
end
|
|
20
28
|
end
|
|
21
29
|
end
|
data/lib/ip_info/request.rb
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
module IpInfo
|
|
2
2
|
class API
|
|
3
3
|
module Request
|
|
4
|
-
|
|
4
|
+
REQUEST_TYPES = %w{city country}
|
|
5
|
+
|
|
6
|
+
def query(data, options = {})
|
|
5
7
|
ip = data.sub(/^https?\:\/\//, '').sub(/^www./,'')
|
|
6
8
|
|
|
7
|
-
type = (
|
|
8
|
-
|
|
9
|
+
type = options.fetch(:type, 'country')
|
|
10
|
+
|
|
11
|
+
unless REQUEST_TYPES.include?(type)
|
|
12
|
+
raise ArgumentError.new("Wrong request type (available: #{REQUEST_TYPES.join(",")})")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
time_zone = options.fetch(:time_zone, false)
|
|
9
16
|
|
|
10
|
-
params = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
params = {
|
|
18
|
+
key: api_key,
|
|
19
|
+
ip: ip,
|
|
20
|
+
timezone: time_zone,
|
|
21
|
+
format: "json"
|
|
22
|
+
}
|
|
15
23
|
|
|
16
24
|
response = self.class.get("#{type}/", query: params)
|
|
17
25
|
parse_response(response.parsed_response)
|
data/lib/ip_info/version.rb
CHANGED
data/spec/ip_info/api_spec.rb
CHANGED
|
@@ -2,17 +2,17 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe IpInfo::API do
|
|
4
4
|
let(:ip) { 'devbattles.com' }
|
|
5
|
-
let(:ip_info) { IpInfo::API.new() }
|
|
5
|
+
let(:ip_info) { IpInfo::API.new(api_key: ENV["IP_INFO_KEY"]) }
|
|
6
6
|
|
|
7
7
|
it "set api_key", :vcr do
|
|
8
8
|
expect(ip_info.api_key).to eq(ENV["IP_INFO_KEY"])
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
it "raise errors with empty api_key", :vcr
|
|
12
|
-
expect{IpInfo::API.new(
|
|
11
|
+
it "raise errors with empty api_key", :vcr do
|
|
12
|
+
expect{IpInfo::API.new()}.to raise_error(ArgumentError)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
it "check type of request", :vcr do
|
|
15
|
+
it "check type of request", :vcr do
|
|
16
16
|
expect(ip_info.lookup(ip)).to be_kind_of(Hash)
|
|
17
17
|
end
|
|
18
18
|
end
|
data/spec/ip_info/parser_spec.rb
CHANGED
|
@@ -4,7 +4,7 @@ require 'spec_helper'
|
|
|
4
4
|
describe IpInfo::API::Request do
|
|
5
5
|
let(:address_url) { 'devbattles.com' }
|
|
6
6
|
let(:address_ip) { '64.233.191.255' }
|
|
7
|
-
let(:ip_info) { IpInfo::API.new() }
|
|
7
|
+
let(:ip_info) { IpInfo::API.new(api_key: ENV['IP_INFO_KEY']) }
|
|
8
8
|
|
|
9
9
|
context "check #query params" do
|
|
10
10
|
it "must be one param" do
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ip_info
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Maxim Djuliy
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-07-
|
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: httparty
|