class Paynl::Api::Api

Public Instance Methods

doRequest(endpoint, version = nil) click to toggle source
# File lib/paynl/api/api.rb, line 43
def doRequest(endpoint, version = nil)

  data = getData
  uri = Paynl::Config::getApiUrl(endpoint, version)
  #puts uri
  #puts data

  if isServiceIdRequired
    # Code to actually do the CURL request
    response = Typhoeus::Request.post(
        uri,
        params: data,
        userpwd: Paynl::Config::getTokenCode + ':' + Paynl::Config::getApiToken,
        :headers => { 'User-Agent' => 'Ruby SDK ' + VERSION }
    )          
  else
    # Code to actually do the CURL request
    response = Typhoeus::Request.post(
        uri,
        params: data,
        :headers => { 'User-Agent' => 'Ruby SDK ' + VERSION }
    )
  end

  # if response.code != 200
  #   raise 'API error'
  # end

  # puts response.code    # http status code
  # puts response.time    # time in seconds the request took
  # puts response.headers # the http headers
  # puts response.headers_hash # http headers put into a hash
  # puts response.body    # the response body

  output = processResult(JSON.parse(response.body))
  return output

end
getData() click to toggle source
# File lib/paynl/api/api.rb, line 19
def getData
  if isApiTokenRequired
    Paynl::Helper::requireApiToken

    @@data['token'] = Paynl::Config::getApiToken
  end

  if isServiceIdRequired
    Paynl::Helper::requireServiceId

    @@data['serviceId'] = Paynl::Config::getServiceId
  end

  return @@data
end
isApiTokenRequired() click to toggle source
# File lib/paynl/api/api.rb, line 10
def isApiTokenRequired
  return @apiTokenRequired
end
isServiceIdRequired() click to toggle source
# File lib/paynl/api/api.rb, line 15
def isServiceIdRequired
  return @serviceIdRequired
end
processResult(result) click to toggle source
# File lib/paynl/api/api.rb, line 35
def processResult(result)
  if result['request']['result'] != '1' and result['request']['result'] != 'TRUE'
    raise result['request']['errorId'] + ' - ' + result['request']['errorMessage']
  end

  return result
end