Class: T2Airtime::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/t2_airtime/serializer.rb

Class Method Summary collapse

Class Method Details

.all(country_id) ⇒ Object



177
178
179
180
181
# File 'lib/t2_airtime/serializer.rb', line 177

def self.all(country_id)
  Rails.cache.fetch("operators/#{country_id}", expires_in: 1.hour) do
    T2Airtime::API.api.operator_list(country_id)
  end
end

.serialize(data, ts = Time.zone.now.to_s, qty = 'all') ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/t2_airtime/serializer.rb', line 194

def self.serialize(data, ts = Time.zone.now.to_s, qty = 'all')
  return [] if data[:operator].nil?
  names = data[:operator].split(',')
  ids = data[:operatorid].split(',')
  Rails.cache.fetch("operators/#{data[:countryid]}/serializer", expires_in: 1.hour) do
    ids.take(qty==='all' ? ids.count : qty).each_with_index.map do |id, n|
      {
        type: 'operators',
        id: Integer(id),
        attributes: {
          name: names[n],
          logo: T2Airtime::Util.operator_logo_url(id),
          countryId: Integer(data[:countryid]), 
          countryName: data[:country],
          countryAlpha3: T2Airtime::Country.alpha3(data[:country]),              
          fetchedAt: T2Airtime::Util.format_time(ts)
        },
        relationships: {
          country: {
            data: {
              type: 'countries',
              id: Integer(data[:countryid])
            }
          }
        }
      }
    end
  end
end

.take(qty = 5, country_qty = 1) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/t2_airtime/serializer.rb', line 183

def self.take(qty = 5, country_qty = 1)
  countries = T2Airtime::Country.take(country_qty).shuffle
  unless countries.empty?
    countries.flat_map do |country| (          
      operators = all(country[:id])
      operators.success? ? serialize(operators.data, operators.headers[:date], qty) : []
    )
    end
  end
end