Class: Locode::Location
- Inherits:
-
Object
- Object
- Locode::Location
- Defined in:
- lib/locode/location.rb
Instance Method Summary collapse
-
#alternative_full_names ⇒ Object
Public: The alternative names of a location.
-
#alternative_full_names=(alternative_full_name) ⇒ Object
Internal: adds the alternative full name of a location to the list of alternatives This might not be coherent with the normal semantics of a setter but I think it is ok since it is just a private method.
-
#alternative_full_names_without_diacritics ⇒ Object
Public: The alternative names of the location, but all non-latin-base characters are converted.
-
#alternative_full_names_without_diacritics=(alternative_full_name_without_diacritics) ⇒ Object
Internal: adds the alternative full name without diacritics of the location to the list of alternatives.
-
#city_code ⇒ Object
Public: Three letter code for a place.
-
#coordinates ⇒ Object
Public: The coordinates of a location.
-
#country_code ⇒ Object
Public: ISO 3166 alpha-2 Country Code.
-
#date ⇒ Object
Public: The date the location was added or updated.
-
#full_name ⇒ Object
Public: The name of a location.
-
#full_name_without_diacritics ⇒ Object
Public: The name of the location, but all non-latin-base characters are converted.
-
#function_classifier ⇒ Object
Public: contains a 1-digit function classifier code for the location.
-
#iata_code ⇒ Object
Public: The IATA code for the location if different from the second part of the UN/LOCODE.
-
#initialize(location_attributes) ⇒ Location
constructor
Public: Initializes a new Location.
-
#locode ⇒ Object
Public: UN/LOCODE.
-
#parsing_completed ⇒ Object
Internal: Once we are done parsing the csv files we no longer want to allow changes to the alternative_full_names or alternative_full_names_without_diacritics.
-
#status ⇒ Object
Public: Indicates the status of the entry by a 2-character code.
-
#subdivision ⇒ Object
Public: The ISO 1 to 3 character alphabetic and/or numeric code for the administrative division (state, province, department, etc.) of the country, as included in ISO 3166-2/1998.
-
#to_h ⇒ Object
Public: The Hash representation of the Location.
-
#to_json ⇒ Object
Public: The JSON representation of the Location.
-
#to_s ⇒ Object
Public: The String representation of the Location.
-
#valid? ⇒ Boolean
Public: To check whether the Locations attributes are all initialized correctly.
Constructor Details
#initialize(location_attributes) ⇒ Location
Public: Initializes a new Location
location_attributes - A Hash of the following structure
{
country_code: String | Symbol
city_code: String | Symbol
full_name: String
full_name_without_diacritics: String
subdivision: String | Symbol
function_classifier: String | Array
status: String | Symbol
date: String
iata_code: String | nil
coordinates: String | nil
}
Examples
Locode::Location.new
#=> <Locode::Location: invalid location>
location_attributes = {
country_code: 'US',
city_code: 'NYC',
full_name: 'New York',
full_name_without_diacritics: 'New York',
subdivision: 'NY',
function_classifier: '12345---',
status: 'AI',
date: '0401',
iata_code: '',
coordinates: '4042N 07400W'
}
Locode::Location.new(location_attributes)
#=> <Locode::Location: 'US NYC'>
43 44 45 46 47 48 49 50 |
# File 'lib/locode/location.rb', line 43 def initialize(location_attributes) location_attributes.each do |k,v| begin send("#{k}=", v) if !v.nil? rescue NoMethodError end end end |
Instance Method Details
#alternative_full_names ⇒ Object
Public: The alternative names of a location
Examples
Locode.find_by_locode('SE GOT').first.alternative_full_names
#=> ['Gothenburg']
Returns an Array of Strings containing the alternative full names of the
location or an empty array.
121 122 123 |
# File 'lib/locode/location.rb', line 121 def alternative_full_names @alternative_full_names ||= [] end |
#alternative_full_names=(alternative_full_name) ⇒ Object
Internal: adds the alternative full name of a location to the list of alternatives
This might not be coherent with the normal semantics of a setter
but I think it is ok since it is just a private method.
Returns nothing
130 131 132 133 134 135 |
# File 'lib/locode/location.rb', line 130 def alternative_full_names=(alternative_full_name) if alternative_full_name && alternative_full_name.is_a?(String) @alternative_full_names ||= [] @alternative_full_names << alternative_full_name.strip end end |
#alternative_full_names_without_diacritics ⇒ Object
Public: The alternative names of the location, but all non-latin-base
characters are converted.
Examples
Locode.find_by_locode('SE GOT').first.alternative_full_names_without_diacritics
#=> ['Gothenburg']
Returns an Array of Strings containing the alternative full names without
diacritics of the location or an empty array.
161 162 163 |
# File 'lib/locode/location.rb', line 161 def alternative_full_names_without_diacritics @alternative_full_names_without_diacritics ||= [] end |
#alternative_full_names_without_diacritics=(alternative_full_name_without_diacritics) ⇒ Object
Internal: adds the alternative full name without diacritics of the location
to the list of alternatives.
This might not be coherent with the normal semantics of a setter
but I think it is ok since it is just a private method.
Returns nothing
171 172 173 174 175 176 |
# File 'lib/locode/location.rb', line 171 def alternative_full_names_without_diacritics=(alternative_full_name_without_diacritics) if alternative_full_name_without_diacritics && alternative_full_name_without_diacritics.is_a?(String) @alternative_full_names_without_diacritics ||= [] @alternative_full_names_without_diacritics << alternative_full_name_without_diacritics.strip end end |
#city_code ⇒ Object
Public: Three letter code for a place
Examples
Locode.find_by_locode('US NYC').first.city_code
#=> 'NYC'
Returns a String containing the city code or an empty string.
95 96 97 |
# File 'lib/locode/location.rb', line 95 def city_code @city_code.to_s end |
#coordinates ⇒ Object
Public: The coordinates of a location.
Examples
Locode.find_by_locode('SE GOT').first.coordinates
#=> nil
Locode.find_by_locode('SE GO2').first.coordinates
#=> '5742N 01156E'
Returns nil if no coordinates are associated with the Location
otherwise it returns a String with the coordinates which
represents these with two numbers and letters for the cardinal
directions. The first followed by either N or S, the second by
either E or W.
295 296 297 |
# File 'lib/locode/location.rb', line 295 def coordinates @coordinates end |
#country_code ⇒ Object
Public: ISO 3166 alpha-2 Country Code
Examples
Locode.find_by_locode('US NYC').first.country_code
#=> 'US'
Returns a String containing the country code or an empty string.
83 84 85 |
# File 'lib/locode/location.rb', line 83 def country_code @country_code.to_s end |
#date ⇒ Object
Public: The date the location was added or updated
Examples
Locode.find_by_locode('US NYC').first.date
#=> '0401'
Returns a String containing the date the location was added to the
list of LOCODEs. The meaning of the date values is the following:
'0207' equals July 2002, '9501' equals January 1995
257 258 259 |
# File 'lib/locode/location.rb', line 257 def date @date end |
#full_name ⇒ Object
Public: The name of a location
Examples
Locode.find_by_locode('SE GOT').first.full_name
#=> 'Göteborg'
Returns a String containing the full name of the location or an
empty string.
108 109 110 |
# File 'lib/locode/location.rb', line 108 def full_name @full_name end |
#full_name_without_diacritics ⇒ Object
Public: The name of the location, but all non-latin-base characters
are converted.
Examples
Locode.find_by_locode('SE GOT').first.full_name_without_diacritics
#=> 'Goteborg'
Returns a String which contains the the full name without
diacritics or an empty string
147 148 149 |
# File 'lib/locode/location.rb', line 147 def full_name_without_diacritics @full_name_without_diacritics end |
#function_classifier ⇒ Object
Public: contains a 1-digit function classifier code for the location
Examples
Locode.find_by_locode('US NYC').first.function_classifier
#=> [1, 2, 3, 4, 5]
Returns an Array containing Integer or :B with the following
meanings:
1 = seaport, any port with the possibility of transport via water
2 = rail terminal
3 = road terminal
4 = airport
5 = postal exchange office
6 = Inland Clearance Depot – ICD or "Dry Port"
7 = reserved for fixed transport functions (e.g. oil platform)
:B = border crossing
214 215 216 |
# File 'lib/locode/location.rb', line 214 def function_classifier @function_classifier end |
#iata_code ⇒ Object
Public: The IATA code for the location if different from the second
part of the UN/LOCODE. Else the second part of the UN/LOCODE.
Examples
Locode.find_by_locode('SE GOT').first.iata_code
#=> 'XWL'
Locode.find_by_locode('US NYC').first.iata_code
#=> 'NYC'
Returns a String which is the IATA code if it is different from
the city code of the LOCODE. Else it returns the city
code.
276 277 278 |
# File 'lib/locode/location.rb', line 276 def iata_code @iata_code end |
#locode ⇒ Object
Public: UN/LOCODE
Examples
Locode.find_by_locode('US NYC').first.locode
#=> 'US NYC'
Returns a String that represents the UN/LOCODE
71 72 73 |
# File 'lib/locode/location.rb', line 71 def locode "#{country_code.to_s} #{city_code.to_s}".strip end |
#parsing_completed ⇒ Object
Internal: Once we are done parsing the csv files we no longer want to allow
changes to the alternative_full_names or
alternative_full_names_without_diacritics.
Returns nothing
57 58 59 60 61 |
# File 'lib/locode/location.rb', line 57 def parsing_completed private :alternative_full_names=, :alternative_full_names_without_diacritics=, :parsing_completed end |
#status ⇒ Object
Public: Indicates the status of the entry by a 2-character code
Examples
Locode.find_by_locode('US NYC').first.status
#=> :AI
Returns a Symbol with the following meaning or nil
:AA = Approved by competent national government agency
:AC = Approved by Customs Authority
:AF = Approved by national facilitation body
:AI = Code adopted by international organisation (IATA or ECLAC)
:AM = Approved by the UN/LOCODE Maintenance Agency
:AS = Approved by national standardisation body
:AQ = Entry approved, functions not verified
:RL = Recognised location - Existence and representation of location name
confirmed by check against nominated gazetteer or other
reference work
:RN = Request from credible national sources for locations in their own country
:RQ = Request under consideration
:RR = Request rejected
:QQ = Original entry not verified since date indicated
:UR = Entry included on user's request; not officially approved
:XX = Entry that will be removed from the next issue of UN/LOCODE
243 244 245 |
# File 'lib/locode/location.rb', line 243 def status @status end |
#subdivision ⇒ Object
Public: The ISO 1 to 3 character alphabetic and/or numeric code for the
administrative division (state, province, department, etc.)
of the country, as included in ISO 3166-2/1998. Only the
latter part of the complete ISO 3166-2 code element (after
the hyphen) is shown.
Examples
Locode.find_by_locode('US NYC').first.subdivision
#=> 'NY'
Locode.find_by_locode('SE GOT').first.subdivision
#=> 'O'
Returns a String representing the subdivision or an empty string
193 194 195 |
# File 'lib/locode/location.rb', line 193 def subdivision @subdivision end |
#to_h ⇒ Object
Public: The Hash representation of the Location
Examples
Locode.find_by_locode('BE ANR').first.to_h
#=> {:country_code=>"BE", ... }
Returns a Hash that represents the Location
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/locode/location.rb', line 319 def to_h { country_code: country_code, city_code: city_code, full_name: full_name, full_name_without_diacritics: full_name_without_diacritics, subdivision: subdivision, function_classifier: function_classifier, status: status, date: date, iata_code: iata_code, coordinates: coordinates } end |
#to_json ⇒ Object
Public: The JSON representation of the Location
Examples
Locode.find_by_locode('US NYC').first.to_json
#=> {"country_code":"US","city_code":"NYC", ...}
Returns a JSON that represents the Location
342 343 344 |
# File 'lib/locode/location.rb', line 342 def to_json MultiJson.dump(self.to_h) end |
#to_s ⇒ Object
Public: The String representation of the Location
Examples
Locode.find_by_locode('US NYC').first.to_s
#=> <Locode::Location: 'US NYC'>
Returns a String that represents the Location
307 308 309 |
# File 'lib/locode/location.rb', line 307 def to_s "<Locode::Location: '#{locode}'>" end |
#valid? ⇒ Boolean
Public: To check whether the Locations attributes are all
initialized correctly.
Examples
Locode.find_by_locode('US NYC').first.valid?
#=> true
Locode::Location.new.valid?
#=> false
Returns true or false
358 359 360 |
# File 'lib/locode/location.rb', line 358 def valid? country_code && country_code.size == 2 && city_code && city_code.size == 3 end |