module PagSeguro::Request

Public Instance Methods

get(path, api_version, data = {}, headers = {}) click to toggle source

Perform a GET request.

# path: the path that will be requested. Must be something like "transactions/code/739D69-79C052C05280-55542D9FBB33-CAB2B1". # api_version: the current PagSeguro API version of the requested service # data: the data that will be sent as query string. Must be a Hash. # headers: any additional header that will be sent through the request.

# File lib/pagseguro/request.rb, line 17
def get(path, api_version, data = {}, headers = {})
  execute :get, path, api_version, data, headers
end
get_with_auth_on_url(path, api_version, credentials) click to toggle source
# File lib/pagseguro/request.rb, line 21
def get_with_auth_on_url(path, api_version, credentials)
  request.public_send(
    :get,
    PagSeguro.api_url("#{api_version}/#{path}?#{credentials_to_params(credentials)}")
  )
end
get_without_api_version(path, data={}, headers={}) click to toggle source
# File lib/pagseguro/request.rb, line 28
def get_without_api_version(path, data={}, headers={})
  request.get(path, extended_data(data), headers)
end
post(path, api_version, data = {}, headers = {}) click to toggle source

Perform a POST request.

# path: the path that will be requested. Must be something like "checkout". # api_version: the current PagSeguro API version of the requested service # data: the data that will be sent as body data. Must be a Hash. # headers: any additional header that will be sent through the request.

# File lib/pagseguro/request.rb, line 39
def post(path, api_version, data = {}, headers = {})
  execute :post, path, api_version, data, headers
end
post_xml(path, api_version, credentials, data = '', options={}) click to toggle source

Perform a POST request, sending XML data.

# path: the path that will be requested. Must be something like "checkout". # api_version: the current PagSeguro API version of the requested service # credentials: the credentials like ApplicationCredentials or AccountCredentials. # data: the data that will be sent as body data. Must be a XML.

# File lib/pagseguro/request.rb, line 50
def post_xml(path, api_version, credentials, data = '', options={})
  credentials_params = credentials_to_params(credentials)
  url_path = [api_version, path].reject(&:nil?).join('/')

  request.post do
    url PagSeguro.api_url("#{url_path}?#{credentials_params}")
    headers "Content-Type" => "application/xml; charset=#{PagSeguro.encoding}"
    headers.merge!(options[:headers]) if options[:headers]
    body data
  end
end
put_xml(path, credentials, data) click to toggle source

Perform a PUT request, sending XML data.

# path: the path that will be requested. Must be something like "checkout". # credentials: the credentials like ApplicationCredentials or AccountCredentials. # data: the data that will be sent as body data. Must be a XML.

# File lib/pagseguro/request.rb, line 68
def put_xml(path, credentials, data)
  full_url = PagSeguro.api_url("#{path}?#{credentials_to_params(credentials)}")

  request.put do
    url full_url
    headers "Content-Type" => "application/xml; charset=#{PagSeguro.encoding}",
            "Accept" => "application/vnd.pagseguro.com.br.v1+xml;charset=ISO-8859-1"
    body data
  end
end

Private Instance Methods

credentials_object(data) click to toggle source
# File lib/pagseguro/request.rb, line 129
def credentials_object(data)
  credentials = data.delete(:credentials)
  if credentials.respond_to? :app_id
    {
      appId: credentials.app_id,
      appKey: credentials.app_key,
      authorizationCode: credentials.authorization_code
    }
  else
    {
      email: credentials.email,
      token: credentials.token
    }
  end
end
credentials_to_params(credentials) click to toggle source
# File lib/pagseguro/request.rb, line 122
def credentials_to_params(credentials)
  credentials_object(credentials: credentials)
    .delete_if { |_, value| value.nil? }
    .map { |key, value| "#{key}=#{value}" }
    .join('&')
end
extended_data(data) click to toggle source
# File lib/pagseguro/request.rb, line 95
def extended_data(data)
  if data[:credentials]
    data.merge!(credentials_object(data))
  else
    data.merge!(global_credentials(data))
  end
  data.merge!({ charset: PagSeguro.encoding })
  data.delete_if { |_, value| value.nil? }
end
extended_headers(request_method, headers) click to toggle source
# File lib/pagseguro/request.rb, line 105
def extended_headers(request_method, headers)
  headers.merge __send__("headers_for_#{request_method}")
end
global_credentials(data) click to toggle source
# File lib/pagseguro/request.rb, line 145
def global_credentials(data)
  if PagSeguro.app_id && PagSeguro.app_key
    {
      appId: PagSeguro.app_id,
      appKey: PagSeguro.app_key
    }
  else
    {
      email: data[:email] || PagSeguro.email,
      token: data[:token] || PagSeguro.token
    }
  end
end
headers_for_get() click to toggle source
# File lib/pagseguro/request.rb, line 116
def headers_for_get
  {
    "Accept-Charset" => PagSeguro.encoding
  }
end
headers_for_post() click to toggle source
# File lib/pagseguro/request.rb, line 109
def headers_for_post
  {
    "Accept-Charset" => PagSeguro.encoding,
    "Content-Type" => "application/x-www-form-urlencoded; charset=#{PagSeguro.encoding}"
  }
end
request() click to toggle source
# File lib/pagseguro/request.rb, line 91
def request
  @request ||= Aitch::Namespace.new
end