Skip to content

Commit

Permalink
Update to work with new Vertebrae and Faraday v2 middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
woodhull committed May 10, 2023
1 parent fc81b60 commit e788ae9
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2
3.2.2
4 changes: 2 additions & 2 deletions action_network_rest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.6'

spec.add_runtime_dependency 'vertebrae', '~> 0.6.0'
spec.add_runtime_dependency 'vertebrae', '>= 1.0.0'

spec.add_development_dependency 'bundler', '~> 2.1'
spec.add_development_dependency 'byebug', '~> 11.1'
spec.add_development_dependency 'debug'
spec.add_development_dependency 'dotenv', '~> 2.7'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
Expand Down
16 changes: 7 additions & 9 deletions lib/action_network_rest/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
module ActionNetworkRest
class API < Vertebrae::API
def setup
connection.stack do |builder|
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
connection.faraday_connection = Faraday.new(connection.configuration.faraday_options) do |f|
f.request :multipart
f.request :url_encoded

builder.use Faraday::Response::Logger if ENV['DEBUG']
f.response :mashify
f.response :json

builder.use FaradayMiddleware::Mashify
builder.use FaradayMiddleware::ParseJson

builder.use ActionNetworkRest::Response::RaiseError
builder.adapter connection.configuration.adapter
f.response :actionnetwork_raise_error
f.adapter connection.configuration.adapter
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/action_network_rest/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def all
wait = min_wait * (1.5**tries)
new_time = Time.now.to_r
if (timestamp + min_wait - new_time).positive?
sleep(wait) # Wait if calling again could excede the rate limit
sleep(wait) # Wait if calling again could exceed the rate limit
tries += 1
next # check again
end
Expand All @@ -46,7 +46,7 @@ def all
end

sleep(wait)
tries += 1 # Exponential back off if got Too Many Requests error reponse
tries += 1 # Exponential back off if got Too Many Requests error response
next
end
timestamp = Time.now.to_r
Expand Down
4 changes: 3 additions & 1 deletion lib/action_network_rest/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActionNetworkRest
module Response
class RaiseError < Faraday::Response::Middleware
class RaiseError < Faraday::Middleware
# rubocop:disable Style/GuardClause
def on_complete(response)
status_code = response[:status].to_i
Expand Down Expand Up @@ -44,3 +44,5 @@ class TooManyRequests < StandardError; end
class UsedAllRequestTries < StandardError; end
end
end

Faraday::Response.register_middleware actionnetwork_raise_error: -> { ActionNetworkRest::Response::RaiseError }
2 changes: 1 addition & 1 deletion lib/action_network_rest/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActionNetworkRest
VERSION = '0.11.0'
VERSION = '1.0.0'
end
10 changes: 5 additions & 5 deletions spec/advocacy_campaigns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

