class MobicomCandy::Client

API Client Wrapper

Constants

GET
POST

Public Class Methods

new(token) click to toggle source

@param [String] token

# File lib/mobicom_candy/client.rb, line 8
def initialize(token)
  @token = token
  @last_sent = nil
end

Public Instance Methods

customer(customer_id, system) click to toggle source

@param [String] customer_id @param [String] system @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 23
def customer(customer_id, system)
  post('customer', customer: customer_id, 'customer.system' => system)
end
reward(customer_id, system, amount, description) click to toggle source

@param [String] customer_id @param [String] system @param [Decimal] amount @param [String] description @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 32
def reward(customer_id, system, amount, description)
  post('reward',
       'customer' => {
         value: customer_id,
         system: system
       },
       amount: amount,
       description: description
  )
end
sell(customer_id, system, amount, description: nil, sms_prefix: nil, sms_suffix: nil, product: nil, product_type: nil) click to toggle source

@param [String] customer_id @param [String] system @param [Decimal] amount @param [String] description @param [String] sms_prefix @param [String] sms_suffix @param [String] product @param [String] product_type @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 52
def sell(customer_id, system, amount, description: nil, sms_prefix: nil, sms_suffix: nil, product: nil, product_type: nil)
  post('sell',
       'customer' => {
         value: customer_id,
         system: system
       },
       amount: amount,
       description: description,
       smsPrefix: sms_prefix,
       smsSuffix: sms_suffix,
       product: product,
       productType: product_type
  )
end
sell_card(card_id, system, amount, pin, description: nil, sms_prefix: nil, sms_suffix: nil, product: nil, product_type: nil) click to toggle source

@param [String] card_id @param [String] system @param [Decimal] amount @param [String] pin @param [String] description @param [String] sms_prefix @param [String] sms_suffix @param [String] product @param [String] product_type @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 89
def sell_card(card_id, system, amount, pin, description: nil, sms_prefix: nil, sms_suffix: nil, product: nil, product_type: nil)
  post('sellcard',
       'customer' => {
         value: card_id,
         system: system
       },
       amount: amount,
       pin: pin,
       description: description,
       smsPrefix: sms_prefix,
       smsSuffix: sms_suffix,
       product: product,
       productType: product_type
  )
end
sell_confirm(customer_id, system, amount, tancode) click to toggle source

@param [String] customer_id @param [String] system @param [Decimal] amount @param [String] tancode @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 72
def sell_confirm(customer_id, system, amount, tancode)
  post('sellconfirm', 'customer' => {
    value: customer_id,
    system: system
  }, amount: amount, tancode: tancode)
end
transaction(limit = 10, offset = 0) click to toggle source

@param [Integer] limit @param [Integer] offset @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 16
def transaction(limit = 10, offset = 0)
  get('transaction', limit: limit, offset: offset)
end

Private Instance Methods

get(fn, params = {}) click to toggle source

@param fn [String] API Endpoint @param [Hash] params @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 116
def get(fn, params = {})
  request("/resource/partner/v1/#{fn}", params: params, type: GET)
end
host() click to toggle source
# File lib/mobicom_candy/client.rb, line 154
def host
  'api.candy.mn'
end
json_body?() click to toggle source
# File lib/mobicom_candy/client.rb, line 150
def json_body?
  true
end
parse(response, &block) click to toggle source

@param [String] response @param [Object] block

# File lib/mobicom_candy/client.rb, line 160
def parse(response, &block)
  case response
    when Net::HTTPNoContent
      :no_content
    when Net::HTTPSuccess
      parse_success(response, &block)
    when Net::HTTPUnauthorized
      raise AuthenticationError, "#{response.code} response with: #{response.body}"
    when Net::HTTPClientError
      raise ClientError, "#{response.code} response with: #{response.body}"
    when Net::HTTPServerError
      raise ServerError, "#{response.code} response with: #{response.body}"
    else
      raise Error, "#{response.code} response from #{host}"
  end
end
parse_success(response) { |response| ... } click to toggle source
# File lib/mobicom_candy/client.rb, line 177
def parse_success(response)
  if response['Content-Type'].split(';').first == 'application/json'
    JSON.parse(response.body, object_class: MobicomCandy::Entity)
  elsif block_given?
    yield response
  else
    response.body
  end
end
post(fn, params = {}) click to toggle source

@param fn [String] API Endpoint @param [Hash] params @return [MobicomCandy::Entity]

# File lib/mobicom_candy/client.rb, line 109
def post(fn, params = {})
  request("/resource/partner/v1/#{fn}", params: params, type: POST)
end
request(path, params: nil, type: GET, &block) click to toggle source

@param [String] path @param [Hash] params @param [Enum] type @param [Function] block

# File lib/mobicom_candy/client.rb, line 124
def request(path, params: nil, type: GET, &block)
  sleep(1) if @last_sent && @last_sent > (Time.now - 1000)
  @last_sent = Time.now
  uri = URI('https://' + host + path)
  unless type::REQUEST_HAS_BODY || params.nil? || params.empty?
    uri.query = Params.encode(params)
  end
  message = type.new(uri.request_uri)
  message['Content-Type'] = 'application/json'
  if type::REQUEST_HAS_BODY
    if json_body?
      message.body = JSON.generate(params)
    else
      message.form_data = params
    end
  end
  message['Authorization'] = "Bearer #{@token}"
  http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
  http.use_ssl = true
  response = http.request(message)
  parse(response, &block)
end