Class: Snail
- Inherits:
-
Object
- Object
- Snail
- Includes:
- Initializable
- Defined in:
- lib/snail/constants.rb,
lib/snail.rb,
lib/snail/helpers.rb,
lib/snail/version.rb,
lib/snail/initializable.rb
Overview
TODO: carmen
Defined Under Namespace
Modules: Helpers, Initializable, Iso3166 Classes: UnknownAttribute, UnknownCountryError
Constant Summary collapse
- REGIONS =
{ # see http://en.wikipedia.org/wiki/Postcodes_in_Australia#Australia_States_and_territories :au => { 'Australian Capital Territory' => 'ACT', 'New South Wales' => 'NSW', 'Victoria' => 'VIC', 'Queensland' => 'QLD', 'South Australia' => 'SA', 'Western Australia' => 'WA', 'Tasmania' => 'TAS', 'Northern Territory' => 'NT' }, # see http://en.wikipedia.org/wiki/Canadian_subnational_postal_abbreviations :ca => { 'Alberta' => 'AB', 'British Columbia' => 'BC', 'Manitoba' => 'MB', 'New Brunswick' => 'NB', 'Newfoundland and Labrador' => 'NL', 'Nova Scotia' => 'NS', 'Northwest Territories' => 'NT', 'Nunavut' => 'NU', 'Ontario' => 'ON', 'Prince Edward Island' => 'PE', 'Quebec' => 'QC', 'Saskatchewan' => 'SK', 'Yukon' => 'YT' }, # see http://www.columbia.edu/kermit/postal.html#usa # and http://www.usps.com/ncsc/lookups/usps_abbreviations.html :us => { 'Alabama' => 'AL', 'Alaska' => 'AK', 'Arizona' => 'AZ', 'Arkansas' => 'AR', 'California' => 'CA', 'Colorado' => 'CO', 'Connecticut' => 'CT', 'Delaware' => 'DE', 'District Of Columbia' => 'DC', 'Florida' => 'FL', 'Georgia' => 'GA', 'Hawaii' => 'HI', 'Idaho' => 'ID', 'Illinois' => 'IL', 'Indiana' => 'IN', 'Iowa' => 'IA', 'Kansas' => 'KS', 'Kentucky' => 'KY', 'Louisiana' => 'LA', 'Maine' => 'ME', 'Maryland' => 'MD', 'Massachusetts' => 'MA', 'Michigan' => 'MI', 'Minnesota' => 'MN', 'Mississippi' => 'MS', 'Missouri' => 'MO', 'Montana' => 'MT', 'Nebraska' => 'NE', 'Nevada' => 'NV', 'New Hampshire' => 'NH', 'New Jersey' => 'NJ', 'New Mexico' => 'NM', 'New York' => 'NY', 'North Carolina' => 'NC', 'North Dakota' => 'ND', 'Ohio' => 'OH', 'Oklahoma' => 'OK', 'Oregon' => 'OR', 'Pennsylvania' => 'PA', 'Rhode Island' => 'RI', 'South Carolina' => 'SC', 'South Dakota' => 'SD', 'Tennessee' => 'TN', 'Texas' => 'TX', 'Utah' => 'UT', 'Vermont' => 'VT', 'Virginia' => 'VA', 'Washington' => 'WA', 'West Virginia' => 'WV', 'Wisconsin' => 'WI', 'Wyoming' => 'WY', # These are not states exactly, but they are addressed as states through USA 'American Samoa' => 'AS', 'Federated States Of Micronesia' => 'FM', 'Guam' => 'GU', 'Marshall Islands' => 'MH', 'Northern Mariana Islands' => 'MP', 'Palau' => 'PW', 'Puerto Rico' => 'PR', 'Virgin Islands' => 'VI', 'Armed Forces Africa' => 'AE', 'Armed Forces Americas (Except Canada)' => 'AA', 'Armed Forces Canada' => 'AE', 'Armed Forces Europe' => 'AE', 'Armed Forces Middle East' => 'AE', 'Armed Forces Pacific' => 'AP', }, # http://en.wikipedia.org/wiki/Counties_of_Ireland # NB: only includes Replublic of Ireland, also no abbreviations :ie => { 'Carlow' => 'Carlow', "Cavan" => 'Cavan', 'Clare' => 'Clare', 'Cork' => 'Cork', 'Donegal' => 'Donegal', 'Dublin' => 'Dublin', 'Dún Laoghaire–Rathdown' => 'Dún Laoghaire–Rathdown', 'Fingal' => 'Fingal', 'Galway' => 'Galway', 'Kerry' => 'Kerry', 'Kildare' => 'Kildare', 'Kilkenny' => 'Kilkenny', 'Laois' => 'Laois', 'Leitrim' => 'Leitrim', 'Limerick' => 'Limerick', 'Longford' => 'Longford', 'Louth' => 'Louth', 'Mayo' => 'Mayo', 'Meath' => 'Meath', 'Monaghan' => 'Monaghan', 'Offaly' => 'Offaly', 'Roscommon' => 'Roscommon', 'Sligo' => 'Sligo', 'South Dublin' => 'South Dublin', 'Tipperary' => 'Tipperary', 'Waterford' => 'Waterford', 'Westmeath' => 'Westmeath', 'Wexford' => 'Wexford', 'Wicklow' => 'Wicklow', } }
- VERSION =
'1.2.3'
Instance Attribute Summary collapse
-
#city ⇒ Object
Returns the value of attribute city.
-
#country ⇒ Object
Returns the value of attribute country.
-
#line_1 ⇒ Object
Returns the value of attribute line_1.
-
#line_2 ⇒ Object
Returns the value of attribute line_2.
-
#name ⇒ Object
My made-up standard fields.
-
#postal_code ⇒ Object
Returns the value of attribute postal_code.
-
#region ⇒ Object
Returns the value of attribute region.
Class Method Summary collapse
- .home_country ⇒ Object
- .home_country=(val) ⇒ Object
-
.load_helpers ⇒ Object
Load the SnailHelpers module into ActionView::Base.
- .lookup_country_iso(val) ⇒ Object
Instance Method Summary collapse
-
#city_line ⇒ Object
this method will get much larger.
-
#country_line ⇒ Object
TODO localize to the origin country.
-
#origin ⇒ Object
Where the mail is coming from.
-
#origin=(val) ⇒ Object
Where the mail is coming from.
- #to_html ⇒ Object
- #to_s ⇒ Object
Methods included from Initializable
Instance Attribute Details
#city ⇒ Object
Returns the value of attribute city.
21 22 23 |
# File 'lib/snail.rb', line 21 def city @city end |
#country ⇒ Object
Returns the value of attribute country.
24 25 26 |
# File 'lib/snail.rb', line 24 def country @country end |
#line_1 ⇒ Object
Returns the value of attribute line_1.
19 20 21 |
# File 'lib/snail.rb', line 19 def line_1 @line_1 end |
#line_2 ⇒ Object
Returns the value of attribute line_2.
20 21 22 |
# File 'lib/snail.rb', line 20 def line_2 @line_2 end |
#name ⇒ Object
My made-up standard fields.
18 19 20 |
# File 'lib/snail.rb', line 18 def name @name end |
#postal_code ⇒ Object
Returns the value of attribute postal_code.
23 24 25 |
# File 'lib/snail.rb', line 23 def postal_code @postal_code end |
#region ⇒ Object
Returns the value of attribute region.
22 23 24 |
# File 'lib/snail.rb', line 22 def region @region end |
Class Method Details
.home_country ⇒ Object
55 56 57 |
# File 'lib/snail.rb', line 55 def self.home_country @home_country ||= "US" end |
.home_country=(val) ⇒ Object
59 60 61 |
# File 'lib/snail.rb', line 59 def self.home_country=(val) @home_country = lookup_country_iso(val) end |
.load_helpers ⇒ Object
Load the SnailHelpers module into ActionView::Base. Previously this was done automatically, but going forward it must be included explicitly by calling Snail.load_helpers.
48 49 50 51 52 53 |
# File 'lib/snail.rb', line 48 def self.load_helpers if defined? ActionView warn '[DEPRECATION] Snail::Helpers will be removed in a future release.' ActionView::Base.class_eval { include Snail::Helpers } end end |
.lookup_country_iso(val) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/snail.rb', line 63 def self.lookup_country_iso(val) return nil if val.nil? || val.empty? val = val.upcase if ::Snail::Iso3166::ALPHA2[val] val elsif iso = ::Snail::Iso3166::ALPHA2_EXCEPTIONS[val] iso elsif iso = ::Snail::Iso3166::ALPHA3_TO_ALPHA2[val] iso else nil end end |
Instance Method Details
#city_line ⇒ Object
this method will get much larger. completeness is out of my scope at this time. currently it’s based on the sampling of city line formats from frank’s compulsive guide.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/snail.rb', line 102 def city_line case country when 'CN', 'IN' "#{city}, #{region} #{postal_code}" when 'BR' "#{postal_code} #{city}-#{region}" when 'MX', 'SK' "#{postal_code} #{city}, #{region}" when 'IT' "#{postal_code} #{city} (#{region})" when 'BY' "#{postal_code} #{city}-(#{region})" when 'US', 'CA', 'AU', nil, "" "#{city} #{region} #{postal_code}" when 'IL', 'DK', 'FI', 'FR', 'DE', 'GR', 'IT', 'NO', 'ES', 'SE', 'TR', 'CY', 'PT', 'MK', 'BA' "#{postal_code} #{city}" when 'KW', 'SY', 'OM', 'EE', 'LU', 'BE', 'IS', 'CH', 'AT', 'MD', 'ME', 'RS', 'BG', 'GE', 'PL', 'AM', 'HR', 'RO', 'AZ' "#{postal_code} #{city}" when 'NL' "#{postal_code} #{city}" when 'IE' "#{city}, #{region}" when 'GB', 'RU', 'UA', 'JO', 'LB', 'IR', 'SA', 'NZ' "#{city} #{postal_code}" # Locally these may be on separate lines. The USPS prefers the city line above the country line, though. when 'EC' "#{postal_code} #{city}" when 'HK', 'IQ', 'YE', 'QA', 'AL' "#{city}" when 'AE' "#{postal_code}\n#{city}" when 'JP' "#{city}, #{region}\n#{postal_code}" when 'EG', 'ZA', 'IM', 'KZ', 'HU' "#{city}\n#{postal_code}" when 'LV' "#{city}, LV-#{postal_code}" when 'LT' "LT-#{postal_code} #{city}" when 'SI' "SI-#{postal_code} #{city}" when 'CZ' "#{postal_code} #{region}\n#{city}" else if Kernel.const_defined?("Rails") Rails.logger.error "[Snail] Unknown Country: #{country}" end "#{city} #{region} #{postal_code}" end end |
#country_line ⇒ Object
TODO localize to the origin country
153 154 155 |
# File 'lib/snail.rb', line 153 def country_line ::Snail::Iso3166::ALPHA2[country].first if country and self.origin != country end |
#origin ⇒ Object
Where the mail is coming from. Defaults to the global ‘home_country`.
83 84 85 |
# File 'lib/snail.rb', line 83 def origin @origin ||= Snail.home_country end |
#origin=(val) ⇒ Object
Where the mail is coming from.
78 79 80 |
# File 'lib/snail.rb', line 78 def origin=(val) @origin = Snail.lookup_country_iso(val) end |
#to_html ⇒ Object
96 97 98 |
# File 'lib/snail.rb', line 96 def to_html CGI.escapeHTML(to_s).gsub("\n", '<br />').html_safe end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/snail.rb', line 92 def to_s [name, line_1, line_2, city_line, country_line].select{|line| !(line.nil? or line.empty?)}.join("\n") end |