From bab77207f78c41ee1dfd919d0d3a89b96a75f520 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 30 Oct 2017 16:01:25 +1100 Subject: [PATCH] feat(matrix): add text/plain content type for easier visualisation of matrix resource in terminal --- .../api/decorators/matrix_text_decorator.rb | 29 +++++++++++++++++++ lib/pact_broker/api/resources/matrix.rb | 15 ++++++++-- pact_broker.gemspec | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 lib/pact_broker/api/decorators/matrix_text_decorator.rb diff --git a/lib/pact_broker/api/decorators/matrix_text_decorator.rb b/lib/pact_broker/api/decorators/matrix_text_decorator.rb new file mode 100644 index 000000000..27068e8de --- /dev/null +++ b/lib/pact_broker/api/decorators/matrix_text_decorator.rb @@ -0,0 +1,29 @@ +require 'ostruct' +require 'pact_broker/api/pact_broker_urls' +require 'table_print' + +module PactBroker + module Api + module Decorators + class MatrixTextDecorator + Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success) + + def initialize(lines) + @lines = lines + end + + def to_text(options) + data = lines.collect do | line | + Line.new(line[:consumer_name], line[:consumer_version_number], line[:provider_name], line[:provider_version_number], line[:success]) + end + printer = TablePrint::Printer.new(data) + printer.table_print + "\n" + end + + private + + attr_reader :lines + end + end + end +end diff --git a/lib/pact_broker/api/resources/matrix.rb b/lib/pact_broker/api/resources/matrix.rb index 16ee5f721..a74964be0 100644 --- a/lib/pact_broker/api/resources/matrix.rb +++ b/lib/pact_broker/api/resources/matrix.rb @@ -1,5 +1,6 @@ require 'pact_broker/api/resources/base_resource' require 'pact_broker/api/decorators/matrix_decorator' +require 'pact_broker/api/decorators/matrix_text_decorator' require 'pact_broker/matrix/parse_query' module PactBroker @@ -12,7 +13,10 @@ def initialize end def content_types_provided - [["application/hal+json", :to_json]] + [ + ["application/hal+json", :to_json], + ["text/plain", :to_text] + ] end def allowed_methods @@ -30,10 +34,17 @@ def malformed_request? end def to_json - lines = matrix_service.find(selectors, options) PactBroker::Api::Decorators::MatrixPactDecorator.new(lines).to_json(user_options: { base_url: base_url }) end + def to_text + PactBroker::Api::Decorators::MatrixTextDecorator.new(lines).to_text(user_options: { base_url: base_url }) + end + + def lines + @lines ||= matrix_service.find(selectors, options) + end + def selectors @selectors end diff --git a/pact_broker.gemspec b/pact_broker.gemspec index 1f73b2776..38fe4a8f4 100644 --- a/pact_broker.gemspec +++ b/pact_broker.gemspec @@ -37,6 +37,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'sucker_punch', '~>2.0' gem.add_runtime_dependency 'rack-protection', '~>2.0' gem.add_runtime_dependency 'dry-types', '~> 0.10.3' # https://travis-ci.org/pact-foundation/pact_broker/jobs/249448621 + gem.add_runtime_dependency 'table_print', '~> 1.5' gem.add_development_dependency 'bundler-audit', '~>0.4' gem.add_development_dependency 'sqlite3', '~>1.3'