telize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d66d599d17bef239d5b9faec14b9088f35897a66
4
+ data.tar.gz: 3dde32a9d8adb5bb41341d9775bf5b13bd74b6e9
5
+ SHA512:
6
+ metadata.gz: d1be8bcb4335c85dc91a850df1dcefef96bff73c3a90b7946e6edf32f7a6c216fe9460883d0443e02461296204993531cc2a857622b433605c315c7e02a899ce
7
+ data.tar.gz: b4dd4177cc8cb497832110160a8218989d2fd6b39c636fbf74a72b808060084e368fc601ffe564b471a6cb63f7663228eb0542d19a85b429809c19c3c89f6ad6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --backtrace
3
+ --order random
4
+ --format progress
5
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in telize.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'webmock', require: 'webmock/rspec'
8
+ gem 'pry'
9
+ gem 'oj'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 undr
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,91 @@
1
+ # Telize
2
+
3
+ A ruby client for Telize geoip service.
4
+
5
+ http://www.telize.com/
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'telize'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install telize
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Configure it if needed
30
+
31
+ ```ruby
32
+ Telize.configure do |config|
33
+ config.host = 'localhost' # Default is www.telize.com
34
+ config.port = 8080 # Default is nil
35
+ config.timeout = nil # Default is 10
36
+ end
37
+ ```
38
+
39
+ and ask the service
40
+
41
+ ```ruby
42
+ Telize.ip
43
+ # => "212.47.239.17"
44
+
45
+ Telize.geoip
46
+
47
+ # => {"dma_code"=>"0",
48
+ # "ip"=>"212.47.239.17",
49
+ # "asn"=>"AS12876",
50
+ # "latitude"=>48.86,
51
+ # "country_code"=>"FR",
52
+ # "offset"=>"2",
53
+ # "country"=>"France",
54
+ # "isp"=>"ONLINE S.A.S.",
55
+ # "timezone"=>"Europe/Paris",
56
+ # "area_code"=>"0",
57
+ # "continent_code"=>"EU",
58
+ # "longitude"=>2.35,
59
+ # "country_code3"=>"FRA"}
60
+
61
+ Telize.geoip('74.125.225.224')
62
+
63
+ # => {"dma_code"=>"0",
64
+ # "ip"=>"74.125.225.224",
65
+ # "asn"=>"AS15169",
66
+ # "city"=>"Mountain View",
67
+ # "latitude"=>37.4192,
68
+ # "country_code"=>"US",
69
+ # "offset"=>"-7",
70
+ # "country"=>"United States",
71
+ # "region_code"=>"CA",
72
+ # "isp"=>"Google Inc.",
73
+ # "timezone"=>"America/Los_Angeles",
74
+ # "area_code"=>"0",
75
+ # "continent_code"=>"NA",
76
+ # "longitude"=>-122.0574,
77
+ # "region"=>"California",
78
+ # "postal_code"=>"94043",
79
+ # "country_code3"=>"USA"}
80
+
81
+ Telize.geoip('74.125.')
82
+ # => {}
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it ( https://github.com/undr/telize/fork )
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,48 @@
1
+ require 'net/http'
2
+
3
+ module Telize
4
+ class Client
5
+ attr_reader :config
6
+
7
+ def initialize(config = Config.new)
8
+ @config = config
9
+ end
10
+
11
+ def ip
12
+ perform('/jsonip')['ip']
13
+ end
14
+
15
+ def geoip(ip_address = nil)
16
+ perform(['', 'geoip', ip_address].compact.join(?/))
17
+ end
18
+
19
+ private
20
+
21
+ def perform(url)
22
+ response = do_request_to(url)
23
+
24
+ case response
25
+ when Net::HTTPSuccess
26
+ decode_body(response.body)
27
+ else
28
+ {}
29
+ end
30
+ rescue Net::OpenTimeout, Net::ReadTimeout, Net::HTTPBadResponse => e
31
+ {}
32
+ end
33
+
34
+ def do_request_to(url)
35
+ Net::HTTP.start(
36
+ config.host,
37
+ config.port,
38
+ open_timeout: config.timeout, read_timeout: config.timeout
39
+ ) { |http| http.request_get(url) }
40
+ end
41
+
42
+ def decode_body(body)
43
+ MultiJson.load(body)
44
+ ensure
45
+ {}
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ module Telize
2
+ class Config
3
+ attr_accessor :host, :port, :timeout
4
+
5
+ def initialize
6
+ @host = 'www.telize.com'
7
+ @port = nil
8
+ @timeout = 10
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Telize
2
+ VERSION = "0.0.1"
3
+ end
data/lib/telize.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'multi_json'
2
+ require 'telize/version'
3
+ require 'telize/config'
4
+ require 'telize/client'
5
+
6
+ module Telize
7
+ extend self
8
+
9
+ def config
10
+ @config ||= Config.new
11
+ end
12
+
13
+ def configure
14
+ yield config
15
+ @client = nil
16
+ end
17
+
18
+ def ip
19
+ client.ip
20
+ end
21
+
22
+ def geoip(ip_address = nil)
23
+ client.geoip(ip_address)
24
+ end
25
+
26
+ private
27
+
28
+ def client
29
+ @client ||= Client.new(config)
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bundler'
4
+ Bundler.require(:default, :test)
5
+
6
+ WebMock.disable_net_connect!
7
+
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.expect_with :rspec do |expectations|
11
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
12
+ end
13
+
14
+ config.mock_with :rspec do |mocks|
15
+ mocks.verify_partial_doubles = true
16
+ end
17
+
18
+ config.disable_monkey_patching!
19
+ config.warnings = true
20
+
21
+ if config.files_to_run.one?
22
+ config.default_formatter = 'doc'
23
+ end
24
+
25
+ config.order = :random
26
+ end
@@ -0,0 +1,124 @@
1
+ RSpec.describe Telize do
2
+ describe '#configure' do
3
+ let(:config) { Telize.config }
4
+
5
+ specify do
6
+ Telize.configure do |c|
7
+ expect(c).to eq(config)
8
+ end
9
+ end
10
+ end
11
+
12
+ describe '#ip' do
13
+ subject { Telize.ip }
14
+
15
+ context 'when everything is ok' do
16
+ let(:response) { '{"ip":"212.47.239.17"}' }
17
+
18
+ before do
19
+ stub_request(:get, 'http://www.telize.com/jsonip').
20
+ to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
21
+ end
22
+
23
+ it { is_expected.to eq('212.47.239.17') }
24
+ end
25
+
26
+ context 'when open timeout is occured' do
27
+ before { stub_request(:get, 'http://www.telize.com/jsonip').to_raise(Net::OpenTimeout) }
28
+
29
+ it { is_expected.to be_nil }
30
+ end
31
+
32
+ context 'when read timeout is occured' do
33
+ before { stub_request(:get, 'http://www.telize.com/jsonip').to_raise(Net::ReadTimeout) }
34
+
35
+ it { is_expected.to be_nil }
36
+ end
37
+
38
+ context 'when bad response is given' do
39
+ before { stub_request(:get, 'http://www.telize.com/jsonip').to_raise(Net::HTTPBadResponse) }
40
+
41
+ it { is_expected.to be_nil }
42
+ end
43
+ end
44
+
45
+ describe '#geoip' do
46
+ context 'when everything is ok' do
47
+ let(:response) { '{"dma_code":"0","ip":"212.47.239.17","asn":"AS12876","latitude":48.86,"country_code":"FR","offset":"2","country":"France","isp":"ONLINE S.A.S.","timezone":"Europe\/Paris","area_code":"0","continent_code":"EU","longitude":2.35,"country_code3":"FRA"}' }
48
+ let(:result) { {
49
+ 'dma_code' => '0',
50
+ 'ip' => '212.47.239.17',
51
+ 'asn' => 'AS12876',
52
+ 'latitude' => 48.86,
53
+ 'country_code' => 'FR',
54
+ 'offset' => '2',
55
+ 'country' => 'France',
56
+ 'isp' => 'ONLINE S.A.S.',
57
+ 'timezone' => 'Europe/Paris',
58
+ 'area_code' => '0',
59
+ 'continent_code' => 'EU',
60
+ 'longitude' => 2.35,
61
+ 'country_code3' => 'FRA'
62
+ } }
63
+
64
+ context 'and nothing was passed as argument' do
65
+ subject { Telize.geoip }
66
+
67
+ before do
68
+ stub_request(:get, 'http://www.telize.com/geoip').
69
+ to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
70
+ end
71
+
72
+ it { is_expected.to eq(result) }
73
+ end
74
+
75
+ context 'and ip address was passed as argument' do
76
+ subject { Telize.geoip('212.47.239.17') }
77
+
78
+ before do
79
+ stub_request(:get, 'http://www.telize.com/geoip/212.47.239.17').
80
+ to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
81
+ end
82
+
83
+ it { is_expected.to eq(result) }
84
+ end
85
+ end
86
+
87
+ context 'when not valid ip address was passed as argument' do
88
+ subject { Telize.geoip('212.47.') }
89
+
90
+ let(:response) { '{"message":"Input string is not a valid IP address","code":401}' }
91
+
92
+ before do
93
+ stub_request(:get, 'http://www.telize.com/geoip/212.47.').
94
+ to_return(status: 400, body: response, headers: { 'Content-Type' => 'application/json' })
95
+ end
96
+
97
+ it { is_expected.to eq({}) }
98
+ end
99
+
100
+ context 'when open timeout is occured' do
101
+ subject { Telize.geoip }
102
+
103
+ before { stub_request(:get, 'http://www.telize.com/geoip').to_raise(Net::OpenTimeout) }
104
+
105
+ it { is_expected.to eq({}) }
106
+ end
107
+
108
+ context 'when read timeout is occured' do
109
+ subject { Telize.geoip }
110
+
111
+ before { stub_request(:get, 'http://www.telize.com/geoip').to_raise(Net::ReadTimeout) }
112
+
113
+ it { is_expected.to eq({}) }
114
+ end
115
+
116
+ context 'when bad response is given' do
117
+ subject { Telize.geoip }
118
+
119
+ before { stub_request(:get, 'http://www.telize.com/geoip').to_raise(Net::HTTPBadResponse) }
120
+
121
+ it { is_expected.to eq({}) }
122
+ end
123
+ end
124
+ end
data/telize.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'telize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'telize'
8
+ spec.version = Telize::VERSION
9
+ spec.authors = ['undr']
10
+ spec.email = ['undr@yandex.ru']
11
+ spec.summary = %q{A ruby client for Telize geoip service.}
12
+ spec.description = %q{A ruby client for Telize geoip service.}
13
+ spec.homepage = ''
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 'multi_json'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - undr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
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
+ - !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
+ description: A ruby client for Telize geoip service.
56
+ email:
57
+ - undr@yandex.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/telize.rb
69
+ - lib/telize/client.rb
70
+ - lib/telize/config.rb
71
+ - lib/telize/version.rb
72
+ - spec/spec_helper.rb
73
+ - spec/telize_spec.rb
74
+ - telize.gemspec
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A ruby client for Telize geoip service.
99
+ test_files:
100
+ - spec/spec_helper.rb
101
+ - spec/telize_spec.rb