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



98
99
100
101
102
# File 'lib/t2_airtime/serializer.rb', line 98

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 = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/t2_airtime/serializer.rb', line 115

def self.serialize(data, ts = Time.zone.now.to_s, qty = nil)
  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.nil? ? 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(country_qty = 1, qty = 5) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/t2_airtime/serializer.rb', line 104

def self.take(country_qty = 1, qty = 5)
  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