before :each do
stub_actionnetwork_request("/advocacy_campaigns/#{advocacy_campaign_id}", method: :get)
.to_return(status: status, body: response_body)
.to_return(status: status, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve advocacy_campaign data' do
Expand Down Expand Up @@ -399,7 +399,7 @@
context 'requesting first page' do
before :each do
stub_actionnetwork_request('/advocacy_campaigns/?page=1', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve first page when callig with argument' do
Expand All @@ -420,7 +420,7 @@
context 'requesting page 3 advocacy_campaigns' do
before :each do
stub_actionnetwork_request('/advocacy_campaigns/?page=3', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retreive the advocacy_campaigns data from page 3' do
Expand Down Expand Up @@ -570,7 +570,7 @@
end
let!(:post_stub) do
stub_actionnetwork_request('/advocacy_campaigns/', method: :post, body: request_body)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should POST advocacy_campaign data' do
Expand Down Expand Up @@ -724,7 +724,7 @@
stub_actionnetwork_request("/advocacy_campaigns/#{advocacy_campaign_id}",
method: :put,
body: advocacy_campaign_data)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should PUT advocacy_campaign data' do
Expand Down
4 changes: 2 additions & 2 deletions spec/attendances_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

let!(:post_stub) do
stub_actionnetwork_request("/events/#{event_id}/attendances/", method: :post, body: request_body)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should POST signature data' do
Expand Down Expand Up @@ -62,7 +62,7 @@
let!(:post_stub) do
stub_actionnetwork_request("/event_campaigns/#{event_campaign_id}/events/#{event_id}/attendances/",
method: :post, body: request_body)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should POST signature data to the prefixed path' do
Expand Down
6 changes: 3 additions & 3 deletions spec/campaigns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

before :each do
stub_actionnetwork_request("/campaigns/#{campaign_id}", method: :get)
.to_return(status: status, body: response_body)
.to_return(status: status, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve campaign data' do
Expand Down Expand Up @@ -176,7 +176,7 @@
context 'requesting first page' do
before :each do
stub_actionnetwork_request('/campaigns/?page=1', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve first page when callig with argument' do
Expand All @@ -197,7 +197,7 @@
context 'requesting page 3 campaigns' do
before :each do
stub_actionnetwork_request('/campaigns/?page=3', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retreive the campaigns data from page 3' do
Expand Down
6 changes: 4 additions & 2 deletions spec/entry_point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
let(:response_body) { { some: 'data' }.to_json }

before :each do
stub_actionnetwork_request('/', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should return the response' do
Expand All @@ -21,7 +22,8 @@

describe '#authenticated_successfully?' do
before :each do
stub_actionnetwork_request('/', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

context 'response includes tags' do
Expand Down
12 changes: 7 additions & 5 deletions spec/event_campaigns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
let(:response_body) { { some: 'data' }.to_json }

before :each do
stub_actionnetwork_request('/event_campaigns/123', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/event_campaigns/123', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should return the response' do
Expand All @@ -23,7 +24,8 @@
let(:response_body) { fixture('event_campaigns/list.json') }

it 'should return the response' do
stub_actionnetwork_request('/event_campaigns/?page=1', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/event_campaigns/?page=1', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })

expect(subject.event_campaigns.list.length).to eq(2)
expect(subject.event_campaigns.list.first)
Expand All @@ -32,8 +34,8 @@
end

it 'should paginate' do
stub_request = stub_actionnetwork_request('/event_campaigns/?page=2', method: :get).to_return(status: 200,
body: response_body)
stub_request = stub_actionnetwork_request('/event_campaigns/?page=2', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
subject.event_campaigns.list(page: 2)
expect(stub_request).to have_been_requested
end
Expand All @@ -57,7 +59,7 @@
end
let!(:put_stub) do
stub_actionnetwork_request("/event_campaigns/#{campaign_id}", method: :put, body: campaign_data)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should PUT event campaign data' do
Expand Down
18 changes: 10 additions & 8 deletions spec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
let(:response_body) { { some: 'data' }.to_json }

before :each do
stub_actionnetwork_request('/events/123', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/events/123', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should return the response' do
Expand All @@ -23,24 +24,25 @@
let(:response_body) { fixture('events/list.json') }

it 'should return the response' do
stub_actionnetwork_request('/events/?page=1', method: :get).to_return(status: 200, body: response_body)
stub_actionnetwork_request('/events/?page=1', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })

expect(subject.events.list.length).to eq(2)
expect(subject.events.list.first).to include({ 'action_network_id' => '8a625981-67a4-4457-8b55-2e30b267b2c2' })
end

it 'should support event_campaign listing' do
stub_request = stub_actionnetwork_request('/event_campaigns/foo/events/?page=1',
method: :get).to_return(status: 200, body: response_body)
stub_request = stub_actionnetwork_request('/event_campaigns/foo/events/?page=1', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })

subject.event_campaigns('foo').events.list

expect(stub_request).to have_been_requested
end

it 'should paginate' do
stub_request = stub_actionnetwork_request('/events/?page=2', method: :get).to_return(status: 200,
body: response_body)
stub_request = stub_actionnetwork_request('/events/?page=2', method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
subject.events.list(page: 2)
expect(stub_request).to have_been_requested
end
Expand All @@ -64,7 +66,7 @@
end
let!(:post_stub) do
stub_actionnetwork_request('/events/', method: :post, body: request_body)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should POST event data' do
Expand Down Expand Up @@ -169,7 +171,7 @@
end
let!(:put_stub) do
stub_actionnetwork_request("/events/#{event_id}", method: :put, body: event_data)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should PUT event data' do
Expand Down
10 changes: 5 additions & 5 deletions spec/forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

before :each do
stub_actionnetwork_request("/forms/#{form_id}", method: :get)
.to_return(status: status, body: response_body)
.to_return(status: status, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve form data' do
Expand Down Expand Up @@ -396,7 +396,7 @@
context 'requesting first page' do
before :each do
stub_actionnetwork_request('/forms/?page=1', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retrieve first page when calling without page as argument' do
Expand All @@ -417,7 +417,7 @@
context 'requesting page 3 forms' do
before :each do
stub_actionnetwork_request('/forms/?page=3', method: :get)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should retreive the forms data from page 3' do
Expand Down Expand Up @@ -563,7 +563,7 @@
end
let!(:post_stub) do
stub_actionnetwork_request('/forms/', method: :post, body: request_body)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should POST form data' do
Expand Down Expand Up @@ -711,7 +711,7 @@
end
let!(:put_stub) do
stub_actionnetwork_request("/forms/#{form_id}", method: :put, body: form_data)
.to_return(status: 200, body: response_body)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

it 'should PUT form data' do
Expand Down

0 comments on commit e788ae9

Please sign in to comment.