Module: JapanPostcoder
- Defined in:
- lib/japan_postcoder.rb,
lib/version.rb,
lib/japan_postcoder/address.rb
Overview
Main module for JapanPostcoder Contains methods to convert Japanese postal code to address in string or hash format
Defined Under Namespace
Classes: Address, InvalidPostalCodeError
Constant Summary collapse
- VERSION =
'0.1.5'
Class Method Summary collapse
-
.get_address(postal_code, romaji: false) ⇒ Object
Returns an Address object Example: #<Japan::Address:0x00007f9d0a8d3d88 @postcode=“2140037”, @prefecture=“神奈川県”, @city=“川崎市”, @ward=“多摩区”, @district=“西生田”>.
-
.to_address(postal_code, romaji: false) ⇒ Object
Returns the address in string format Example: ‘神奈川県川崎市多摩区西生田’.
-
.to_address_hash(postal_code, romaji: false) ⇒ Object
Returns the address in hash format Example: { postcode: ‘2140037’, prefecture: ‘神奈川県’, city: ‘川崎市’, ward: ‘多摩区’, district: ‘西生田’ }.
-
.validate_postal_code!(postal_code) ⇒ Object
Validates the postal code format Raises InvalidPostalCodeError if the postal code is invalid.
Class Method Details
.get_address(postal_code, romaji: false) ⇒ Object
Returns an Address object Example: #<Japan::Address:0x00007f9d0a8d3d88 @postcode=“2140037”,
@prefecture="神奈川県",
@city="川崎市",
@ward="多摩区",
@district="西生田">
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/japan_postcoder.rb', line 61 def self.get_address(postal_code, romaji: false) # Read from ./jp_postcodes.json (Shift JIS) # Format: { # "2140037": { # "prefecture": "神奈川県", # "city": "川崎市", # "ward": "多摩区", # "district": "西生田" # } data = JSON.parse(File.read(File.join(__dir__, 'jp_postcodes.json'))) prefecture = data[postal_code]["prefecture#{'_romaji' if romaji}"] city = data[postal_code]["city#{'_romaji' if romaji}"] ward = data[postal_code]["ward#{'_romaji' if romaji}"] district = data[postal_code]["district#{'_romaji' if romaji}"] JapanPostcoder::Address.new(postal_code, prefecture, city, ward, district) end |
.to_address(postal_code, romaji: false) ⇒ Object
Returns the address in string format Example: ‘神奈川県川崎市多摩区西生田’
16 17 18 19 20 21 22 23 24 |
# File 'lib/japan_postcoder.rb', line 16 def self.to_address(postal_code, romaji: false) postal_code = postal_code.to_s validate_postal_code!(postal_code) address = get_address(postal_code, romaji: romaji) address.to_s(romaji: romaji) end |
.to_address_hash(postal_code, romaji: false) ⇒ Object
Returns the address in hash format Example:
postcode: '2140037',
prefecture: '神奈川県',
city: '川崎市',
ward: '多摩区',
district: '西生田'
35 36 37 38 39 40 41 42 43 |
# File 'lib/japan_postcoder.rb', line 35 def self.to_address_hash(postal_code, romaji: false) postal_code = postal_code.to_s validate_postal_code!(postal_code) address = get_address(postal_code, romaji: romaji) address.to_h end |
.validate_postal_code!(postal_code) ⇒ Object
Validates the postal code format Raises InvalidPostalCodeError if the postal code is invalid
47 48 49 50 51 52 |
# File 'lib/japan_postcoder.rb', line 47 def self.validate_postal_code!(postal_code) return if postal_code.match?(/\A\d{7}\z/) raise InvalidPostalCodeError, 'Invalid postal code. Please make sure the postal code is 7 digits long and only contains numbers.' end |