module T2Airtime class AirtimeController < ActionController::API def countries reply = T2Airtime::API.api.country_list if reply.success? data = T2Airtime::Country.serialize(reply.data) render json: { links: { self: request.path }, data: data, status: :ok } else render_error(T2Airtime::Error.new(reply.error_code, reply.error_message)) end end def operators reply = T2Airtime::API.api.operator_list(params[:country_aid]) if reply.success? data = T2Airtime::Operator.serialize(reply.data) render json: { operators: data, status: :ok } else render_error(T2Airtime::Error.new(reply.error_code, reply.error_message)) end end def products reply = T2Airtime::API.api.product_list(params[:operator_aid]) if reply.success? data = T2Airtime::Product.serialize(reply.data) render json: { products: data, status: :ok } else render_error(T2Airtime::Error.new(reply.error_code, reply.error_message)) end end def transactions reply = T2Airtime::API.api.trans_list(params[:start], params[:stop], params[:msisdn], params[:destination], params[:code]) if reply.success? data = T2Airtime::Transaction.serialize(reply.data) render json: { transactions: data, status: :ok } else render_error(T2Airtime::Error.new(reply.error_code, reply.error_message)) end end def transaction reply = T2Airtime::API.api.trans_info(params[:id]) if reply.success? data = T2Airtime::Transaction.serialize_one(reply.data) render json: { transaction: data, status: :ok } else render_error(T2Airtime::Error.new(reply.error_code, reply.error_message)) end end protected def render_error(error) render json: { errors: [{ code: error.code, detail: error.message }], status: :bad_request } end end end