Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ jobs:

- name: Validate Base Ref
id: validate_base_ref
env:
JSON_ENV_VARS: ${{ inputs.credentials_json }}
shell: bash
run: |
if [ "${{ github.event.base_ref }}" != "refs/heads/main" ]; then
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 3.2
TargetRubyVersion: 3.3
Exclude:
- vendor/**/*
- spec/**/*
Expand Down
2 changes: 1 addition & 1 deletion fat_zebra.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.files = Dir.glob('lib/**/*') + %w[README.md fat_zebra.gemspec]
s.require_paths = ['lib']

s.required_ruby_version = '>= 3.2'
s.required_ruby_version = '>= 3.3'

s.add_development_dependency 'byebug'
s.add_development_dependency 'rake'
Expand Down
10 changes: 6 additions & 4 deletions lib/fat_zebra/api_operation/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ def save(params = {}, options = {})
path = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path
method = singleton_methods.include?(:id) ? :put : :post

params_for_save = to_hash.merge(params)

# Remove the id from the params to save, it should not be updated.
params_for_save.delete('id')
# Send each attribute once, whether it's keyed by a string or a symbol: the caller's
# value over the stored one, and the last of any the caller repeats, under that key.
# The id is never sent: it can't be updated.
params_for_save = [*to_hash, *params].reject { |key, _| key.to_s == 'id' }
.group_by { |key, _| key.to_s }
.to_h { |_, pairs| pairs.last }

response = request(method, path, params_for_save, options)
update_from(response)
Expand Down
5 changes: 4 additions & 1 deletion lib/fat_zebra/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def base_path
# @raise [FatZebra::RequestError] if the request is invalid
# rubocop:disable-next Metrics/AbcSize
def request(method, path, payload = {}, options = {})
payload[:test] = true if configurations.test_mode
if configurations.test_mode
payload.delete('test')
payload[:test] = true
end
payload = Util.format_dates_in_hash(payload)
url_params = Util.encode_parameters(payload) if method == :get
uri = build_endpoint_url(configurations.gateway, path, url_params, http_secure: configurations.http_secure)
Expand Down
2 changes: 1 addition & 1 deletion lib/fat_zebra/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def validate_type(field, options, params)
when :file_type
/\A\#<File:.*\z/
when :boolean
/\Atrue|false\z/
/\A(?:true|false)\z/
else
raise RequestValidationError, "Unknown type #{options} for #{field}"
end
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/fat_zebra/payment_plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,20 @@
it { expect(payment_plan).to be_a(FatZebra::PaymentPlan) }
end

describe '#suspend! payload' do
subject(:payment_plan) do
FatZebra::PaymentPlan.initialize_from('successful' => true, 'response' => { 'id' => '071-PP-1', 'status' => 'Active' })
end

before do
allow(FatZebra::PaymentPlan).to receive(:request).and_return('successful' => true, 'response' => {})
payment_plan.suspend!('new_status' => 'Cancelled')
end

it "sends the caller's new_status in place of the default" do
expect(FatZebra::PaymentPlan).to have_received(:request)
.with(:put, anything, { 'status' => 'Active', 'new_status' => 'Cancelled' }, {})
end
end

end
9 changes: 9 additions & 0 deletions spec/lib/fat_zebra/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
it { expect { klass.valid!({}, :create) }.to raise_error(FatZebra::RequestValidationError) }
end

describe 'type: :boolean' do
before { klass.validates(:flag, type: :boolean, on: :create) }

it { expect(klass.valid?({ flag: true }, :create)).to be_truthy }
it { expect(klass.valid?({ flag: false }, :create)).to be_truthy }
it { expect(klass.valid?({ flag: 'trueish' }, :create)).to be_falsey }
it { expect(klass.valid?({ flag: 'notfalse' }, :create)).to be_falsey }
end

end
56 changes: 56 additions & 0 deletions spec/lib/fat_zebra/web_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,60 @@
it { is_expected.to be_accepted }
end

describe '#update payload' do
subject(:web_hook) { FatZebra::WebHook.initialize_from(api_response('test hook')) }

def api_response(name)
{ 'successful' => true, 'response' => { 'id' => '071-WH-1', 'name' => name, 'mode' => 'Test' } }
end

context 'with a new value for an existing attribute and an id' do
before do
allow(FatZebra::WebHook).to receive(:request).and_return(api_response('New Test hook'))
web_hook.update(name: 'New Test hook', id: '071-WH-2')
end

it 'sends each attribute once, with the new value and no id' do
expect(FatZebra::WebHook).to have_received(:request)
.with(:put, anything, { name: 'New Test hook', 'mode' => 'Test' }, {})
end
end

context 'with test passed in test mode' do
before do
allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook')))
web_hook.update(name: 'New Test hook', test: true)
end

it 'sends test once' do
expect(FatZebra::Request).to have_received(:execute)
.with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true }))
end
end

context 'with test passed as a string in test mode' do
before do
allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook')))
web_hook.update(name: 'New Test hook', 'test' => true)
end

it 'sends test once' do
expect(FatZebra::Request).to have_received(:execute)
.with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true }))
end
end

context 'with one attribute passed as both a symbol and a string' do
before do
allow(FatZebra::WebHook).to receive(:request).and_return(api_response('Second name'))
web_hook.update(:name => 'First name', 'name' => 'Second name')
end

it 'sends it once, with the last value' do
expect(FatZebra::WebHook).to have_received(:request)
.with(:put, anything, { 'name' => 'Second name', 'mode' => 'Test' }, {})
end
end
end

end
Loading