Skip to content
Open
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: 1 addition & 1 deletion app/controllers/cangaroo/endpoint_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def handle_request
def ensure_json_request
return if request.headers['Content-Type'] == 'application/json'

render nothing: true, status: 406
head 406
end

def key
Expand Down
3 changes: 2 additions & 1 deletion app/interactors/cangaroo/perform_flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class PerformFlow

organize ValidateJsonSchema,
CountJsonObject,
PerformJobs
PerformJobs,
PersistParameters
end
end
40 changes: 40 additions & 0 deletions app/interactors/cangaroo/persist_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Cangaroo
class PersistParameters
include Interactor

def call
return if request_params.empty? || new_params.empty?
unless connection.update(parameters: new_params)
fail_context!
end
end

private

def new_params
persisted_params
.with_indifferent_access
.merge(request_params)
.slice(*persisted_params.keys)
end

def connection
context.flow.send(:destination_connection)
end

def persisted_params
connection.parameters
end

def request_params
context.parameters.to_h.select{|k,v| v.present?}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.
Unused block argument - k. If it's necessary, use _ or _k as an argument name to indicate that it won't be used.
Space missing after comma.
Space missing inside }.

end

def fail_context!
context.fail!(
message: "could not update #{context.flow.class.name} parameters: #{connection.errors.full_messages.to_sentence}",
error_code: 500
)
end
end
end
1 change: 1 addition & 0 deletions app/jobs/cangaroo/base_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def restart_flow(response)
return unless process_response

command = Cangaroo::PerformFlow.call(
flow: self,
source_connection: destination_connection,
json_body: response,
jobs: Rails.configuration.cangaroo.jobs.reject{ |job| job == self.class }
Expand Down
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
require "rubygems"
require "bundler/setup"

load Gem.bin_path("factory_girl_rails", "setup")
load Gem.bin_path("factory_bot_rails", "setup")
2 changes: 1 addition & 1 deletion cangaroo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'appraisal'
s.add_development_dependency 'codeclimate-test-reporter'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'factory_bot_rails'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec-rails'
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/cangaroo_connections.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'securerandom'

FactoryGirl.define do
FactoryBot.define do
factory :cangaroo_connection, class: 'Cangaroo::Connection' do
name :store
url 'www.store.com'
Expand Down
27 changes: 27 additions & 0 deletions spec/fixtures/json_payload_parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"orders":[
{
"id":"O154085346172",
"state":"cart"
},
{
"id":"O154085343224",
"state":"payed"
}
],
"shipments":[
{
"id":"S53454325",
"state":"shipped"
},
{
"id":"S53565543",
"state":"waiting"
}
],
"line_items": [],
"parameters": {
"first": 1521034044,
"second": "oh hai"
}
}
3 changes: 2 additions & 1 deletion spec/interactors/cangaroo/perform_flow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
let(:interactors) do
[Cangaroo::ValidateJsonSchema,
Cangaroo::CountJsonObject,
Cangaroo::PerformJobs]
Cangaroo::PerformJobs,
Cangaroo::PersistParameters]
end

it { is_expected.to eql interactors }
Expand Down
161 changes: 161 additions & 0 deletions spec/interactors/cangaroo/persist_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
require 'rails_helper'

class JobC < Cangaroo::BaseJob
connection :job_c_connection
end
class JobD < Cangaroo::BaseJob; end
class JobE < Cangaroo::BaseJob
connection :job_e_connection
end

describe Cangaroo::PersistParameters do
let!(:connection) { create(:cangaroo_connection, name: JobC.connection.to_s) }
let(:flow) { JobC.new(source_connection: nil, type: 'orders', payload: {}) }
let(:parameters) { Hash.new }
subject do
described_class.new(
flow: flow,
parameters: parameters
)
end

describe 'integration with other interactors' do
let(:json_body) { JSON.parse(load_fixture('json_payload_parameters.json')) }
before(:each) do
job_d = double(:job_d, perform?: true, enqueue: true)
allow(JobD).to receive(:new).and_return(job_d)
end
context 'connection with parameters' do
before(:each) { expect(connection.parameters).to_not be_empty }
it "persists the new parameters" do
expect{
Cangaroo::PerformFlow.call(
flow: flow,
json_body: json_body,
jobs: [JobD],
source_connection: connection
)
}.to change{
connection.reload.parameters
}.to(json_body["parameters"])
end
end
context 'connection without parameters' do
let!(:connection) { create(:store, name: JobC.connection.to_s) }
before(:each) { expect(connection.parameters).to_not be_present }
it "doesn't persist params for a connection without them" do
expect{
Cangaroo::PerformFlow.call(
flow: flow,
json_body: json_body,
jobs: [JobD],
source_connection: connection
)
}.to_not change{
connection.reload.parameters
}.from({})
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at block body end.

