class PactBroker::Api::Renderers::HtmlPactRenderer

Public Class Methods

call(pact, options = {}) click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 18
def self.call pact, options = {}
  new(pact, options).call
end
new(pact, options = {}) click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 22
def initialize pact, options = {}
  @json_content = pact.json_content
  @pact = pact
  @options = options
end

Public Instance Methods

call() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 28
def call
  "<html>
    <head>#{head}</head>
    <body>
      #{breadcrumbs}#{pact_metadata}#{html}
    </body>
  </html>"
end

Private Instance Methods

badge_list_item() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 94
def badge_list_item
  "<li class='pact-badge'>
    <img src=\"#{badge_url}\"/>
  </li>"
end
badge_markdown() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 106
def badge_markdown
  warning = if badges_protected?
              "If the broker is protected by authentication, set `enable_public_badge_access` to true in the configuration to enable badges to be embedded in a markdown file.\n"
            else
              ""
            end
  "#{warning}[![#{@pact.consumer.name}/#{@pact.provider.name} Pact Status](#{badge_url})](#{badge_target_url})"
end
badge_markdown_item() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 100
def badge_markdown_item
  "<li class='pact-badge-markdown' style='display:none'>
      <textarea rows='3' cols='100'>#{badge_markdown}</textarea>
  </li>"
end
badge_target_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 163
def badge_target_url
  base_url
end
badge_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 167
def badge_url
  @options[:badge_url]
end
badges_protected?() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 115
def badges_protected?
  !PactBroker.configuration.enable_public_badge_access
end
base_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 119
def base_url
  @options[:base_url] || ""
end
breadcrumbs() click to toggle source
consumer_contract() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 193
def consumer_contract
  Pact::ConsumerContract.from_json(@json_content)
rescue => e
  logger.info "Could not parse the following content to a Pact due to #{e.class} #{e.message}, showing raw content instead: #{@json_content}"
  raise NotAPactError
end
consumer_name() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 131
def consumer_name
  h(@pact.consumer.name)
end
consumer_version_number() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 127
def consumer_version_number
  h(@pact.consumer_version_number)
end
h(string) click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 200
def h string
  Rack::Utils.escape_html(string)
end
head() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 39
def head
  "<title>#{title}</title>
  <link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/github.css'>
  <link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/github-json.css'>
  <link rel='stylesheet' type='text/css' href='#{base_url}/css/bootstrap.min.css'>
  <link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/material-menu.css'>
  <link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/pact.css'>
  <link rel='stylesheet' type='text/css' href='#{base_url}/stylesheets/jquery-confirm.min.css'>
  <script src='#{base_url}/javascripts/highlight.pack.js'></script>
  <script src='#{base_url}/javascripts/jquery-3.5.1.min.js'></script>
  <script src='#{base_url}/js/bootstrap.min.js'></script>
  <script src='#{base_url}/javascripts/material-menu.js'></script>
  <script src='#{base_url}/javascripts/pact.js'></script>
  <script src='#{base_url}/javascripts/jquery-confirm.min.js'></script>
  <script>hljs.initHighlightingOnLoad();</script>"
end
html() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 189
def html
  Redcarpet::Markdown.new(Redcarpet::Render::HTML, :fenced_code_blocks => true, :lax_spacing => true).render(markdown)
end
json_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 147
def json_url
  PactBroker::Api::PactBrokerUrls.hal_browser_url pact_url, base_url
end
latest_pact_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 159
def latest_pact_url
  PactBroker::Api::PactBrokerUrls.latest_pact_url base_url, @pact
end
markdown() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 180
def markdown
  Pact::Doc::Markdown::ConsumerContractRenderer.call consumer_contract
rescue StandardError
  heading = "### A contract between #{@pact.consumer.name} and #{@pact.provider.name}"
  warning = "_Note: this contract could not be parsed to a v1 or v2 Pact, showing raw content instead._"
  pretty_json = JSON.pretty_generate(@pact.content_hash)
  "#{heading}\n#{warning}\n```json\n#{pretty_json}\n```\n"
end
matrix_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 155
def matrix_url
  PactBroker::Api::PactBrokerUrls.matrix_for_pact_url(@pact, base_url)
end
pact_metadata() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 67
def pact_metadata
  "<div class='pact-metadata'>
    <ul>
      #{badge_list_item}
      #{badge_markdown_item}
      <li>
        <span class='name'>#{consumer_name} version:</span>
        <span class='value'>#{consumer_version_number}#{tags}</span>
      </li>
      <li>
        <span class='name' title='#{published_date}'>Date published:</span>
        <span class='value' title='#{published_date}'>#{published_date_in_words}</span>
      </li>
      <li>
        <span data-consumer-name=\"#{consumer_name}\"
              data-provider-name=\"#{provider_name}\"
              data-consumer-version-number=\"#{consumer_version_number}\"
              data-api-browser-url=\"#{json_url}\"
              data-matrix-url=\"#{matrix_url}\"
              data-pact-url=\"#{pact_url}\"
              class='more-options kebab-horizontal'
              aria-hidden='true'></span>
      </li>
    </ul>
  </div>"
end
pact_url() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 151
def pact_url
  PactBroker::Api::PactBrokerUrls.pact_url base_url, @pact
end
provider_name() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 135
def provider_name
  h(@pact.provider.name)
end
published_date() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 139
def published_date
  @pact.created_at.to_time.localtime.to_datetime.strftime("%a %d %b %Y, %l:%M%P %:z")
end
published_date_in_words() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 143
def published_date_in_words
  PactBroker::DateHelper.distance_of_time_in_words(@pact.created_at.to_time, DateTime.now) + " ago"
end
tags() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 171
def tags
  if @pact.consumer_version_tag_names.any?
    tag_names = @pact.consumer_version_tag_names.collect{ |t| h(t) }.join(", ")
    " (#{tag_names})"
  else
    ""
  end
end
title() click to toggle source
# File lib/pact_broker/api/renderers/html_pact_renderer.rb, line 123
def title
  "Pact between #{consumer_name} and #{provider_name}"
end