Skip to content

Commit

Permalink
Merge pull request #35 from ahx/customize-faraday
Browse files Browse the repository at this point in the history
Allow to pass a block to Store.new to customize faraday
  • Loading branch information
doughsay authored Apr 12, 2019
2 parents 8b0e189 + 330b775 commit 07d56e0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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 (<https://github.com/peek-travel/lurch/fork>)
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* [x] Allow arbitrary query params
* [ ] Singleton resources
* [ ] Handle links better?
Expand Down
4 changes: 3 additions & 1 deletion lib/lurch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,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)
Expand Down Expand Up @@ -87,6 +88,7 @@ def client

conn.request :jsonapi
conn.response :jsonapi
@customize_faraday&.yield(conn)

conn.adapter :typhoeus
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lurch/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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

Expand Down
22 changes: 22 additions & 0 deletions test/lurch/test_customize_faraday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require_relative "../test_helper"

class TestCustomizeFaraday < 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

0 comments on commit 07d56e0

Please sign in to comment.