From 6940934ccccc76988c5e75d6a3890309a37a9b60 Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Tue, 9 Apr 2019 15:58:37 +0200 Subject: [PATCH 1/3] Allow customizing faraday to add arbitrary headers --- README.md | 14 ++++++++++++++ lib/lurch/client.rb | 4 +++- lib/lurch/store.rb | 4 ++-- test/lurch/test_customize_faraday.rb | 22 ++++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/lurch/test_customize_faraday.rb diff --git a/README.md b/README.md index 9547101..e6cc063 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,20 @@ You can add an *Authorization* header to all your requests by configuring the st store = Lurch::Store.new("...", authorization: "Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOjEsIm5hbWUiOiJCb2IifQ.") ``` +## Customize Faraday + +You can customize [faraday](https://rubygems.org/gems/faraday) by passing a block to `Store.new`. +Use this to add arbitrary headers or faraday middlewares. + +```ruby +store = Lurch::Store.new("http://example.com/api") do |conn| + # conn.use MyFaradayMiddleware + conn.headers['X-Request-Id'] = '123' +end + +store.from(:people).all +``` + ## Contributing 1. Fork it () diff --git a/lib/lurch/client.rb b/lib/lurch/client.rb index bd65acb..8813360 100644 --- a/lib/lurch/client.rb +++ b/lib/lurch/client.rb @@ -11,9 +11,10 @@ class Client 422 => Errors::UnprocessableEntity }.freeze - def initialize(url, config) + def initialize(url, config, &block) @url = url @config = config + @customize_faraday = block if block_given? end def get(path) @@ -85,6 +86,7 @@ def client conn.request :jsonapi conn.response :jsonapi + @customize_faraday&.yield(conn) conn.adapter :typhoeus end diff --git a/lib/lurch/store.rb b/lib/lurch/store.rb index f58bd0a..974f31d 100644 --- a/lib/lurch/store.rb +++ b/lib/lurch/store.rb @@ -1,8 +1,8 @@ module Lurch class Store - def initialize(url, options = {}) + def initialize(url, options = {}, &block) @config = StoreConfiguration.new(options) - @client = Client.new(url, @config) + @client = Client.new(url, @config, &block) @store = Hash.new { |hash, key| hash[key] = {} } end diff --git a/test/lurch/test_customize_faraday.rb b/test/lurch/test_customize_faraday.rb new file mode 100644 index 0000000..cfa0cc1 --- /dev/null +++ b/test/lurch/test_customize_faraday.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative "../test_helper" + +class TestQueries < Minitest::Test + include LurchTest + + def test_customize_faraday + stub = stub_request( + :get, + "#{@url}/people" + ).with(headers: { 'X-Request-Id' => '123' }) + + store = Lurch::Store.new(@url) do |conn| + conn.headers['X-Request-Id'] = '123' + end + + store.from(:people).all + + assert_requested(stub) + end +end From 7a9ae8a8f3e0cce85b3d284b751581c93c37edcf Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Tue, 9 Apr 2019 15:59:19 +0200 Subject: [PATCH 2/3] Check TODO: Allow arbitrary headers --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 98ef1d3..3585501 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ * [x] Configurable field and path adapters (dasherized vs underscored vs ~~camelcased~~) * [x] Configurable pluralization/singularization (don't assume urls and types are always plural) * [x] Handle paginated results -* [ ] Allow arbitrary headers +* [x] Allow arbitrary headers * [ ] Allow arbitrary query params * [ ] Singleton resources * [ ] Handle links better? From 330b775d0c7fac2245bb102f16a6525bb51adb9c Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Fri, 12 Apr 2019 23:41:53 +0200 Subject: [PATCH 3/3] Rename test --- test/lurch/test_customize_faraday.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lurch/test_customize_faraday.rb b/test/lurch/test_customize_faraday.rb index cfa0cc1..f39b80c 100644 --- a/test/lurch/test_customize_faraday.rb +++ b/test/lurch/test_customize_faraday.rb @@ -2,7 +2,7 @@ require_relative "../test_helper" -class TestQueries < Minitest::Test +class TestCustomizeFaraday < Minitest::Test include LurchTest def test_customize_faraday