Skip to content

Commit

Permalink
Add after_response callback (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sos4nt authored Nov 6, 2023
1 parent e119b98 commit ca292cc
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### next

* Moved the development dependencies from the gemspec to the Gemfile (#19)
* Introduce `after_response` callback (#22)

### 1.1.0

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Billomat.configure do |config|
# https://www.billomat.com/en/api/basics/rate-limiting/
config.app_id = '12345'
config.app_secret = 'c3df...'

config.after_response = lambda do |response|
# API response callback, e.g. for inspecting the rate limit
# header via response.headers[:x_rate_limit_remaining]
end
end
```

Expand Down
12 changes: 12 additions & 0 deletions lib/billomat/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ module Billomat
# The +billomat+ gem configuration.
class Configuration
attr_accessor :api_key, :subdomain, :timeout, :app_id, :app_secret
attr_reader :after_response

# Sets a callback to be called for each API response
#
# @param [Proc] callback The callback
def after_response=(callback)
unless callback.respond_to?(:call)
raise ArgumentError, "callback must respond to `call'"
end

@after_response = callback
end
end
end
2 changes: 1 addition & 1 deletion lib/billomat/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def response
timeout: timeout,
headers: headers,
payload: body.to_json
)
).tap { |response| Billomat.configuration.after_response&.call(response) }
end

# @return [String] the complete URL for the request
Expand Down
19 changes: 19 additions & 0 deletions spec/billomat/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Billomat::Configuration do
let(:configuration) { described_class.new }

describe '.after_response=' do
it 'sets after_response' do
callback = proc {}
configuration.after_response = callback
expect(configuration.after_response).to eq(callback)
end

it 'raises an error when assigned a non proc' do
expect { configuration.after_response = :foo }.to raise_error(ArgumentError)
end
end
end
9 changes: 9 additions & 0 deletions spec/billomat/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
end

describe '#run' do
let(:callback) { instance_double(Proc) }

context 'when API Call is successful' do
let(:execute_args) do
{
Expand All @@ -52,6 +54,13 @@

gateway.new(:get, '/clients', foo: 'bar').run
end

it 'invokes after_response callback with response' do
expect(callback).to receive(:call).with(good_response)

Billomat.configuration.after_response = callback
gateway.new(:get, '/clients', foo: 'bar').run
end
end

context 'when API Call is not successful' do
Expand Down

0 comments on commit ca292cc

Please sign in to comment.