Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial

  • Loading branch information...
commit 8a59bee0ecbda1893ffc337575d66aa5cba01a55 0 parents
@farski authored
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in iso-country.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Chris Kalafarski
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 README.md
@@ -0,0 +1,76 @@
+# Iso::Country
+
+The iso-country gem helps you manage country data, without needing rely on a database.
+
+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)
+
+## Usage
+
+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:
+
+ sample_codes = [:us, :gb, :ru]
+
+The easiest way to access a country's object instance is with the ::find method:
+
+ Iso::Country::Country.find(:us)
+ # => #<Iso::Country::Country:0x007f93439c8c30>
+
+You can return an array of multiple countries:
+
+ Country.find(:us, :gb, :ru)
+
+To return an array of all countries (there is no guarantee as to the order):
+
+ Country.all # or Country.find(:all)
+
+Basic full-name searching can be done with
+
+ Country.find_with_name("Brazil")
+
+This will search all localizations, but the input must be an exact match. If you want to search a specific localization only:
+
+ Country.find_with_name("España", :es)
+
+There is also a ::where method that will do it's best to find what you're looking for
+
+ Country.where(:us)
+ Country.where('RU')
+ Country.where('GBR')
+ Country.where(100)
+ Country.where('Sweeden')
+ Country.where(alpha2: :ru)
+ Country.where(alpha3: :gbr)
+ Country.where(numeric: 100)
+
+## Mongoid
+
+Support for country as a Mongoid field type is included
+
+ field :country, type: Iso::Country::Country
+
+You could then
+
+ country = Country.find(params[:country])
+ Place.new(name: params[:name], country: country)
+
+And later
+
+ place.country.name
+
+Without needing to create a new table in your database with seeded country data
+
+## Rails
+
+A future version of the gem will include a form helper
+
+## TODO
+
+- Support for actual localization (en-us, etc) rather than just language (en, etc)
+- When there's a default locale (eg in a rails app) use that
+- Support for long and short names in localizations
+- Better searching for partial name matches
+
+
+## License
+
+MIT license
2  Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
17 iso-country.gemspec
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/iso-country/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ['Chris Kalafarski']
+ gem.email = ['chris@farski.com']
+ gem.description = %q{A standard way of handling country data}
+ gem.summary = %q{A standard way of handling country data}
+ gem.homepage = ''
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = 'iso-country'
+ gem.require_paths = ['lib']
+ gem.version = Iso::Country::VERSION
+end
44 lib/iso-country.rb
@@ -0,0 +1,44 @@
+# encoding: UTF-8
+
+require 'json'
+
+require 'iso-country/version'
+require 'iso-country/data'
+require 'iso-country/exceptions'
+require 'iso-country/finders'
+require 'iso-country/instance'
+require 'iso-country/mongoid'
+
+module Iso
+ module Country
+ include Iso::Country::Exceptions
+ include Iso::Country::Data
+
+ class Country
+ include Iso::Country::InstanceMethods
+
+ @countries = {}
+
+ def self.demongoize(country)
+ find(country)
+ end
+
+ def self.evolve(country)
+ country.id
+ end
+
+ Iso::Country::Data::DICTIONARY[:countries].each do |c|
+ localizations = {}
+ if Iso::Country::Data::DICTIONARY[:localizations][c['alpha-2']]
+ localizations = Iso::Country::Data::DICTIONARY[:localizations][c['alpha-2']].inject({}){|memo, (k,v)| memo[k.to_sym] = v; memo}
+ end
+
+ country = Country.new(c['alpha-2'], c['alpha-3'], c['numeric'], localizations)
+ @countries[country.id] = country
+ end
+
+ extend Iso::Country::Finders
+ include Iso::Country::Mongoid
+ end
+ end
+end
5 lib/iso-country/version.rb
@@ -0,0 +1,5 @@
+module Iso
+ module Country
+ VERSION = '0.1.0'
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.