Skip to content

Instantly share code, notes, and snippets.

@simeonwillbanks
Last active January 25, 2023 17:49

Revisions

  1. simeonwillbanks revised this gist Jan 25, 2023. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions client.rb
    Original file line number Diff line number Diff line change
    @@ -24,8 +24,9 @@ def connection
    # the RaiseError middleware raises a Faraday::Error exception
    f.response :raise_error

    # https://honeyryderchuck.gitlab.io/httpx/wiki/Faraday-Adapter
    # adds the adapter to the connection
    # https://gitlab.com/os85/httpx#what-makes-it-the-best-ruby-http-client
    # https://os85.gitlab.io/httpx/
    # https://os85.gitlab.io/httpx/wiki/home.html
    f.adapter :httpx
    end
    end
  2. simeonwillbanks created this gist Jan 25, 2023.
    43 changes: 43 additions & 0 deletions client.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # frozen_string_literal: true
    require "faraday"
    require "faraday/retry"
    require "httpx/adapters/faraday"

    module App
    class Client
    def connection
    Faraday.new(url) do |f|
    # https://github.com/lostisland/faraday-retry#usage
    # Retry transient failures
    # By default, it retries 2 times and handles only timeout exceptions
    f.request :retry

    # https://lostisland.github.io/faraday/middleware/authentication
    # Automatically add an Authorization header to your requests
    f.request :authorization, "Bearer", -> { auth_token }

    f.request :json # encode req bodies as JSON and automatically set the Content-Type header
    f.response :json # decode response bodies as JSON

    # https://lostisland.github.io/faraday/middleware/raise-error
    # If an HTTP response returns with a 4xx or 5xx status code
    # the RaiseError middleware raises a Faraday::Error exception
    f.response :raise_error

    # https://honeyryderchuck.gitlab.io/httpx/wiki/Faraday-Adapter
    # adds the adapter to the connection
    f.adapter :httpx
    end
    end

    private

    def auth_token
    ENV["AUTH_TOKEN"]
    end

    def url
    "https://example.com"
    end
    end
    end
    15 changes: 15 additions & 0 deletions resource.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # frozen_string_literal: true

    module App
    class Resource
    attr_reader :connection

    def initialize(connection: Client.new.connection)
    @connection = connection
    end

    def get(id:)
    connection.get("/resources/#{id}")
    end
    end
    end
    17 changes: 17 additions & 0 deletions test_client.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # frozen_string_literal: true

    require "test_helper"

    class TestAppResource < Minitest::Test
    def setup
    @resource = App::Resource.new
    end

    def test_get
    VCR.use_cassette("test_get") do
    id = 1
    response = @resource.get(id: id)
    assert_equal id, response.body["id"]
    end
    end
    end
    11 changes: 11 additions & 0 deletions test_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # frozen_string_literal: true

    $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
    require "app"
    require "minitest/autorun"
    require "vcr"

    VCR.configure do |c|
    c.cassette_library_dir = "test/vcr"
    c.hook_into :faraday
    end