Skip to content

Commit bab7720

Browse files
committed
feat(matrix): add text/plain content type for easier visualisation of matrix resource in terminal
1 parent fa98cc0 commit bab7720

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'ostruct'
2+
require 'pact_broker/api/pact_broker_urls'
3+
require 'table_print'
4+
5+
module PactBroker
6+
module Api
7+
module Decorators
8+
class MatrixTextDecorator
9+
Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success)
10+
11+
def initialize(lines)
12+
@lines = lines
13+
end
14+
15+
def to_text(options)
16+
data = lines.collect do | line |
17+
Line.new(line[:consumer_name], line[:consumer_version_number], line[:provider_name], line[:provider_version_number], line[:success])
18+
end
19+
printer = TablePrint::Printer.new(data)
20+
printer.table_print + "\n"
21+
end
22+
23+
private
24+
25+
attr_reader :lines
26+
end
27+
end
28+
end
29+
end

lib/pact_broker/api/resources/matrix.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'pact_broker/api/resources/base_resource'
22
require 'pact_broker/api/decorators/matrix_decorator'
3+
require 'pact_broker/api/decorators/matrix_text_decorator'
34
require 'pact_broker/matrix/parse_query'
45

56
module PactBroker
@@ -12,7 +13,10 @@ def initialize
1213
end
1314

1415
def content_types_provided
15-
[["application/hal+json", :to_json]]
16+
[
17+
["application/hal+json", :to_json],
18+
["text/plain", :to_text]
19+
]
1620
end
1721

1822
def allowed_methods
@@ -30,10 +34,17 @@ def malformed_request?
3034
end
3135

3236
def to_json
33-
lines = matrix_service.find(selectors, options)
3437
PactBroker::Api::Decorators::MatrixPactDecorator.new(lines).to_json(user_options: { base_url: base_url })
3538
end
3639

40+
def to_text
41+
PactBroker::Api::Decorators::MatrixTextDecorator.new(lines).to_text(user_options: { base_url: base_url })
42+
end
43+
44+
def lines
45+
@lines ||= matrix_service.find(selectors, options)
46+
end
47+
3748
def selectors
3849
@selectors
3950
end

pact_broker.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |gem|
3737
gem.add_runtime_dependency 'sucker_punch', '~>2.0'
3838
gem.add_runtime_dependency 'rack-protection', '~>2.0'
3939
gem.add_runtime_dependency 'dry-types', '~> 0.10.3' # https://travis-ci.org/pact-foundation/pact_broker/jobs/249448621
40+
gem.add_runtime_dependency 'table_print', '~> 1.5'
4041

4142
gem.add_development_dependency 'bundler-audit', '~>0.4'
4243
gem.add_development_dependency 'sqlite3', '~>1.3'

0 commit comments

Comments
 (0)