ip_info 0.3.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42bc6e3ffc7e7a2d01a8d4cd37d8242c5cfdf7a1
4
+ data.tar.gz: d9f01ec880f73c97e04e8c6f19085c42194f54ea
5
+ SHA512:
6
+ metadata.gz: 32e3730317b72b704ae250707d026448d629fd0bfce4299e16e30ca7e932431013f2e02833550c4ab0d8f19e4070ddf7de9382dd55d07eb295d50027f3b0884b
7
+ data.tar.gz: 2c8e44d0b5c2e3c9f7de520eaf8faff3f9683efeb207eaffdd9bd0b06e8daebb2299f1bd3992656fa0945f94fb7b9c9d520b2d096d525f3f5012b394d8e55a1d
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.1
5
+ script: 'bundle exec rspec'
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ipinfodb.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Maxim Djuliy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # IpInfo Gem
2
+
3
+ [![Build Status](https://travis-ci.org/max-si-m/ipinfo.svg?branch=master)](https://travis-ci.org/max-si-m/ipinfo)
4
+
5
+ Find location from an IP address.
6
+ This's simple gem for [http://ipinfodb.com/](http://ipinfodb.com/)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ipinfo'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ipinfo
23
+
24
+ ## Usage
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
+ ```ruby
28
+ ip_info = IpInfo::API.new()
29
+ ```
30
+ And use `#lookup` for geting information about *ip*:
31
+
32
+ ```ruby
33
+ ip_info.lookup('209.85.227.104', type: 'city', time_zone: true)
34
+ ```
35
+ or *domain*:
36
+
37
+ ```ruby
38
+ ip_info.lookup('devbattles.com', type: 'country')
39
+ ```
40
+
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/ipinfo/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ipinfo.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ip_info/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ip_info"
8
+ spec.version = IpInfo::VERSION
9
+ spec.authors = ["Maxim Djuliy"]
10
+ spec.email = ["mak7.dj@gmail.com"]
11
+ spec.description = %q{API interface for http://ipinfodb.com }
12
+ spec.summary = "Using for getting information about ip"
13
+ spec.homepage = "https://github.com/max-si-m/ipinfodb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty", "~> 0.13.5"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency 'vcr', '~> 2.9', '>= 2.9.3'
26
+ spec.add_development_dependency 'webmock', '~>1.21'
27
+ spec.add_development_dependency 'dotenv', '~>2.0'
28
+ end
data/lib/ip_info.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "httparty"
2
+ require "ipinfo/version"
3
+ require "ipinfo/parser"
4
+ require "ipinfo/request"
5
+ require "ipinfo/api"
6
+
7
+ module IpInfo
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,44 @@
1
+ require "ip_info"
2
+
3
+ module IpInfo
4
+ class API
5
+ include HTTParty
6
+ include IpInfo::API::Request
7
+ include IpInfo::API::Parser
8
+
9
+ class ApiKeyError < ArgumentError; end
10
+
11
+ base_uri "http://api.ipinfodb.com/v3/ip-"
12
+ format :json
13
+
14
+ attr_accessor :api_key
15
+
16
+ # Using for adding adding api_key for requests
17
+ # API_KEY you can get from: http://ipinfodb.com/account.php
18
+ #
19
+ # Api key also can be stored in environment variable ENV['IP_INFO_KEY'].
20
+ #
21
+ # It takes one arguments:
22
+ # * +api_key+: string of api_key
23
+ #
24
+ # ==== Example:
25
+ # ip_info = IpInfo::API.new()
26
+ def initialize()
27
+ self.api_key ||= ENV['IP_INFO_KEY']
28
+ raise ApiKeyError.new("Error! Add your API key") if api_key.nil?
29
+ end
30
+
31
+ # Retreive the remote location of a given ip address.
32
+ #
33
+ # It takes two optional arguments:
34
+ # * +type+: can either be +:country+ (default) or +:city+
35
+ # * +time_zone+: can either be +false+ (default) or +true+
36
+ #
37
+ # ==== Example:
38
+ # ip_info.lookup('209.85.227.104', type: 'city', time_zone: true)
39
+ def lookup(ip, options = {})
40
+ query(ip, options)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ require "json"
2
+
3
+ module IpInfo
4
+ class API
5
+ module Parser
6
+ class InvalidParamsError < ArgumentError; end
7
+
8
+ def parse_response(response)
9
+ result = convert_keys response
10
+ raise InvalidParamsError.new(result[:status_message]) if result[:status_code] == "ERROR"
11
+ result
12
+ end
13
+
14
+ private
15
+ def convert_keys(response)
16
+ raise InvalidParamsError.new("Params must be presence") if response.nil?
17
+ response.inject({}) {|hsh, (key,value)| hsh[key.to_s.gsub(/[A-Z]/){|s| '_' + s.downcase}.gsub(/^_/, '').to_sym] = value; hsh}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module IpInfo
2
+ class API
3
+ module Request
4
+ def query data, options = {}
5
+ ip = data.sub(/^https?\:\/\//, '').sub(/^www./,'')
6
+
7
+ type = (options[:type] == "city") ? "city" : "country"
8
+ time_zone = (options[:time_zone] == true ) ? true : false
9
+
10
+ params = {}
11
+ params[:key] = self.api_key
12
+ params[:ip] = ip
13
+ params[:timezone] = time_zone
14
+ params[:format] = "json"
15
+
16
+ response = self.class.get("#{type}/", query: params)
17
+ parse_response(response.parsed_response)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module IpInfo
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe IpInfo::API do
4
+ let(:ip) { 'devbattles.com' }
5
+ let(:ip_info) { IpInfo::API.new() }
6
+
7
+ it "set api_key", :vcr do
8
+ expect(ip_info.api_key).to eq(ENV["IP_INFO_KEY"])
9
+ end
10
+
11
+ it "raise errors with empty api_key", :vcr do
12
+ expect{IpInfo::API.new(nil)}.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it "check type of request", :vcr do
16
+ expect(ip_info.lookup(ip)).to be_kind_of(Hash)
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ #using IpInfo::API instanse for access to IpInfo::Parser
4
+ describe IpInfo::API::Parser do
5
+ let(:ip_info) { IpInfo::API.new() }
6
+
7
+ let(:response) do
8
+ {
9
+ "statusCode" => "OK",
10
+ "statusMessage" => "",
11
+ "ipAddress" => "64.233.191.255",
12
+ "countryCode" => "US",
13
+ "countryName" => "United States",
14
+ "regionName" => "California",
15
+ "cityName" => "Mountain View",
16
+ "zipCode" => "94043",
17
+ "latitude" => "37.406",
18
+ "longitude" => "-122.079",
19
+ "timeZone" => "-07:00"
20
+ }
21
+ end
22
+
23
+ context "check #parse_response params" do
24
+ it "must return Hash" do
25
+ res = ip_info.parse_response(response)
26
+ expect(res).to be_kind_of(Hash)
27
+ end
28
+
29
+ it "raise error for bad data" do
30
+ response["statusCode"] = "ERROR"
31
+ response["statusMessage"] = "Some errors"
32
+
33
+ expect{ip_info.parse_response(response)}.to raise_error(ArgumentError)
34
+ end
35
+ end
36
+
37
+ context "check #convert_keys params" do
38
+ it "convert array to hash" do
39
+ res = ip_info.send('convert_keys',response)
40
+ expect(res).to be_kind_of(Hash)
41
+ end
42
+ it "raise error for empty response" do
43
+ expect{ip_info.send('convert_keys',nil)}.to raise_error(ArgumentError)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ #using IpInfo::API instanse for access to IpInfo::Request
4
+ describe IpInfo::API::Request do
5
+ let(:address_url) { 'devbattles.com' }
6
+ let(:address_ip) { '64.233.191.255' }
7
+ let(:ip_info) { IpInfo::API.new() }
8
+
9
+ context "check #query params" do
10
+ it "must be one param" do
11
+ expect{ip_info.query()}.to raise_error(ArgumentError)
12
+ end
13
+ it "second param is optioanl", :vcr do
14
+ expect(ip_info.query(address_ip)).to be_kind_of(Hash)
15
+ end
16
+ end
17
+
18
+ context "check #query response", :vcr do
19
+ context "ip address" do
20
+ it "check valid ip address without params" do
21
+ expect(ip_info.query(address_ip)).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"64.233.191.255", :country_code=>"US", :country_name=>"United States"})
22
+ end
23
+
24
+ it "check valid ip address with city params" do
25
+ expect(ip_info.query(address_ip, type:'city')).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"64.233.191.255", :country_code=>"US", :country_name=>"United States", :region_name=>"California", :city_name=>"Mountain View", :zip_code=>"94043", :latitude=>"37.406", :longitude=>"-122.079", :time_zone=>"-07:00"})
26
+ end
27
+
28
+ it "check valid ip address with both params" do
29
+ expect(ip_info.query(address_ip, type:'city', time_zone: true)).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"64.233.191.255", :country_code=>"US", :country_name=>"United States", :region_name=>"California", :city_name=>"Mountain View", :zip_code=>"94043", :latitude=>"37.406", :longitude=>"-122.079", :time_zone=>"-07:00"})
30
+ end
31
+ end
32
+ context "url address" do
33
+ it "check valid url address without params" do
34
+ expect(ip_info.query(address_url)).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"104.236.167.162", :country_code=>"US", :country_name=>"United States"})
35
+ end
36
+
37
+ it "check valid url address with city params" do
38
+ expect(ip_info.query(address_url, type:'city')).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"104.236.167.162", :country_code=>"US", :country_name=>"United States", :region_name=>"California", :city_name=>"San Francisco", :zip_code=>"94102", :latitude=>"37.7749", :longitude=>"-122.419", :time_zone=>"-07:00"})
39
+ end
40
+
41
+ it "check valid url address with both params" do
42
+ expect(ip_info.query(address_url, type:'city', time_zone: true)).to eq({:status_code=>"OK", :status_message=>"", :ip_address=>"104.236.167.162", :country_code=>"US", :country_name=>"United States", :region_name=>"California", :city_name=>"San Francisco", :zip_code=>"94102", :latitude=>"37.7749", :longitude=>"-122.419", :time_zone=>"-07:00"})
43
+ end
44
+ end
45
+ context "invalid response" do
46
+ it "ip address" do
47
+ expect{ip_info.query("invalid ip")}.to raise_error(ArgumentError)
48
+ end
49
+ it "url address" do
50
+ expect{ip_info.query("invalid url")}.to raise_error(ArgumentError)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/ip_info' # load gem
2
+
3
+ require 'dotenv'
4
+ require 'vcr'
5
+ require 'support/vcr'
6
+
7
+ Dotenv.load
@@ -0,0 +1,12 @@
1
+ VCR.configure do |config|
2
+ config.cassette_library_dir = 'spec/cassettes'
3
+ config.hook_into :webmock
4
+ config.filter_sensitive_data('api_key') { ENV["IP_INFO_KEY"] }
5
+ end
6
+
7
+ RSpec.configure do |c|
8
+ c.around(:each, :vcr) do |example|
9
+ name = example.metadata[:full_description].split(/\s+/, 2).join("/").downcase.gsub(/\s+/,"_")
10
+ VCR.use_cassette(name) { example.call }
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip_info
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Djuliy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.9.3
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '2.9'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.9.3
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.21'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.21'
89
+ - !ruby/object:Gem::Dependency
90
+ name: dotenv
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ description: 'API interface for http://ipinfodb.com '
104
+ email:
105
+ - mak7.dj@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".travis.yml"
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - ipinfo.gemspec
118
+ - lib/ip_info.rb
119
+ - lib/ip_info/api.rb
120
+ - lib/ip_info/parser.rb
121
+ - lib/ip_info/request.rb
122
+ - lib/ip_info/version.rb
123
+ - spec/ip_info/api_spec.rb
124
+ - spec/ip_info/parser_spec.rb
125
+ - spec/ip_info/request_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/vcr.rb
128
+ homepage: https://github.com/max-si-m/ipinfodb
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Using for getting information about ip
152
+ test_files:
153
+ - spec/ip_info/api_spec.rb
154
+ - spec/ip_info/parser_spec.rb
155
+ - spec/ip_info/request_spec.rb
156
+ - spec/spec_helper.rb
157
+ - spec/support/vcr.rb