seventeen_mon 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 478fcca45e1767adda18e8287fa0de19786af7a2
4
+ data.tar.gz: 569dd944ccde9d37743c828e75f5e4037f5bdea2
5
+ SHA512:
6
+ metadata.gz: ba877d83a15ef768247e6a314ad31fd2dba8a1c1796180c8186a76bb10b6aa3761fc98bd9793dcb1d92328918ad9e4d27a69c66499e0e0c6f3653c91f312a9b9
7
+ data.tar.gz: 1e96a6e08efa6931aef39eca0948beb72e3b3662d9887cb3e471b1faa0e4a4151782da9f13e8ae88eb200b1ef3f028c5570ac5e88523a361dbe07a4955835cb1
data/.gitignore ADDED
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seventeen_mom.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jingkai He
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,36 @@
1
+ # SeventeenMon
2
+
3
+ Simply find location by IP.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'seventeen_mon', git: "git@github.com:mycolorway/SeventeenMon.git"
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or you can install simply by
17
+
18
+ $ gem install seventeen_mon
19
+
20
+ ## Usage
21
+
22
+ ```(ruby)
23
+ SeventeenMon.find_by_ip ip: "YOUR_IP_ADDRESS"
24
+ # => {:country=>"英国", :city=>"英国"}
25
+
26
+ SM.find_by_address address: "ruby-lang.com", protocol: "http"
27
+ # => {:country=>"荷兰", :city=>"荷兰"}
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( http://github.com/<my-github-username>/seventeen_mom/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run an IRB session with SeventeenMon preloaded"
7
+ task :console do
8
+ exec "irb -I lib -r seventeen_mon"
9
+ end
10
+
11
+ desc "Run the test suite"
12
+ RSpec::Core::RakeTask.new(:rspec) do |t|
13
+ t.pattern = FileList['spec/**/*_spec.rb']
14
+ t.rspec_opts = %w|--color|
15
+ end
16
+
17
+ task default: :spec
Binary file
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ module SeventeenMon
4
+ require "socket"
5
+ require "iconv"
6
+ require "ipaddr"
7
+ require "seventeen_mon/version"
8
+ require "seventeen_mon/ipdb"
9
+ require "seventeen_mon/ip"
10
+
11
+ def self.find_by_ip(_ip)
12
+ IP.new(ip: _ip).find
13
+ end
14
+
15
+ def self.find_by_address(address: nil, protocol: "http")
16
+ IP.new(address: address, protocol: protocol).find
17
+ end
18
+ end
19
+
20
+ SM = SeventeenMon
@@ -0,0 +1,52 @@
1
+ module SeventeenMon
2
+ class IP
3
+ attr_reader :ip
4
+
5
+ def initialize(ip: nil, address: nil, protocol: nil)
6
+ @ip = ip || Socket.getaddrinfo(address, protocol)[0][3]
7
+ end
8
+
9
+ def four_number
10
+ @four_number ||= begin
11
+ fn = ip.split(".").map(&:to_i)
12
+ raise "ip is no valid" if fn.length != 4 || fn.any?{ |d| d < 0 || d > 255}
13
+ fn
14
+ end
15
+ end
16
+
17
+ def ip2long
18
+ @ip2long ||= ::IPAddr.new(ip).to_i
19
+ end
20
+
21
+ def packed_ip
22
+ @packed_ip ||= [ ip2long ].pack 'N'
23
+ end
24
+
25
+ def find
26
+ tmp_offset = four_number[0] * 4
27
+ start = IPDB.instance.index[tmp_offset..(tmp_offset + 3)].unpack("V")[0] * 8 + 1024
28
+
29
+ index_offset = nil
30
+
31
+ while start < IPDB.instance.max_comp_length
32
+ if IPDB.instance.index[start..(start + 3)] >= packed_ip
33
+ index_offset = "#{IPDB.instance.index[(start + 4)..(start + 6)]}\x0".unpack("V")[0]
34
+ index_length = IPDB.instance.index[(start + 7)].unpack("C")[0]
35
+ break
36
+ end
37
+ start += 8
38
+ end
39
+
40
+ return "N/A" unless index_offset
41
+
42
+ result = IPDB.instance.seek(index_offset, index_length).map do |str|
43
+ ::Iconv.conv("UTF-8", "UTF-8", str)
44
+ end
45
+
46
+ {
47
+ country: result[0],
48
+ city: result[1]
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ module SeventeenMon
2
+ class IPDB
3
+
4
+ private_class_method :new
5
+
6
+ def ip_db_path
7
+ @ip_db_path ||= File.expand_path'../../data/17monipdb.dat', __FILE__
8
+ end
9
+ def ip_db
10
+ @ip_db ||= File.open ip_db_path, 'rb'
11
+ end
12
+
13
+ def offset
14
+ @offset ||= ip_db.read(4).unpack("Nlen")[0]
15
+ end
16
+
17
+ def index
18
+ @index ||= ip_db.read(offset - 4)
19
+ end
20
+
21
+ def max_comp_length
22
+ @max_comp_length ||= offset - 1028
23
+ end
24
+
25
+ def self.instance
26
+ @instance ||= self.send :new
27
+ end
28
+
29
+ def seek(_offset, length)
30
+ IO.read(ip_db_path, length, offset + _offset - 1024).split "\t"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module SeventeenMon
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seventeen_mon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "seventeen_mon"
8
+ spec.version = SeventeenMon::VERSION
9
+ spec.authors = ["Jingkai He"]
10
+ spec.email = ["jaxihe@gmail.com"]
11
+ spec.summary = %q{Simply find location by IP.}
12
+ spec.description = %q{Simply find location by IP.}
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 "iconv", "~> 1.0.4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake", "~> 10.1.1"
25
+ spec.add_development_dependency "rspec", "~> 2.14.1"
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeventeenMon do
4
+ describe "# IPDB loading" do
5
+ it 'should be eager loaded' do
6
+ ipdb_1 = SM::IPDB.instance
7
+ ipdb_2 = SM::IPDB.instance
8
+
9
+ ipdb_1.object_id.should == ipdb_2.object_id
10
+ end
11
+ end
12
+
13
+ describe "# query test" do
14
+ before do
15
+ @ip_param = "129.215.5.255"
16
+ @url_params = {
17
+ address: "www.ruby-lang.com",
18
+ protocol: "http"
19
+ }
20
+ end
21
+
22
+ it "can find location by ip" do
23
+ result = SM.find_by_ip @ip_param
24
+ result.should include(:city)
25
+ result.should include(:country)
26
+ end
27
+
28
+ it "can find location by address" do
29
+ result = SM.find_by_address @url_params
30
+ result.should include(:city)
31
+ result.should include(:country)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ require "seventeen_mon"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ config.order = 'random'
9
+
10
+ config.color_enabled = true
11
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seventeen_mon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jingkai He
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: iconv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.4
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.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
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.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 10.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ description: Simply find location by IP.
70
+ email:
71
+ - jaxihe@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/data/17monipdb.dat
82
+ - lib/seventeen_mon.rb
83
+ - lib/seventeen_mon/ip.rb
84
+ - lib/seventeen_mon/ipdb.rb
85
+ - lib/seventeen_mon/version.rb
86
+ - seventeen_mon.gemspec
87
+ - spec/seventeen_mon_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.0
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simply find location by IP.
113
+ test_files:
114
+ - spec/seventeen_mon_spec.rb
115
+ - spec/spec_helper.rb