class Cielo::API30::Request::CieloRequest

Attributes

merchant[RW]

Public Class Methods

new(merchant) click to toggle source
# File lib/cielo/api30/request/cielo_request.rb, line 14
def initialize(merchant)
  @merchant = merchant
end

Protected Instance Methods

send_request(method, uri, data=nil) click to toggle source
# File lib/cielo/api30/request/cielo_request.rb, line 19
def send_request(method, uri, data=nil)
  body = nil
  headers = {"User-Agent" => "CieloEcommerce/3.0 Ruby SDK",
             "RequestId" => UUIDTools::UUID.random_create.to_s,
             "MerchantId" => merchant.merchant_id,
             "MerchantKey" => merchant.merchant_key}

  if data.nil?
    headers["Content-Length"] = "0"
  else
    headers["Content-Type"] = "application/json"
    body = data.to_json
  end

  client = Net::HTTP.new(uri.host, uri.port)
  client.use_ssl = true

  response = client.send_request(method, uri.request_uri, body, headers)

  data = JSON.parse(response.body)

  raise CieloError.new(data.first["Code"], data.first["Message"]) if response.code.to_i >= 400

  data
end