kokki 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/kokki.gemspec +0 -2
- data/lib/kokki.rb +2 -0
- data/lib/kokki/ip_address.rb +23 -7
- data/lib/kokki/ipinfo.rb +60 -0
- data/lib/kokki/version.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c4a9dbfae1ec276cef2d3d34847f2b388c33e5e77adcf786eaa3632d800a6f1
|
4
|
+
data.tar.gz: ce43acc978c9a883764875763b3644851ed8f4f4dd7bf7eb89b66510cd5a90f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d013a6c530c203c51fb30687fcdd68bf05f7539304de62cde1ee69d8711305397db32b8ec078ab977def68e0a7b28d02ae73eb2e6848f865efc5b3803d4b84e
|
7
|
+
data.tar.gz: 3605cc3efee40bc90acfe969646c7f65478423e1ad1cc880da4c1076b1481313610998f1a74a63f7c7340e65d971cf939811de53a1c64b13d4633610b81625a0
|
data/kokki.gemspec
CHANGED
data/lib/kokki.rb
CHANGED
data/lib/kokki/ip_address.rb
CHANGED
@@ -1,30 +1,46 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "ipaddr"
|
4
|
-
require "geocoder"
|
5
4
|
|
6
5
|
module Kokki
|
7
6
|
class IPAddress
|
8
7
|
attr_reader :address
|
9
8
|
|
10
|
-
|
11
|
-
raise InvalidInputError, "invalid input: #{address}" unless ip_address?(address)
|
9
|
+
PRIVATE_IPS = %w(10.0.0.0/8 172.16.0.0/12 192.168.0.0/16).map { |ip| IPAddr.new(ip) }.freeze
|
12
10
|
|
11
|
+
def initialize(address)
|
13
12
|
@address = address
|
13
|
+
|
14
|
+
raise InvalidInputError, "Invalid input: #{address}" if !valid_format? || internal?
|
14
15
|
end
|
15
16
|
|
16
17
|
def country_code
|
17
18
|
@country_code ||=
|
18
19
|
[].tap do |out|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
begin
|
21
|
+
res = IPInfo.geo(address)
|
22
|
+
out << res.dig("country")
|
23
|
+
rescue Error => _e
|
24
|
+
out << nil
|
25
|
+
end
|
22
26
|
end.first
|
23
27
|
end
|
24
28
|
|
29
|
+
def internal?
|
30
|
+
loopback? || private?
|
31
|
+
end
|
32
|
+
|
33
|
+
def loopback?
|
34
|
+
address == "0.0.0.0" || address.start_with?("127") || address == "::1"
|
35
|
+
end
|
36
|
+
|
37
|
+
def private?
|
38
|
+
PRIVATE_IPS.any? { |ip| ip.include?(address) }
|
39
|
+
end
|
40
|
+
|
25
41
|
private
|
26
42
|
|
27
|
-
def
|
43
|
+
def valid_format?
|
28
44
|
IPAddr.new(address)
|
29
45
|
true
|
30
46
|
rescue IPAddr::InvalidAddressError => _e
|
data/lib/kokki/ipinfo.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "net/https"
|
5
|
+
require "uri"
|
6
|
+
|
7
|
+
module Kokki
|
8
|
+
class IPInfo
|
9
|
+
HOST = "ipinfo.io"
|
10
|
+
BASE_URL = "https://#{HOST}"
|
11
|
+
|
12
|
+
def geo(address)
|
13
|
+
get("/#{address}/geo") { |json| json }
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.geo(address)
|
17
|
+
new.geo address
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def url_for(path)
|
23
|
+
URI(BASE_URL + path)
|
24
|
+
end
|
25
|
+
|
26
|
+
def https_options
|
27
|
+
if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"]
|
28
|
+
uri = URI(proxy)
|
29
|
+
{
|
30
|
+
proxy_address: uri.hostname,
|
31
|
+
proxy_port: uri.port,
|
32
|
+
proxy_from_env: false,
|
33
|
+
use_ssl: true
|
34
|
+
}
|
35
|
+
else
|
36
|
+
{ use_ssl: true }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def request(req)
|
41
|
+
Net::HTTP.start(HOST, 443, https_options) do |http|
|
42
|
+
response = http.request(req)
|
43
|
+
|
44
|
+
case response.code
|
45
|
+
when "200"
|
46
|
+
yield JSON.parse(response.body)
|
47
|
+
else
|
48
|
+
raise(Error, "Unsupported response code returned: #{response.code}")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def get(path, &block)
|
54
|
+
uri = url_for(path)
|
55
|
+
get = Net::HTTP::Get.new(uri)
|
56
|
+
|
57
|
+
request(get, &block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/kokki/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kokki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manabu Niseki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,20 +94,6 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '3.6'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: geocoder
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - "~>"
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '1.5'
|
104
|
-
type: :runtime
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - "~>"
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '1.5'
|
111
97
|
description: Convert a country name / code & IP address to an emoji flag
|
112
98
|
email:
|
113
99
|
- manabu.niseki@gmail.com
|
@@ -134,6 +120,7 @@ files:
|
|
134
120
|
- lib/kokki/dictionary.rb
|
135
121
|
- lib/kokki/errors.rb
|
136
122
|
- lib/kokki/ip_address.rb
|
123
|
+
- lib/kokki/ipinfo.rb
|
137
124
|
- lib/kokki/version.rb
|
138
125
|
homepage: https://github.com/ninoseki/kokki
|
139
126
|
licenses:
|
@@ -154,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
141
|
- !ruby/object:Gem::Version
|
155
142
|
version: '0'
|
156
143
|
requirements: []
|
157
|
-
rubygems_version: 3.0.
|
144
|
+
rubygems_version: 3.0.3
|
158
145
|
signing_key:
|
159
146
|
specification_version: 4
|
160
147
|
summary: Convert a country name / code & IP address to an emoji flag
|