class Pact::Doc::InteractionViewModel

Constants

MARKDOWN_BOLD_CHARACTERS

Attributes

consumer_contract[R]
interaction[R]

Public Class Methods

new(interaction, consumer_contract) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 13
def initialize interaction, consumer_contract
  @interaction = interaction
  @consumer_contract = consumer_contract
end

Public Instance Methods

consumer_name() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 41
def consumer_name
  markdown_escape @consumer_contract.consumer.name
end
description(start_of_sentence = false) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 67
def description start_of_sentence = false
  return "" unless @interaction.description
  markdown_escape apply_capitals(@interaction.description.strip, start_of_sentence)
end
formatted_provider_states(mark_bold: false) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 57
def formatted_provider_states mark_bold: false
  bold_marker = mark_bold ? MARKDOWN_BOLD_CHARACTERS : ""

  (@interaction.provider_states || []).map do |ps|
    "#{bold_marker}" \
    "#{markdown_escape(apply_capitals(ps.name.strip, false))}" \
    "#{bold_marker}"
  end.join(" and ")
end
has_provider_state?() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 49
def has_provider_state?
  @interaction.provider_state && !@interaction.provider_state.empty?
end
id() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 18
def id
  @id ||= begin
    full_desc = if has_provider_state?
                  "#{description} given #{interaction.provider_state}"
                else
                  description
                end
    CGI.escapeHTML(full_desc.gsub(/\s+/,"_"))
  end
end
provider_name() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 45
def provider_name
  markdown_escape @consumer_contract.provider.name
end
provider_state(start_of_sentence = false) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 53
def provider_state start_of_sentence = false
  markdown_escape apply_capitals(@interaction.provider_state.strip, start_of_sentence)
end
request() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 72
def request
  fix_json_formatting JSON.pretty_generate(clean_request)
end
request_method() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 29
def request_method
  interaction.request.method.upcase
end
request_path() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 33
def request_path
  interaction.request.path
end
response() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 76
def response
  fix_json_formatting JSON.pretty_generate(clean_response)
end
response_status() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 37
def response_status
  interaction.response.status
end

Private Instance Methods

apply_capitals(string, start_of_sentence = false) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 118
def apply_capitals string, start_of_sentence = false
  start_of_sentence ? capitalize_first_letter(string) : lowercase_first_letter(string)
end
capitalize_first_letter(string) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 122
def capitalize_first_letter string
  string[0].upcase + string[1..-1]
end
clean_request() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 84
def clean_request
  reified_request = Reification.from_term(interaction.request)
  ordered_clean_hash(reified_request).tap do | hash |
    hash[:body] = reified_request[:body] if reified_request[:body]
  end
end
clean_response() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 91
def clean_response
  ordered_clean_hash Reification.from_term(interaction.response)
end
lowercase_first_letter(string) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 126
def lowercase_first_letter string
  string[0].downcase + string[1..-1]
end
markdown_escape(string) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 130
def markdown_escape string
  return nil unless string
  string.gsub("*","\\*").gsub("_","\\_")
end
ordered_clean_hash(source) click to toggle source

Remove empty body and headers hashes from response, and empty headers from request, as an empty hash means “allow anything” - it's more intuitive and cleaner to just remove the empty hashes from display.

# File lib/pact/doc/interaction_view_model.rb, line 98
def ordered_clean_hash source
  ordered_keys.each_with_object({}) do |key, target|
    if source.key? key
      target[key] = source[key] unless value_is_an_empty_hash(source[key])
    end
  end
end
ordered_keys() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 110
def ordered_keys
  [:method, :path, :query, :status, :headers, :body]
end
remove_key_if_empty(key, hash) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 114
def remove_key_if_empty key, hash
  hash.delete(key) if hash[key].is_a?(Hash) && hash[key].empty?
end
value_is_an_empty_hash(value) click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 106
def value_is_an_empty_hash value
  value.is_a?(Hash) && value.empty?
end