iso-country 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +76 -0
- data/Rakefile +2 -0
- data/iso-country.gemspec +17 -0
- data/lib/iso-country.rb +44 -0
- data/lib/iso-country/version.rb +5 -0
- metadata +53 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Chris Kalafarski
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Iso::Country
|
|
2
|
+
|
|
3
|
+
The iso-country gem helps you manage country data, without needing rely on a database.
|
|
4
|
+
|
|
5
|
+
It uses values from the ISO 3166-1 standard. The data can be localized, making it easy to present country names correctly for a given local (eg Spain vs España)
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
Once the gem is loaded it provides pure Ruby instances of the countries listed in the source data .json file. Countries are primarily referenced using a symbol of their ISO 3166-1 Alpha-2 two-character country code:
|
|
10
|
+
|
|
11
|
+
sample_codes = [:us, :gb, :ru]
|
|
12
|
+
|
|
13
|
+
The easiest way to access a country's object instance is with the ::find method:
|
|
14
|
+
|
|
15
|
+
Iso::Country::Country.find(:us)
|
|
16
|
+
# => #<Iso::Country::Country:0x007f93439c8c30>
|
|
17
|
+
|
|
18
|
+
You can return an array of multiple countries:
|
|
19
|
+
|
|
20
|
+
Country.find(:us, :gb, :ru)
|
|
21
|
+
|
|
22
|
+
To return an array of all countries (there is no guarantee as to the order):
|
|
23
|
+
|
|
24
|
+
Country.all # or Country.find(:all)
|
|
25
|
+
|
|
26
|
+
Basic full-name searching can be done with
|
|
27
|
+
|
|
28
|
+
Country.find_with_name("Brazil")
|
|
29
|
+
|
|
30
|
+
This will search all localizations, but the input must be an exact match. If you want to search a specific localization only:
|
|
31
|
+
|
|
32
|
+
Country.find_with_name("España", :es)
|
|
33
|
+
|
|
34
|
+
There is also a ::where method that will do it's best to find what you're looking for
|
|
35
|
+
|
|
36
|
+
Country.where(:us)
|
|
37
|
+
Country.where('RU')
|
|
38
|
+
Country.where('GBR')
|
|
39
|
+
Country.where(100)
|
|
40
|
+
Country.where('Sweeden')
|
|
41
|
+
Country.where(alpha2: :ru)
|
|
42
|
+
Country.where(alpha3: :gbr)
|
|
43
|
+
Country.where(numeric: 100)
|
|
44
|
+
|
|
45
|
+
## Mongoid
|
|
46
|
+
|
|
47
|
+
Support for country as a Mongoid field type is included
|
|
48
|
+
|
|
49
|
+
field :country, type: Iso::Country::Country
|
|
50
|
+
|
|
51
|
+
You could then
|
|
52
|
+
|
|
53
|
+
country = Country.find(params[:country])
|
|
54
|
+
Place.new(name: params[:name], country: country)
|
|
55
|
+
|
|
56
|
+
And later
|
|
57
|
+
|
|
58
|
+
place.country.name
|
|
59
|
+
|
|
60
|
+
Without needing to create a new table in your database with seeded country data
|
|
61
|
+
|
|
62
|
+
## Rails
|
|
63
|
+
|
|
64
|
+
A future version of the gem will include a form helper
|
|
65
|
+
|
|
66
|
+
## TODO
|
|
67
|
+
|
|
68
|
+
- Support for actual localization (en-us, etc) rather than just language (en, etc)
|
|
69
|
+
- When there's a default locale (eg in a rails app) use that
|
|
70
|
+
- Support for long and short names in localizations
|
|
71
|
+
- Better searching for partial name matches
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT license
|
data/Rakefile
ADDED
data/iso-country.gemspec
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/iso-country/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ['Chris Kalafarski']
|
|
6
|
+
gem.email = ['chris@farski.com']
|
|
7
|
+
gem.description = %q{A standard way of handling country data}
|
|
8
|
+
gem.summary = %q{A standard way of handling country data}
|
|
9
|
+
gem.homepage = ''
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = 'iso-country'
|
|
15
|
+
gem.require_paths = ['lib']
|
|
16
|
+
gem.version = Iso::Country::VERSION
|
|
17
|
+
end
|
data/lib/iso-country.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
require 'iso-country/version'
|
|
6
|
+
require 'iso-country/data'
|
|
7
|
+
require 'iso-country/exceptions'
|
|
8
|
+
require 'iso-country/finders'
|
|
9
|
+
require 'iso-country/instance'
|
|
10
|
+
require 'iso-country/mongoid'
|
|
11
|
+
|
|
12
|
+
module Iso
|
|
13
|
+
module Country
|
|
14
|
+
include Iso::Country::Exceptions
|
|
15
|
+
include Iso::Country::Data
|
|
16
|
+
|
|
17
|
+
class Country
|
|
18
|
+
include Iso::Country::InstanceMethods
|
|
19
|
+
|
|
20
|
+
@countries = {}
|
|
21
|
+
|
|
22
|
+
def self.demongoize(country)
|
|
23
|
+
find(country)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.evolve(country)
|
|
27
|
+
country.id
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
Iso::Country::Data::DICTIONARY[:countries].each do |c|
|
|
31
|
+
localizations = {}
|
|
32
|
+
if Iso::Country::Data::DICTIONARY[:localizations][c['alpha-2']]
|
|
33
|
+
localizations = Iso::Country::Data::DICTIONARY[:localizations][c['alpha-2']].inject({}){|memo, (k,v)| memo[k.to_sym] = v; memo}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
country = Country.new(c['alpha-2'], c['alpha-3'], c['numeric'], localizations)
|
|
37
|
+
@countries[country.id] = country
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
extend Iso::Country::Finders
|
|
41
|
+
include Iso::Country::Mongoid
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: iso-country
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Chris Kalafarski
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: A standard way of handling country data
|
|
15
|
+
email:
|
|
16
|
+
- chris@farski.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- .gitignore
|
|
22
|
+
- Gemfile
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- iso-country.gemspec
|
|
27
|
+
- lib/iso-country.rb
|
|
28
|
+
- lib/iso-country/version.rb
|
|
29
|
+
homepage: ''
|
|
30
|
+
licenses: []
|
|
31
|
+
post_install_message:
|
|
32
|
+
rdoc_options: []
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
none: false
|
|
37
|
+
requirements:
|
|
38
|
+
- - ! '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ! '>='
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubyforge_project:
|
|
49
|
+
rubygems_version: 1.8.23
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 3
|
|
52
|
+
summary: A standard way of handling country data
|
|
53
|
+
test_files: []
|