Created
April 29, 2022 20:13
-
-
Save leopolicastro/43cea7f65fa83caac3445442480f75d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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