country_currency 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module CountryCurrency
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/overrides.yml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ "TWN":
3
+ :name: "Taiwan"
@@ -0,0 +1,7 @@
1
+ # Only works on unixy type systems.
2
+ # http://blog.behindlogic.com/2008/10/auto-generate-your-manifest-and-gemspec.html
3
+ desc 'Rebuild manifest and gemspec.'
4
+ task :cultivate do
5
+ system %q{touch Manifest.txt; rake check_manifest | grep -v "(in " | patch}
6
+ system %q{rake debug_gem | grep -v "(in " > `basename \`pwd\``.gemspec}
7
+ end
@@ -0,0 +1,13 @@
1
+ namespace :codes do
2
+ desc 'Create ISO 3166-1 code classes from Wikipedia ISO 3166-1 tables.'
3
+ task :update do
4
+ dirname = File.dirname(__FILE__)
5
+ gen = File.join(dirname, %w{iso_3166_1.rb})
6
+ lib = File.expand_path(File.join(dirname, %w{.. lib country_currency iso_3166_1.rb}))
7
+ require gen
8
+ modules = CountryCurrency::Task::UpdateCodes.get
9
+ File.open(lib, File::CREAT | File::TRUNC | File::WRONLY) do |f|
10
+ f.write modules
11
+ end
12
+ end
13
+ end # :currency
@@ -0,0 +1,74 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'erubis'
4
+ require 'yaml'
5
+
6
+ module CountryCurrency
7
+ module Task
8
+ module UpdateCodes
9
+ def self.get
10
+ doc = Nokogiri::HTML.parse(open('https://en.wikipedia.org/wiki/ISO_3166-1'), nil, 'UTF-8')
11
+ codes = {}
12
+ td_map = {
13
+ :name => 1,
14
+ :alpha2 => 2,
15
+ :alpha3 => 3,
16
+ :numeric => 4
17
+ }
18
+
19
+ code_labels = td_map.keys
20
+
21
+ doc.search('table.wikitable.sortable tr').each do |row|
22
+ value_hash = {}
23
+
24
+ code_labels.each do |key|
25
+ selector = "td:nth-of-type(#{td_map[key]})"
26
+ selector << ' a' if key == :name
27
+
28
+ value = row.search(selector).
29
+ reject { |el| el.parent.name == 'sup' }.
30
+ map(&:text).join.strip
31
+
32
+ next if value == ''
33
+
34
+ # This is a terrible hack to skip the first table of the
35
+ # Wikipedia page, under "Naming and disputes". The
36
+ # giveaway is that this table doesn't include a "Numeric
37
+ # code" field.
38
+ if key == :numeric
39
+ next unless value.to_i > 0
40
+ end
41
+
42
+ value_hash[key] = value
43
+
44
+ if value_hash.length == code_labels.length - 1
45
+ value_hash.keys.each do |value_hash_key|
46
+ codes[value_hash[value_hash_key]] = value_hash if value_hash_key == :alpha3
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ codes = recurse_merge(codes, self.exceptions)
53
+
54
+ to_ruby(codes) if codes
55
+ end
56
+
57
+ def self.to_ruby(codes)
58
+ tmpl = File.read(File.join(File.dirname(__FILE__), 'iso_3166_1.rb.erb'))
59
+ eruby = Erubis::Eruby.new(tmpl)
60
+ eruby.result(:codes => codes)
61
+ end
62
+
63
+ def self.exceptions
64
+ @config ||= YAML.load_file('overrides.yml')
65
+ end
66
+
67
+ def self.recurse_merge(a,b)
68
+ a.merge(b) do |_,x,y|
69
+ (x.is_a?(Hash) && y.is_a?(Hash)) ? recurse_merge(x,y) : y
70
+ end
71
+ end
72
+ end # UpdateCodes
73
+ end # Task
74
+ end # CountryCurrency
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ module CountryCurrency
4
+ class Code
5
+ <% codes.keys.each do |code| -%>
6
+ class <%= code.upcase %> < Code #:nodoc:
7
+ self.numeric = %q{<%= codes[code][:numeric] %>}
8
+ self.name = %q{<%= codes[code][:name] %>}
9
+ self.alpha2 = %q{<%= codes[code][:alpha2] %>}
10
+ self.alpha3 = %q{<%= codes[code][:alpha3] %>}
11
+ end
12
+ <% end -%>
13
+ end # end Code
14
+ end # CountryCurrency
15
+
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'tzinfo'
3
+ require 'country_currency'
4
+
5
+ class TestCountryCurrency < Test::Unit::TestCase
6
+
7
+ def test_all_tzinfo_names
8
+ result = TZInfo::Country.all.any? do |c|
9
+ CountryCurrency.find(c.code).name != c.name
10
+ end
11
+
12
+ assert !result
13
+ end
14
+
15
+ def test_find_by_all_names_and_codes
16
+ assert_equal 'Argentina', CountryCurrency.find('AR').name
17
+ assert_equal 'Argentina', CountryCurrency.find('Argentina').name
18
+ assert_equal 'Argentina', CountryCurrency.find('arGentIna').name
19
+ assert_equal 'Argentina', CountryCurrency.find('ARG').name
20
+ end
21
+
22
+ def test_search_by_currency
23
+ assert_equal 'Argentina', CountryCurrency.search_by_currency('ARS').first.name
24
+
25
+ assert_includes CountryCurrency.search_by_currency('USD').map(&:name), 'United States'
26
+ end
27
+
28
+ def test_currency_symbol
29
+ assert_equal '$', CountryCurrency.find('AR').currency_symbol
30
+ assert_equal '$', CountryCurrency.search_by_currency('ARS').first.currency_symbol
31
+ assert_equal 'u$s', CountryCurrency.find('US').currency_symbol
32
+ assert_equal ['u$s'], CountryCurrency.search_by_currency('USD').map(&:currency_symbol).uniq
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: country_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "N\xC3\xA9stor Coppi"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2018-04-20 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: "12"
22
+ type: :runtime
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: tzinfo
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: "1"
32
+ type: :runtime
33
+ version_requirements: *id002
34
+ - !ruby/object:Gem::Dependency
35
+ name: test-unit
36
+ prerelease: false
37
+ requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: "3.2"
42
+ type: :development
43
+ version_requirements: *id003
44
+ description: ISO country code and currency library
45
+ email: nestorcoppi@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files: []
51
+
52
+ files:
53
+ - .travis.yml
54
+ - CHANGELOG.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - Manifest.txt
58
+ - README.md
59
+ - Rakefile
60
+ - VERSION
61
+ - country_currency.gemspec
62
+ - lib/country_currency.rb
63
+ - lib/country_currency/calling.rb
64
+ - lib/country_currency/code.rb
65
+ - lib/country_currency/continent.rb
66
+ - lib/country_currency/country_currency.rb
67
+ - lib/country_currency/iso_13616_1.rb
68
+ - lib/country_currency/iso_3166_1.rb
69
+ - lib/country_currency/iso_4217.rb
70
+ - lib/country_currency/version.rb
71
+ - overrides.yml
72
+ - rakelib/cultivate.rake
73
+ - rakelib/iso_3166_1.rake
74
+ - rakelib/iso_3166_1.rb
75
+ - rakelib/iso_3166_1.rb.erb
76
+ - test/country_currency_test.rb
77
+ homepage: http://github.com/sequre/country_currency
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - &id004
90
+ - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - *id004
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.12
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Provides ISO 3166-1 country codes/names and ISO 4217 currencies.
103
+ test_files: []
104
+