[{"data":1,"prerenderedAt":77},["Reactive",2],{"/samples/ruby/faraday":3,"related:Q5NWkMDk6w":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":8,"link":11,"body":12,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/samples/ruby/faraday","ruby",false,"","Faraday","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.","Ruby","https://lostisland.github.io/faraday/",{"type":13,"children":14,"toc":51},"root",[15,30,42],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20,28],{"type":16,"tag":21,"props":22,"children":25},"a",{"href":11,"rel":23},[24],"nofollow",[26],{"type":27,"value":8},"text",{"type":27,"value":29}," 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.",{"type":16,"tag":31,"props":32,"children":36},"pre",{"className":33,"code":35,"language":5,"meta":7},[34],"language-ruby","require 'faraday'\nrequire 'json'\n\ndef convert(api_key, params, endpoint = 'pdf')\n  # Ensure the endpoint is valid\n  unless %w[pdf png jpg webp].include?(endpoint)\n    raise ArgumentError, 'Invalid endpoint specified'\n  end\n\n  # Set the API URL\n  url = \"https://api.pdfshift.io/v3/convert/#{endpoint}\"\n\n  # Create a Faraday connection\n  conn = Faraday.new(url: url) do |faraday|\n    faraday.request :url_encoded\n    faraday.adapter Faraday.default_adapter\n  end\n\n  # Make the POST request\n  response = conn.post do |req|\n    req.headers['Content-Type'] = 'application/json'\n    req.headers['X-API-Key'] = api_key\n    req.body = params.to_json\n  end\n\n  # Check for successful response\n  unless response.success?\n    raise \"Request failed with status code #{response.status}: #{response.body}\"\n  end\n\n  # Return the response based on the presence of filename or webhook\n  response_body.key?('filename') || response_body.key?('webhook') ? JSON.parse(response.body) : response.body\nend\n",[37],{"type":16,"tag":38,"props":39,"children":40},"code",{"__ignoreMap":7},[41],{"type":27,"value":35},{"type":16,"tag":31,"props":43,"children":46},{"className":44,"code":45,"language":5,"meta":7},[34],"begin\n  result = convert('sk_XXXXXXXXXXXXXX', {'source' => 'https://en.wikipedia.org/wiki/REST'})\n  puts result\nrescue StandardError => e\n  puts \"Error: #{e.message}\"\nend\n",[47],{"type":16,"tag":38,"props":48,"children":49},{"__ignoreMap":7},[50],{"type":27,"value":45},{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:samples:ruby:faraday.md","content","samples/ruby/faraday.md","md",[60,61,65,69,73],{"_path":4,"language":10,"library":8,"_id":55},{"_path":62,"language":10,"library":63,"_id":64},"/samples/ruby/httpparty","HttpParty","content:samples:ruby:httpparty.md",{"_path":66,"language":10,"library":67,"_id":68},"/samples/ruby/nethttp","Net::HTTP","content:samples:ruby:nethttp.md",{"_path":70,"language":10,"library":71,"_id":72},"/samples/ruby/restclient","RestClient","content:samples:ruby:restclient.md",{"_path":74,"language":10,"library":75,"_id":76},"/samples/ruby/typhoeus","Typhoeus","content:samples:ruby:typhoeus.md",1785426823470]