|
| 1 | +require 'ostruct' |
| 2 | +require 'pact_broker/api/pact_broker_urls' |
| 3 | + |
| 4 | +module PactBroker |
| 5 | + module Api |
| 6 | + module Decorators |
| 7 | + class DashboardDecorator |
| 8 | + include PactBroker::Api::PactBrokerUrls |
| 9 | + |
| 10 | + def initialize(index_items) |
| 11 | + @index_items = index_items |
| 12 | + end |
| 13 | + |
| 14 | + def to_json(options) |
| 15 | + to_hash(options).to_json |
| 16 | + end |
| 17 | + |
| 18 | + def to_hash(options) |
| 19 | + { |
| 20 | + items: items(index_items, options[:user_options][:base_url]) |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + private |
| 25 | + |
| 26 | + attr_reader :index_items |
| 27 | + |
| 28 | + def items(index_items, base_url) |
| 29 | + index_items.collect do | index_item | |
| 30 | + index_item_hash(index_item.consumer, index_item.provider, index_item.consumer_version, index_item, base_url) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def index_item_hash(consumer, provider, consumer_version, index_item, base_url) |
| 35 | + { |
| 36 | + consumer: consumer_hash(index_item, consumer, consumer_version, base_url), |
| 37 | + provider: provider_hash(index_item, provider, base_url), |
| 38 | + pact: pact_hash(index_item, base_url), |
| 39 | + latestVerificationResult: verification_hash(index_item, base_url), |
| 40 | + verificationStatus: index_item.verification_status.to_s, |
| 41 | + webhookStatus: index_item.webhook_status.to_s, |
| 42 | + latestWebhookExecution: latest_webhook_execution(index_item, base_url), |
| 43 | + _links: links(index_item, base_url) |
| 44 | + |
| 45 | + } |
| 46 | + end |
| 47 | + |
| 48 | + def consumer_hash(index_item, consumer, consumer_version, base_url) |
| 49 | + { |
| 50 | + name: index_item.consumer_name, |
| 51 | + version: { |
| 52 | + number: index_item.consumer_version_number, |
| 53 | + _links: { |
| 54 | + self: { |
| 55 | + href: version_url(base_url, index_item.consumer_version) |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + _links: { |
| 60 | + self: { |
| 61 | + href: pacticipant_url(base_url, index_item.consumer) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + end |
| 66 | + |
| 67 | + def provider_hash(index_item, provider, base_url) |
| 68 | + hash = { |
| 69 | + name: index_item.provider_name, |
| 70 | + version: nil, |
| 71 | + _links: { |
| 72 | + self: { |
| 73 | + href: pacticipant_url(base_url, index_item.provider) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if index_item.latest_verification |
| 79 | + hash[:version] = { number: index_item.provider_version_number } |
| 80 | + end |
| 81 | + |
| 82 | + hash |
| 83 | + end |
| 84 | + |
| 85 | + def pact_hash(index_item, base_url) |
| 86 | + { |
| 87 | + createdAt: index_item.latest_pact.created_at.to_datetime.xmlschema, |
| 88 | + _links: { |
| 89 | + self: { |
| 90 | + href: pact_url(base_url, index_item.latest_pact) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + end |
| 95 | + |
| 96 | + def verification_hash(index_item, base_url) |
| 97 | + if index_item.latest_verification |
| 98 | + { |
| 99 | + success: index_item.latest_verification.success, |
| 100 | + verifiedAt: index_item.latest_verification.created_at.to_datetime.xmlschema, |
| 101 | + _links: { |
| 102 | + self: { |
| 103 | + href: verification_url(index_item.latest_verification, base_url) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + else |
| 108 | + nil |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + def latest_webhook_execution(index_item, base_url) |
| 113 | + if index_item.last_webhook_execution_date |
| 114 | + { |
| 115 | + triggeredAt: index_item.last_webhook_execution_date.to_datetime.xmlschema |
| 116 | + } |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def links(index_item, base_url) |
| 121 | + { |
| 122 | + 'pb:webhook-status' => { |
| 123 | + title: "Status of webhooks for #{index_item.consumer_name}/#{index_item.provider_name} pact", |
| 124 | + href: webhooks_status_url(index_item.consumer, index_item.provider, base_url) |
| 125 | + } |
| 126 | + } |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments