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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 772cdf4092ed406b8f744745b7b539ffa4375795498a4a688a71475a0df755be
4
- data.tar.gz: 805150748bd3696df1463a85a01064e56d07aaf32723fed065667e8fb9b39da8
3
+ metadata.gz: 2c4a9dbfae1ec276cef2d3d34847f2b388c33e5e77adcf786eaa3632d800a6f1
4
+ data.tar.gz: ce43acc978c9a883764875763b3644851ed8f4f4dd7bf7eb89b66510cd5a90f5
5
5
  SHA512:
6
- metadata.gz: dad024a32e0b8da163ef23d513f85bacb258753a9e51c268e7c1cf35954dee0ef31c7956ee8c3291c83827db9b4d2a9f57a0eebbde77a1efadf64e1560dc72e2
7
- data.tar.gz: f5a67b073fa69e1eaf2fb8a997187da4d62df03421eca50716387120f48015c945e38defe7399dba371d5de8282083b3fa3dccd4b588b3bb2275d3403e6e4d30
6
+ metadata.gz: 4d013a6c530c203c51fb30687fcdd68bf05f7539304de62cde1ee69d8711305397db32b8ec078ab977def68e0a7b28d02ae73eb2e6848f865efc5b3803d4b84e
7
+ data.tar.gz: 3605cc3efee40bc90acfe969646c7f65478423e1ad1cc880da4c1076b1481313610998f1a74a63f7c7340e65d971cf939811de53a1c64b13d4633610b81625a0
data/kokki.gemspec CHANGED
@@ -30,6 +30,4 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "rspec", "~> 3.8"
31
31
  spec.add_development_dependency "vcr", "~> 5.0"
32
32
  spec.add_development_dependency "webmock", "~> 3.6"
33
-
34
- spec.add_dependency "geocoder", "~> 1.5"
35
33
  end
data/lib/kokki.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require "kokki/version"
4
4
  require "kokki/errors"
5
5
 
6
+ require "kokki/ipinfo"
7
+
6
8
  require "kokki/dictionary"
7
9
  require "kokki/ip_address"
8
10
  require "kokki/converter"
@@ -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
- def initialize(address)
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
- out << Geocoder.search(address)&.first&.country_code&.upcase
20
- rescue Geocoder::Error => _e
21
- out << nil
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 ip_address?(address)
43
+ def valid_format?
28
44
  IPAddr.new(address)
29
45
  true
30
46
  rescue IPAddr::InvalidAddressError => _e
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kokki
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.4"
5
5
  end
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.3
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-07-04 00:00:00.000000000 Z
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.2
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