Skip to content

Commit

Permalink
feat(matrix): add text/plain content type for easier visualisation of…
Browse files Browse the repository at this point in the history
… matrix resource in terminal
  • Loading branch information
bethesque committed Oct 30, 2017
1 parent fa98cc0 commit bab7720
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/pact_broker/api/decorators/matrix_text_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 13 additions & 2 deletions lib/pact_broker/api/resources/matrix.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pact_broker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit bab7720

Please sign in to comment.