Class: Locode::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/locode/location.rb

Instance Method Summary collapse

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_namesObject

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_diacriticsObject

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_codeObject

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

#coordinatesObject

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_codeObject

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

#dateObject

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_nameObject

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_diacriticsObject

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_classifierObject

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_codeObject

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

#locodeObject

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_completedObject

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

#statusObject

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

#subdivisionObject

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_hObject

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_jsonObject

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_sObject

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

Returns:

  • (Boolean)


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