end
end

describe '#call' do
context "new parameters" do
let(:parameters) { { connection.parameters.keys.first => "new value" } }
it 'persists the new parameters' do
old_params = connection.parameters
new_params = connection.parameters.with_indifferent_access.merge(parameters)
expect{
subject.call
}.to change{
connection.reload.parameters
}.from(old_params).to(new_params)
end
it 'fails if updates cannot be persisted' do
allow_any_instance_of(Cangaroo::Connection).to receive(:update) { false }
context = described_class.call(flow: flow, parameters: parameters)
expect(context).to_not be_success
expect(context).to be_failure
end
end
context 'different request parameters' do
let(:parameters) { { "different" => "param" } }
it 'does not update the DB' do
expect(connection.parameters.keys).to_not include(parameters.keys.first)
expect{ subject.call }.to_not change{ connection.reload.parameters.with_indifferent_access }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parenthesize the param change{ connection.reload.parameters.with_indifferent_access } to make sure that the block will be associated with the change method call.

end
end
it 'does not update the DB if there are no request params' do
expect(parameters).to be_empty
expect{ subject.call }.to_not change{ connection.reload.parameters }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parenthesize the param change{ connection.reload.parameters } to make sure that the block will be associated with the change method call.

end
end

describe '#connection' do
it "is the flow's connection" do
expect(subject.send(:connection)).to eq connection
end
end

describe '#request_params' do
context 'nil params' do
let(:parameters) { nil }
it "does not throw an error" do
expect(subject.send(:request_params)).to eq({})
end
end
it "handles empty params" do
expect(parameters).to be_empty
expect(subject.send(:request_params)).to eq({})
end
context "empty param values" do
let(:parameters) { {"a" => nil, "b" => 3, "c" => ""} }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside { missing.
Space inside } missing.

it "removes them" do
expect(subject.send(:request_params)).to eq({"b" => 3})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside { missing.
Redundant curly braces around a hash parameter.
Space inside } missing.

end
end
end

describe "#new_params" do
let(:new_params) { subject.send(:new_params) }
context "empty request params" do
before(:each) { expect(parameters).to be_empty }
it "returns the persisted params" do
persisted_params = connection.parameters
persisted_params.each do |key, value|
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use each_key instead of each.
Unused block argument - value. If it's necessary, use _ or _value as an argument name to indicate that it won't be used.

expect(new_params.fetch(key)).to eq(persisted_params.fetch(key))
end
end
end
context "extra request params" do
let(:parameters) { connection.parameters.merge("a" => 1) }
it "removes them" do
expect(new_params).to_not have_key("a")
end
end
context "missing request params" do
let(:param_key) { connection.parameters.keys.first }
let(:parameters) { { param_key => "new value" } }
it "ignores them" do
old_params = connection.parameters
missing_keys = old_params.keys - [param_key]
missing_keys.each do |missing_key|
expect(new_params[missing_key]).to eq(old_params[missing_key])
end
end
it "sets the present key" do
expect(new_params[param_key]).to eq(parameters[param_key])
end
end
context "symbol request params" do
let(:param_key) { connection.parameters.keys.first }
let(:parameters) { { param_key.to_sym => "new value" } }
before(:each) do
connection.update(parameters: connection.parameters.stringify_keys)
end
it "still matches them with their string counterparts" do
expect(new_params[param_key]).to eq(parameters[param_key.to_sym])
end
end
end
end
1 change: 1 addition & 0 deletions spec/jobs/cangaroo/poll_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class FakePollJob < Cangaroo::PollJob
allow_any_instance_of(Cangaroo::Webhook::Client)
.to receive(:post)
.and_return(parse_fixture('json_payload_ok.json'))
allow_any_instance_of(Cangaroo::Webhook::Client).to receive(:post).and_return(parse_fixture('json_payload_ok.json'))

allow(Cangaroo::PerformFlow).to receive(:call).and_return(double(success?: false,
message: 'bad failure'))
Expand Down
3 changes: 2 additions & 1 deletion spec/jobs/cangaroo/push_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module Cangaroo
job.perform_now
expect(Cangaroo::PerformFlow).to have_received(:call)
.once
.with(source_connection: destination_connection,
.with(flow: job,
source_connection: destination_connection,
json_body: connection_response,
jobs: Rails.configuration.cangaroo.jobs)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/factory_girl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'factory_girl_rails'
require 'factory_bot_rails'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include FactoryBot::Syntax::Methods
end