Class: Location

Inherits:
Struct
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/location.rb

Constant Summary collapse

KEY_ORDER =

For mapping the JSON camelcase data from the location service into the Location structs:

[:id, :sasId, :name, :code, :kind, :lat, :lng, :timezone, :parentLocationId, :updatedAt, :createdAt, :childLocations, :beacons].map(&:to_s).freeze
CACHE_KEY =
'location_service_request_cache'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#beaconsObject

Returns the value of attribute beacons

Returns:

  • (Object)

    the current value of beacons



8
9
10
# File 'lib/location.rb', line 8

def beacons
  @beacons
end

#child_locationsObject

Returns the value of attribute child_locations

Returns:

  • (Object)

    the current value of child_locations



8
9
10
# File 'lib/location.rb', line 8

def child_locations
  @child_locations
end

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



8
9
10
# File 'lib/location.rb', line 8

def code
  @code
end

#created_atObject

Returns the value of attribute created_at

Returns:

  • (Object)

    the current value of created_at



8
9
10
# File 'lib/location.rb', line 8

def created_at
  @created_at
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



8
9
10
# File 'lib/location.rb', line 8

def id
  @id
end

#kindObject

Returns the value of attribute kind

Returns:

  • (Object)

    the current value of kind



8
9
10
# File 'lib/location.rb', line 8

def kind
  @kind
end

#latObject

Returns the value of attribute lat

Returns:

  • (Object)

    the current value of lat



8
9
10
# File 'lib/location.rb', line 8

def lat
  @lat
end

#lngObject

Returns the value of attribute lng

Returns:

  • (Object)

    the current value of lng



8
9
10
# File 'lib/location.rb', line 8

def lng
  @lng
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/location.rb', line 8

def name
  @name
end

#parent_location_idObject

Returns the value of attribute parent_location_id

Returns:

  • (Object)

    the current value of parent_location_id



8
9
10
# File 'lib/location.rb', line 8

def parent_location_id
  @parent_location_id
end

#sas_idObject

Returns the value of attribute sas_id

Returns:

  • (Object)

    the current value of sas_id



8
9
10
# File 'lib/location.rb', line 8

def sas_id
  @sas_id
end

#timezoneObject

Returns the value of attribute timezone

Returns:

  • (Object)

    the current value of timezone



8
9
10
# File 'lib/location.rb', line 8

def timezone
  @timezone
end

#updated_atObject

Returns the value of attribute updated_at

Returns:

  • (Object)

    the current value of updated_at



8
9
10
# File 'lib/location.rb', line 8

def updated_at
  @updated_at
end

Class Method Details

.allObject



54
55
56
# File 'lib/location.rb', line 54

def all
  @locations ||= transform_data(request('/locations')).try(:compact) || []
end

.cacheObject



91
92
93
# File 'lib/location.rb', line 91

def cache
  defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new
end

.childrenObject



46
47
48
# File 'lib/location.rb', line 46

def children
  all.map{|l| l.child_locations }.compact
end

.find(id) ⇒ Object



24
25
26
# File 'lib/location.rb', line 24

def find(id)
  flattened.detect{|l| l.id == id }
end

.find_by_code(code) ⇒ Object



28
29
30
# File 'lib/location.rb', line 28

def find_by_code(code)
  flattened.detect{|l| l.code == code }
end

.find_by_codes(codes, child_locations = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/location.rb', line 38

def find_by_codes(codes, child_locations=nil)
  child_locations ||= all
  codes             = [codes].flatten
  code              = codes.shift
  leaf              = child_locations.detect{|l| l.code == code }
  codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations) 
end

.find_by_parent_code_and_child_name(parent_code, child_name) ⇒ Object Also known as: find_by_airport_and_gate



32
33
34
35
# File 'lib/location.rb', line 32

def find_by_parent_code_and_child_name(parent_code, child_name)
  airport = all.detect {|l| l.code == parent_code}
  airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name }
end

.flattenedObject



50
51
52
# File 'lib/location.rb', line 50

def flattened
  (all + children).flatten
end

.loggerObject



95
96
97
# File 'lib/location.rb', line 95

def logger
  defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

.request(endpoint) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/location.rb', line 58

def request(endpoint)
  user_token = Thread.current[:current_user].try(:authentication_token)
  url = Settings.location_service.url + "v1/" + endpoint + "?api_key=#{user_token||Settings.location_service.token}"
  cache.fetch(CACHE_KEY) do
    logger.debug url
    JSON.parse(RestClient.get(url))
  end
end

.resetObject



67
68
69
70
# File 'lib/location.rb', line 67

def reset
  Rails.cache.delete(CACHE_KEY)
  @locations = nil
end

.timeObject



88
89
90
# File 'lib/location.rb', line 88

def time
  Time.zone || Time
end

.transform_data(data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/location.rb', line 72

def transform_data(data)
  if data.kind_of? Array
    data.map { |v| transform_data(v) }

  elsif data.kind_of? Hash
    #HashWithIndifferentAccess[data.map {|k, v| [k.to_s.underscore, transform_data(v)] }]
    l = Location.new(*data.values_at(*KEY_ORDER))
    l.created_at = data['createdAt'] && time.at( data['createdAt']/1000 ) 
    l.updated_at = data['updatedAt'] && time.at( data['updatedAt']/1000 ) 
    l.child_locations = transform_data( data['childLocations'] ) || []
    l
  else
    data
  end
end

Instance Method Details

#ruby_timezoneObject



11
12
13
14
15
16
17
# File 'lib/location.rb', line 11

def ruby_timezone
  begin 
    TZInfo::Timezone.get(timezone) if timezone && !timezone.empty?
  rescue TZInfo::InvalidTimezoneIdentifier 
    raise TZInfo::InvalidTimezoneIdentifier.new("'#{timezone}' is an invalid identifier")
  end
end