kokki 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Kokki
6
+ class Dictionary
7
+ def lookup_by_alpha_2_code(code)
8
+ lookup("alpha_2_code", code)
9
+ end
10
+
11
+ def lookup_by_alpha_3_code(code)
12
+ lookup("alpha_3_code", code)
13
+ end
14
+
15
+ def lookup_by_name(name)
16
+ lookup("name", name)
17
+ end
18
+
19
+ private
20
+
21
+ def data
22
+ @data ||= YAML.safe_load(File.read(File.expand_path("./data/dict.yml", __dir__)))
23
+ end
24
+
25
+ def lookup(key, value)
26
+ entry = data.find { |e| e.dig(key).to_s == value }
27
+ entry&.dig("emoji")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kokki
4
+ class Error < StandardError; end
5
+
6
+ class InvalidInputError < Error; end
7
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+ require "geocoder"
5
+
6
+ module Kokki
7
+ class IPAddress
8
+ attr_reader :address
9
+
10
+ def initialize(address)
11
+ raise InvalidInputError, "invalid input: #{address}" unless ip_address?(address)
12
+
13
+ @address = address
14
+ end
15
+
16
+ def country_code
17
+ @country_code ||=
18
+ [].tap do |out|
19
+ out << Geocoder.search(address)&.first&.country
20
+ rescue Geocoder::Error => _
21
+ out << nil
22
+ end.first
23
+ end
24
+
25
+ private
26
+
27
+ def ip_address?(address)
28
+ IPAddr.new(address)
29
+ true
30
+ rescue IPAddr::InvalidAddressError => _
31
+ false
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kokki
4
+ VERSION = "0.1.0"
5
+ end
data/lib/kokki.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kokki/version"
4
+ require "kokki/errors"
5
+
6
+ require "kokki/dictionary"
7
+ require "kokki/ip_address"
8
+ require "kokki/converter"
9
+
10
+ module Kokki
11
+ def self.flagize(input)
12
+ Converter.convert input
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kokki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Manabu Niseki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-18 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.5'
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
+ description: Convert a country name / code & IP address to an emoji flag
112
+ email:
113
+ - manabu.niseki@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - kokki.gemspec
128
+ - lib/kokki.rb
129
+ - lib/kokki/converter.rb
130
+ - lib/kokki/data/dict.yml
131
+ - lib/kokki/dictionary.rb
132
+ - lib/kokki/errors.rb
133
+ - lib/kokki/ip_address.rb
134
+ - lib/kokki/version.rb
135
+ homepage: https://github.com/ninoseki/kokki
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.0.2
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Convert a country name / code & IP address to an emoji flag
158
+ test_files: []