sina_geoip 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43f6a00a13e71ec4b1c3cef10443298448c14269
4
+ data.tar.gz: 04adb8329b0f6a93890f8c2ffe69aa5b699f61e6
5
+ SHA512:
6
+ metadata.gz: 498faaf5950bf134201e4601d8c515a5daf64079996170c9bc90f2fd4f61fc4664df60f246c6af2faab44035c821c968feace7c85ee2ceb6c04a6edc3230a738
7
+ data.tar.gz: 8c4fb48308db1a8a0efa6ddc36c0c8439708498acf0c13e89a62faf534343a9d77edebfea00ba39b025156b0544dbff44ebfd58c48147db14ae671e24340eedb
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sina_geoip.gemspec
4
+ gemspec
5
+ group :development , :test do
6
+ gem 'rspec' , '~>2.5.0'
7
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jdd
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.
@@ -0,0 +1,108 @@
1
+ # SinaGeoip
2
+
3
+ Retreive the geolocation of an IP address based on the [http://int.dpool.sina.com.cn/iplookup/iplookup.php](http://int.dpool.sina.com.cn/iplookup/iplookup.php) webservice.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sina_geoip'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sina_geoip
18
+
19
+ ## Usage
20
+
21
+
22
+
23
+ ### Retrieve geolocation
24
+ SinaGeoIp.geolocation(ip_address)
25
+
26
+ ### Example
27
+
28
+ # 12.130.132.30 = sina.com.cn (CN)
29
+ GeoIp.geolocation('12.130.132.30')
30
+
31
+ returns:
32
+
33
+ {
34
+ :ret => 1,
35
+ :start => "202.108.24.0",
36
+ :end => "202.108.58.255",
37
+ :country => "中国",
38
+ :province => "北京",
39
+ :city => "北京",
40
+ :district => "",
41
+ :isp => "联通",
42
+ :type => "",
43
+ :desc => ""
44
+ }
45
+
46
+
47
+ ### precision
48
+
49
+ There is an option to only retreive the country information and thus excluding the city details. This results in a faster response from the service since less queries need to be done.
50
+
51
+ #### precision country
52
+
53
+ GeoIp.geolocation('202.108.24.0', :precision => :country)
54
+
55
+ returns:
56
+
57
+ {
58
+ :country => "中国"
59
+ }
60
+
61
+ #### precision province
62
+
63
+ GeoIp.geolocation('202.108.24.0', :precision => :province)
64
+
65
+ returns:
66
+
67
+ {
68
+ :country => "中国",
69
+ :province => "北京"
70
+ }
71
+
72
+ #### precision city
73
+
74
+ GeoIp.geolocation('202.108.24.0', :precision => :city)
75
+
76
+ returns:
77
+
78
+ {
79
+ :country => "中国",
80
+ :province => "北京",
81
+ :city => "北京"
82
+
83
+ }
84
+
85
+ ## Test
86
+
87
+ rspec spec/sina_geoip/sina_geoip_spec.rb
88
+
89
+ ## Contributors
90
+
91
+ * [geo_ip](https://github.com/jeroenj/geo_ip)
92
+
93
+ ## Bugs
94
+
95
+ Please report them on the [Github issue tracker](https://github.com/pobing/sina_geoip/issues)
96
+ for this project.
97
+
98
+ If you have a bug to report, please include the following information:
99
+
100
+ * **Version information for bierdopje, Rails and Ruby.**
101
+ * Stack trace and error message.
102
+
103
+ You may also fork this project on Github and create a pull request.
104
+ Do not forget to include tests.
105
+
106
+ ## Copyright
107
+
108
+ Copyright (c) 2013 by pobing.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,81 @@
1
+ #encoding: utf-8
2
+ require "sina_geoip/version"
3
+ require 'json'
4
+ require 'rest-client'
5
+
6
+ class SinaGeoIp
7
+ SERVICE_URL = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php'
8
+ IPV4_REGEXP = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
9
+
10
+ @@timeout = 1
11
+ @@fallback_timeout = 3
12
+
13
+ class << self
14
+
15
+
16
+ def timeout
17
+ @@timeout
18
+ end
19
+
20
+ def timeout= timeout
21
+ @@timeout = timeout
22
+ end
23
+
24
+ def fallback_timeout
25
+ @@fallback_timeout
26
+ end
27
+
28
+ def fallback_timeout= fallback_timeout
29
+ @@fallback_timeout = fallback_timeout
30
+ end
31
+
32
+ def set_defaults_if_necessary options
33
+ args = [:country, :province,:city]
34
+ if options[:precision] && !args.include?(options[:precision])
35
+ raise 'Invalid precision.'
36
+ end
37
+ end
38
+
39
+ def lookup_url(ip, options = {})
40
+ set_defaults_if_necessary options
41
+ raise 'Invalid IP address' unless ip.to_s =~ IPV4_REGEXP
42
+ #http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=202.102.154.3
43
+ "#{SERVICE_URL}?format=json&ip=#{ip}"
44
+ end
45
+
46
+ # Retreive the remote location of a given ip address.
47
+ #
48
+ # It takes two optional arguments:
49
+ #
50
+ # ==== Example:
51
+ # GeoIp.geolocation('209.85.227.104', {:precision => :city})
52
+ def geolocation(ip, options={})
53
+ location = nil
54
+ Timeout.timeout(self.fallback_timeout) do
55
+ parsed_response = JSON.parse RestClient::Request.execute(:method => :get, :url => lookup_url(ip, options), :timeout => self.timeout)
56
+ location = to_j(parsed_response, options)
57
+ end
58
+ location
59
+ end
60
+
61
+ private
62
+
63
+ #hash => {"ret"=>1, "start"=>"202.102.151.208", "end"=>"202.102.154.255", "country"=>"中国", "province"=>"山东", "city"=>"济南", "district"=>"", "isp"=>"联通", "type"=>"", "desc"=>""}
64
+ def to_j(hash, options={})
65
+ h = {ret: hash['ret'], start: hash['start'], end: hash['end'], country: hash['country'], province: hash['province'], city: hash['city'],\
66
+ district: hash['district'], isp: hash['isp'], type: hash['type'], desc: hash['desc']}
67
+ case options[:precision]
68
+ when :country
69
+ h = {country: hash['country']} # only return country
70
+ when :province
71
+ h = {country: hash['country'], province: hash['province']} # only country,province
72
+ when :city
73
+ h = {country: hash['country'], province: hash['province'],city: hash['city']} # only country,province, city
74
+ else
75
+ h # return all
76
+ end
77
+ h
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,3 @@
1
+ module SinaGeoip
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sina_geoip/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sina_geoip"
8
+ spec.version = SinaGeoip::VERSION
9
+ spec.authors = ["pobing"]
10
+ spec.email = ["cn.jdong@gmail.com"]
11
+ spec.description = %q{Retreive the geolocation of an IP address based on the http://int.dpool.sina.com.cn/iplookup/iplookup.php webservice}
12
+ spec.summary = %q{A call to the sina geoip app . will be done to retreive the geolocation based on the IP address. No need to include a database file in the application.}
13
+ spec.homepage = "https://github.com/pobing/sina_geoip"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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
+ spec.add_dependency 'json', '~> 1.4.6'
21
+ spec.add_dependency 'rest-client', '~> 1.6.1'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,37 @@
1
+ # encoding:utf-8
2
+ require "spec_helper"
3
+
4
+ describe SinaGeoIp do
5
+ before :all do
6
+ IP = "12.130.132.30"
7
+ end
8
+ it "execute success and return ok" do
9
+ res = SinaGeoIp.geolocation(IP)
10
+ res.class.should == Hash
11
+ res.size == 10
12
+ res[:ret].should == 1
13
+ end
14
+
15
+ it "precision country" do
16
+ res = SinaGeoIp.geolocation(IP,:precision=>:country)
17
+ res.size.should == 1
18
+ res.keys.include?(:country)
19
+ end
20
+
21
+ it "precision province" do
22
+ res = SinaGeoIp.geolocation(IP,:precision => :province)
23
+ res.size.should == 2
24
+ res.keys.include?(:province)
25
+ end
26
+
27
+ it "precision city" do
28
+ res = SinaGeoIp.geolocation(IP,:precision=>:city)
29
+ res.size.should == 3
30
+ res.keys.include?(:city)
31
+ end
32
+
33
+ it "Invalid IP address" do
34
+ expect { SinaGeoIp.geolocation("125.3366.363.25") }.to raise_error(RuntimeError)
35
+ end
36
+
37
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ require 'sina_geoip'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sina_geoip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - pobing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Retreive the geolocation of an IP address based on the http://int.dpool.sina.com.cn/iplookup/iplookup.php
70
+ webservice
71
+ email:
72
+ - cn.jdong@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/sina_geoip.rb
83
+ - lib/sina_geoip/version.rb
84
+ - sina_geoip.gemspec
85
+ - spec/sina_geoip/sina_geoip_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/pobing/sina_geoip
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A call to the sina geoip app . will be done to retreive the geolocation based
111
+ on the IP address. No need to include a database file in the application.
112
+ test_files:
113
+ - spec/sina_geoip/sina_geoip_spec.rb
114
+ - spec/spec_helper.rb