-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'pact_broker/client/pact_broker_client' | ||
require 'pact_broker/client/versions/formatter' | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
class Describe | ||
|
||
class Result | ||
attr_reader :success, :message | ||
|
||
def initialize success, message = nil | ||
@success = success | ||
@message = message | ||
end | ||
end | ||
|
||
def self.call params, options, pact_broker_base_url, pact_broker_client_options | ||
new(params, options, pact_broker_base_url, pact_broker_client_options).call | ||
end | ||
|
||
def initialize params, options, pact_broker_base_url, pact_broker_client_options | ||
@params = params | ||
@options = options | ||
@pact_broker_base_url = pact_broker_base_url | ||
@pact_broker_client_options = pact_broker_client_options | ||
end | ||
|
||
def call | ||
version_hash = if params[:version] | ||
versions_client.find params | ||
else | ||
pact_broker_client.pacticipants.versions.latest(params) | ||
end | ||
Result.new(true, format_version(version_hash)) | ||
end | ||
|
||
private | ||
|
||
def format_version(version_hash) | ||
Formatter.call(version_hash, options[:output]) | ||
end | ||
|
||
attr_reader :params, :options, :pact_broker_base_url, :pact_broker_client_options | ||
|
||
def versions_client | ||
pact_broker_client.pacticipants.versions | ||
end | ||
|
||
def pact_broker_client | ||
@pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url, client_options: pact_broker_client_options) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'pact_broker/client/versions/json_formatter' | ||
require 'pact_broker/client/versions/text_formatter' | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
class Formatter | ||
def self.call(matrix_lines, format) | ||
formatter = case format | ||
when 'json' then JsonFormatter | ||
when 'table' then TextFormatter | ||
else | ||
raise PactBroker::Client::Error.new("Invalid output option '#{format}. Must be one of 'table' or 'json'.") | ||
end | ||
formatter.call(matrix_lines) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'table_print' | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
class JsonFormatter | ||
def self.call(matrix) | ||
JSON.pretty_generate(matrix) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'table_print' | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
class TextFormatter | ||
|
||
Line = Struct.new(:number, :tags) | ||
|
||
OPTIONS = [ | ||
{ number: {} }, | ||
{ tags: {} } | ||
] | ||
|
||
def self.call(version_hash) | ||
tags = (lookup(version_hash, [], :_embedded, :tags) || []).collect{ | t| t[:name] }.join(" ") | ||
data = Line.new(version_hash[:number], tags) | ||
|
||
printer = TablePrint::Printer.new([data], OPTIONS) | ||
printer.table_print | ||
end | ||
|
||
def self.lookup line, default, *keys | ||
keys.reduce(line) { | line, key | line[key] } | ||
rescue NoMethodError | ||
default | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'pact_broker/client/versions/describe' | ||
|
||
module PactBroker | ||
module Client | ||
class Versions | ||
describe Describe do | ||
describe ".call" do | ||
|
||
let(:versions_client) do | ||
instance_double('PactBroker::Client::Versions', latest: version_hash) | ||
end | ||
|
||
let(:pact_broker_base_url) { 'http://broker' } | ||
let(:pact_broker_client_options) { {} } | ||
let(:pact_broker_client) { instance_double('PactBroker::Client::PactBrokerClient')} | ||
let(:version_hash) do | ||
{ | ||
foo: 'bar' | ||
} | ||
end | ||
|
||
let(:params) do | ||
{ | ||
pacticipant: 'foo', | ||
latest: true, | ||
tag: 'bar' | ||
} | ||
end | ||
|
||
let(:options) do | ||
{ | ||
output: 'someformat' | ||
} | ||
end | ||
|
||
before do | ||
allow(PactBroker::Client::PactBrokerClient).to receive(:new).and_return(pact_broker_client) | ||
allow(pact_broker_client).to receive_message_chain(:pacticipants, :versions).and_return(versions_client) | ||
allow(Formatter).to receive(:call).and_return('formatted_output') | ||
end | ||
|
||
subject { Describe.call(params, options, pact_broker_base_url, pact_broker_client_options) } | ||
|
||
it "invokes the versions client" do | ||
expect(versions_client).to receive(:latest).with(params) | ||
subject | ||
end | ||
|
||
it "formats the output" do | ||
expect(Formatter).to receive(:call).with(version_hash, 'someformat') | ||
subject | ||
end | ||
|
||
it "returns a successful result" do | ||
expect(subject.success).to be true | ||
end | ||
|
||
it "returns a result with the formatted output as the message" do | ||
expect(subject.message).to eq 'formatted_output' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |