Loading...
 

RUBY Code Example


Sample Authorization Key Generation

This is a sample authorization that must be used in all requests against the API.

#Creates authorization header for a given method and URL.
  def self.createAuthorization(method, url)
    stringToSign = method + " " + removeServletName(url) + " " + @applicationKey
    hash = OpenSSL::HMAC.digest('sha256', @sharedSecret, stringToSign)
    requestSignature = Base64.encode64(hash).gsub(/\s+/, ' ').strip    
    credentials = @applicationKey + ":" + requestSignature;    
    encodedCredentials = Base64.encode64(credentials).gsub(/\s+/, ' ').strip
    return encodedCredentials
  end

  #Removes the servlet name from a given external API URL.
  def self.removeServletName(url)
    urlAsArray = url.split("/");
    newURL = ""
    for i in 4..(urlAsArray.size() - 1)
      newURL += "/" + urlAsArray[i]
    end
    return newURL
  end

Sample Get Request

APP_CONTENT_TYPE = "application/vnd.pncp.v1.0+json"
  AUTH_SCHEME = "PNCP "
  
  #Sends an HTTP GET request and returns response.
  def self.sendGETRequest(authInfo, url)
  
    self.runChecks
  
    begin
      
      @applicationKey = authInfo.getApplicationKey()
      @sharedSecret = authInfo.getSharedSecret()
    
      @url = URI.parse(url)
        
      #Setup SSL validation.
      http = Net::HTTP.new(@url.host, @url.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.cert_store = OpenSSL::X509::Store.new
      http.cert_store.set_default_paths
      http.cert_store.add_file(PEM_FILE_PATH)
       
      #Create and execute request
      request = Net::HTTP::Get.new(@url.request_uri)
      request['Accept'] = APP_CONTENT_TYPE  
      request['Authorization'] = AUTH_SCHEME + createAuthorization("GET", url)
      response = http.request(request)
      
      rescue Exception => e  
        raise "Error creating API request: " + e.message
      
    end    
    
    #Raise error if response is not successful.
    self.handleResponseError(response)
    
    #Return response.
    return response
  end

Sample POST Request

APP_CONTENT_TYPE = "application/vnd.pncp.v1.0+json"
  AUTH_SCHEME = "PNCP "

  #Sends an HTTP POST request and returns response.
  def self.sendPOSTRequest(authInfo, url, body)
    
    self.runChecks
    
    begin
    
      @applicationKey = authInfo.getApplicationKey()
      @sharedSecret = authInfo.getSharedSecret()
    
      @url = URI.parse(url)
        
      #Setup SSL validation.
      http = Net::HTTP.new(@url.host, @url.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.cert_store = OpenSSL::X509::Store.new
      http.cert_store.set_default_paths
      http.cert_store.add_file(PEM_FILE_PATH)
       
      #Create and execute request
      request = Net::HTTP::Post.new(@url.request_uri)
      request['Accept'] = APP_CONTENT_TYPE  
      request['Content-Type'] = APP_CONTENT_TYPE
      request['Authorization'] = AUTH_SCHEME + createAuthorization("POST", url)
      request.body = body
      response = http.request(request)
      
      rescue Exception => e  
        raise "Error creating API request: " + e.message    
    
    end
    
    #Raise error if response is not successful.
    self.handleResponseError(response)
    
    #Return response.
    return response
  end