Skip to content

Instantly share code, notes, and snippets.

@dazza-codes
Created February 13, 2016 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazza-codes/89efe92d43449c07c729 to your computer and use it in GitHub Desktop.
Save dazza-codes/89efe92d43449c07c729 to your computer and use it in GitHub Desktop.
Faraday JSON client
require 'faraday'
require 'faraday_middleware'
class Client
JSON_CONTENT = 'application/json'
attr_reader :conn
# Initialize a new client
def initialize
@base_uri = 'https://api.example.org'
@api_path = '/v1/xyz'
@conn = Faraday.new(url: @base_uri) do |conn|
# conn.use FaradayMiddleware::FollowRedirects, limit: 3
# conn.use Faraday::Response::RaiseError # raise exceptions on 40x, 50x
# conn.request :logger, @config.logger
conn.request :retry, max: 2,
interval: 0.5,
interval_randomness: 0.5,
backoff_factor: 2
# conn.request :url_encoded
conn.request :json
conn.response :json, :content_type => JSON_CONTENT
conn.adapter :httpclient
end
@conn.options.timeout = 90
@conn.options.open_timeout = 10
#@conn.headers.merge!(json_payloads)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment