mxmnd 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mxmnd.rb +76 -0
- data/mxmnd.gemspec +28 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 56a961e68bbf46c972d8cf3d95d76fc04de49f86
|
4
|
+
data.tar.gz: c31b670924998c08ef9ce7a42d9d72260bca7db4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff6fc0193225dfa1758eeaacc6eb596a1fc4b9b785ffa322973b57d472ecf58488471bc1f8008714ed1b671bebca73d5006081da3e48c2e5bc4e91586b97414d
|
7
|
+
data.tar.gz: df25fff1a04246fc4700d04f3b7801075a2b41a691732eeb04d2c20eb69831a981e28e2bdb2f882b62fa2766d1571033eec7093e091f8ce90a78c4f8c9a55b75
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Valentin Vasilyev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Mxmnd
|
2
|
+
|
3
|
+
Minimalistic MaxMind API wrapper for Ruby.
|
4
|
+
|
5
|
+
`Faraday` is the only runtime dependency.
|
6
|
+
|
7
|
+
Supports only `#city` API call. API compatible with `Geoip2.city`.
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'mxmnd'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
Mxmnd.city('8.8.8.8')
|
22
|
+
```
|
23
|
+
|
24
|
+
If you want to use faraday options, you can pass them as a second parameter:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Mxmnd.city('8.8.8.8', request: {timeout: 1})
|
28
|
+
```
|
29
|
+
|
30
|
+
## License
|
31
|
+
|
32
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
33
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mxmnd"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/mxmnd.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
module Mxmnd
|
5
|
+
VERSION = '0.1.0'
|
6
|
+
BASE_URL = 'https://geoip.maxmind.com'.freeze
|
7
|
+
BASE_PATH = '/geoip/v2.1'.freeze
|
8
|
+
USER_ID_ENV_KEY = 'MAX_MIND_USER_ID'.freeze
|
9
|
+
LICENSE_KEY_ENV_KEY = 'MAX_MIND_LICENSE_KEY'.freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def city(ip, faraday_options = {})
|
13
|
+
raise BadRequest.new('IP_ADDRESS_REQUIRED', 'You have not passed IP address.') unless ip
|
14
|
+
path = "/city/#{ip}"
|
15
|
+
get(connection(faraday_options), path)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def connection(faraday_options = {})
|
21
|
+
@connection ||= Faraday.new(BASE_URL, faraday_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(connection, path)
|
25
|
+
connection.basic_auth(ENV[USER_ID_ENV_KEY], ENV[LICENSE_KEY_ENV_KEY])
|
26
|
+
full_path = "#{BASE_PATH}#{path}"
|
27
|
+
response = connection.get(full_path)
|
28
|
+
raise_if_error!(response)
|
29
|
+
JSON.parse(response.body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def raise_if_error!(response)
|
33
|
+
case response.status
|
34
|
+
when 400 then raise_client_error(response, BadRequest)
|
35
|
+
when 401 then raise_client_error(response, Unauthorized)
|
36
|
+
when 402 then raise_client_error(response, PaymentRequired)
|
37
|
+
when 403 then raise_client_error(response, Forbidden)
|
38
|
+
# MaxMind will only return 503, but just in case
|
39
|
+
when 500..599 then raise ServerError
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def raise_client_error(response, klass)
|
44
|
+
response = JSON.parse(response.body)
|
45
|
+
code, error = response.values_at(*%w(code error))
|
46
|
+
raise klass.new(code, error)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class ClientError < StandardError
|
51
|
+
attr_reader :code
|
52
|
+
|
53
|
+
def initialize(code, error)
|
54
|
+
@code = code
|
55
|
+
super(error)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Unauthorized < ClientError
|
60
|
+
def initialize(code, error)
|
61
|
+
msg = "#{error} Set valid #{USER_ID_ENV_KEY} and #{LICENSE_KEY_ENV_KEY} environmental variables to authenticate with MaxMind API."
|
62
|
+
super(code, msg)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class BadRequest < ClientError; end
|
67
|
+
class PaymentRequired < ClientError; end
|
68
|
+
class NotFound < ClientError; end
|
69
|
+
class Forbidden < ClientError; end
|
70
|
+
|
71
|
+
class ServerError < StandardError
|
72
|
+
def initialize
|
73
|
+
super('MaxMind service is not available (503)')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/mxmnd.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 'mxmnd'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mxmnd"
|
8
|
+
spec.version = Mxmnd::VERSION
|
9
|
+
spec.authors = ["Valentin Vasilyev"]
|
10
|
+
spec.email = ["valentin.vasilyev@outlook.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Minimalistic MaxMind API wrapper for Ruby}
|
13
|
+
spec.homepage = "https://github.com/Valve/mxmnd"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
|
27
|
+
spec.add_dependency "faraday"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mxmnd
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Valentin Vasilyev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- valentin.vasilyev@outlook.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/mxmnd.rb
|
86
|
+
- mxmnd.gemspec
|
87
|
+
homepage: https://github.com/Valve/mxmnd
|
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.5.1
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Minimalistic MaxMind API wrapper for Ruby
|
111
|
+
test_files: []
|