Skip to content

Instantly share code, notes, and snippets.

@leopolicastro
Created April 29, 2022 20:13
Show Gist options
  • Save leopolicastro/91b52186fc6c75854bcceef9d7f7364a to your computer and use it in GitHub Desktop.
Save leopolicastro/91b52186fc6c75854bcceef9d7f7364a to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# Plain Old Ruby Object
# Not backed by ActiveRecord
class RequestsClient
# client class
attr_reader :options, :adapter, :base_url
def initialize(base_url, options: {}, adapter: Faraday.default_adapter)
@options = default_request_options.merge(options)
@adapter = adapter
@base_url = base_url
end
def connection
@connection ||= Faraday.new(url: @base_url) do |faraday|
faraday.url_prefix = @base_url
faraday.request :json
faraday.response :json, content_type: "application/json"
faraday.adapter @adapter
faraday.headers = @options[:headers]
end
end
def default_request_options
{
headers: {"Content-Type": "application/json"}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment