class T2Airtime::Operator
Public Class Methods
all(country_id)
click to toggle source
# 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')
click to toggle source
# 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)
click to toggle source
# 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