|
| 1 | +require 'ostruct' |
| 2 | +require 'pact_broker/api/pact_broker_urls' |
| 3 | + |
| 4 | +module PactBroker |
| 5 | + module Api |
| 6 | + module Decorators |
| 7 | + class MatrixPactDecorator |
| 8 | + include PactBroker::Api::PactBrokerUrls |
| 9 | + |
| 10 | + def initialize(lines) |
| 11 | + @lines = lines |
| 12 | + end |
| 13 | + |
| 14 | + def to_json(options) |
| 15 | + to_hash(options).to_json |
| 16 | + end |
| 17 | + |
| 18 | + def to_hash(options) |
| 19 | + { |
| 20 | + matrix: matrix(lines, options[:user_options][:base_url]) |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + private |
| 25 | + |
| 26 | + attr_reader :lines |
| 27 | + |
| 28 | + def matrix(lines, base_url) |
| 29 | + provider = nil |
| 30 | + consumer = nil |
| 31 | + lines.collect do | line | |
| 32 | + provider ||= OpenStruct.new(name: line[:provider_name]) |
| 33 | + consumer ||= OpenStruct.new(name: line[:consumer_name]) |
| 34 | + consumer_version = OpenStruct.new(number: line[:consumer_version_number], pacticipant: consumer) |
| 35 | + line_hash(consumer, provider, consumer_version, line, base_url) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def line_hash(consumer, provider, consumer_version, line, base_url) |
| 40 | + { |
| 41 | + consumer: consumer_hash(line, consumer_version, base_url), |
| 42 | + provider: provider_hash(line, provider, base_url), |
| 43 | + pact: pact_hash(line, base_url), |
| 44 | + verificationResult: verification_hash(line, base_url) |
| 45 | + } |
| 46 | + end |
| 47 | + |
| 48 | + def consumer_hash(line, consumer_version, base_url) |
| 49 | + { |
| 50 | + version: { |
| 51 | + number: line[:consumer_version_number], |
| 52 | + _links: { |
| 53 | + self: { |
| 54 | + href: version_url(base_url, consumer_version) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + end |
| 60 | + |
| 61 | + def provider_hash(line, provider, base_url) |
| 62 | + if !line[:provider_version].nil? |
| 63 | + { |
| 64 | + version: { |
| 65 | + number: line[:provider_version] |
| 66 | + } |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + version: nil |
| 71 | + } |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def pact_hash(line, base_url) |
| 76 | + { |
| 77 | + _links: { |
| 78 | + self: { |
| 79 | + href: pact_url_from_params(base_url, line) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + def verification_hash(line, base_url) |
| 86 | + if !line[:success].nil? |
| 87 | + { |
| 88 | + success: line[:success], |
| 89 | + _links: { |
| 90 | + self: { |
| 91 | + href: verification_url(OpenStruct.new(line), base_url) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + nil |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments