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_PRE =
"location_service_client".freeze
BENCHMARK =
false.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



90
91
92
93
94
95
96
# File 'lib/location.rb', line 90

def all
  bench do
    cache.fetch("#{CACHE_PRE}_request") do 
      transform_data(request('/locations').try(:compact) || [])
    end
  end
end

.benchObject

e.g. logb(caller)



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

def bench # e.g. logb(caller[0])
  if !BENCHMARK
    yield
  else
    start = Time.now
    v = yield
    logger.fatal v.try(:name)
    logger.fatal "#{caller[0].split(' ').last} #{(Time.now-start)*1000}ms"
    v
  end
end

.cacheObject



133
134
135
# File 'lib/location.rb', line 133

def cache
  @cache ||= defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new('tmp/cache')
end

.find(id) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/location.rb', line 50

def find(id)
  bench do
    cache.fetch("#{CACHE_PRE}-id-#{id}") do
      tables[:id_table][id]
    end
  end
end

.find_by_code(code) ⇒ Object



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

def find_by_code(code)
  bench do
    cache.fetch("#{CACHE_PRE}-code-#{code}") do
      tables[:code_table][code]
    end
  end
end

.find_by_codes(codes, child_locations = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/location.rb', line 74

def find_by_codes(codes, child_locations=nil)
  bench do
    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
end

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



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

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

.flattenedObject



84
85
86
87
88
# File 'lib/location.rb', line 84

def flattened
  bench do
    (all +  all.map {|l| l.child_locations}.compact).flatten
  end
end

.loggerObject



141
142
143
# File 'lib/location.rb', line 141

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

.request(endpoint) ⇒ Object



98
99
100
101
102
103
# File 'lib/location.rb', line 98

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}"
  logger.debug url
  JSON.parse(RestClient.get(url))
end

.resetObject



105
106
107
108
109
110
111
112
# File 'lib/location.rb', line 105

def reset
  flattened.each do |l|
    Rails.cache.delete("#{CACHE_PRE}-id-#{l.id}")
    Rails.cache.delete("#{CACHE_PRE}-code-#{l.code}")
  end
  Rails.cache.delete("#{CACHE_PRE}_memos")
  Rails.cache.delete("#{CACHE_PRE}_request")
end

.tablesObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/location.rb', line 26

def tables
  @t   = nil if !testing? # test doesn't have a cache, so memoize in the test env
  @t ||= cache.fetch("#{CACHE_PRE}_memos") do
    t = { id_table: [], code_table: {} }
    flattened.sort_by(&:id).each do |l|
      t[:id_table][l.id]     = l
      t[:code_table][l.code] = l
    end
    t
  end
end

.testing?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/location.rb', line 137

def testing?
  defined?(Rails) && Rails.env.test?
end

.timeObject



129
130
131
# File 'lib/location.rb', line 129

def time
  Time.zone || Time
end

.transform_data(data) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/location.rb', line 114

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

  elsif data.kind_of? Hash
    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



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

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