Class: T2Airtime::Product

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

Class Method Summary collapse

Class Method Details

.serialize(data, qty = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/t2_airtime/serializer.rb', line 112

def self.serialize(data, qty=nil)
     currency = data[:destination_currency]
     retail_prices = data[:retail_price_list].split(",") 
     wholesale_prices = data[:wholesale_price_list].split(",")
     ids = data[:product_list].split(",")
     ids.take(qty.nil? ? ids.count : qty).each_with_index.map{|id, n| {
       country: data[:country], 
       country_aid: Integer(data[:countryid]),
       alpha3: T2Airtime::Country.alpha3(data[:country]),
       operator: data[:operator], 
       operator_aid: Integer(data[:operatorid]),  
       operator_logo_url: T2Airtime::Util.operator_logo_url(data[:operatorid]),         
       name: "#{id}#{currency}", 
       currency: currency,
       local_price: Float(id),
       retail_price: Float(retail_prices[n]),
       wholesale_price: Float(wholesale_prices[n]),           
       local_price_display: T2Airtime::Util.format_price(id, currency),
       retail_price_display: T2Airtime::Util.format_price(retail_prices[n]),
       wholesale_price_display: T2Airtime::Util.format_price(wholesale_prices[n]),
       aid: Integer(id)
     }}
end

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



102
103
104
105
106
107
108
109
110
# File 'lib/t2_airtime/serializer.rb', line 102

def self.take(operator_qty=1, qty=5)
    operators = T2Airtime::Operator.take(operator_qty).shuffle
    unless operators.empty?
      operators.flat_map{|operator| (
        reply = T2Airtime::API.api.product_list(operator[:aid])
        reply.success? ? serialize(reply.data, qty) : []
      )}        
    end         
end