View all our code samples

Using Faraday in Ruby

Faraday is an HTTP client library for Ruby that provides a flexible and modular API for making HTTP requests and handling responses. It supports middleware, which allows developers to customize the behavior of requests and responses. Faraday is commonly used for interacting with web services and consuming APIs in Ruby applications.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and Ruby:

require 'faraday'
require 'json'

def convert(api_key, params, endpoint = 'pdf')
  # Ensure the endpoint is valid
  unless %w[pdf png jpg webp].include?(endpoint)
    raise ArgumentError, 'Invalid endpoint specified'
  end

  # Set the API URL
  url = "https://api.pdfshift.io/v3/convert/#{endpoint}"

  # Create a Faraday connection
  conn = Faraday.new(url: url) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end

  # Make the POST request
  response = conn.post do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{Base64.strict_encode64("api:#{api_key}")}"
    req.body = params.to_json
  end

  # Check for successful response
  unless response.success?
    raise "Request failed with status code #{response.status}: #{response.body}"
  end

  # Return the response based on the presence of filename or webhook
  response_body.key?('filename') || response_body.key?('webhook') ? JSON.parse(response.body) : response.body
end

Here's how you can use the above code:

begin
  result = convert('sk_XXXXXXXXXXXXXX', {'source' => 'https://en.wikipedia.org/wiki/REST'})
  puts result
rescue StandardError => e
  puts "Error: #{e.message}"
end

We've tested this code with the latest version of Faraday and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.