class Pact::Doc::InteractionViewModel

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 11
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 39
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 55
def description start_of_sentence = false
  return '' unless @interaction.description
  markdown_escape apply_capitals(@interaction.description.strip, start_of_sentence)
end
has_provider_state?() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 47
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 16
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 43
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 51
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 60
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 27
def request_method
  interaction.request.method.upcase
end
request_path() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 31
def request_path
  interaction.request.path
end
response() click to toggle source
# File lib/pact/doc/interaction_view_model.rb, line 64
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 35
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 106
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 110
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 72
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 79
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 114
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 118
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 86
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 98
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 102
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 94
def value_is_an_empty_hash value
  value.is_a?(Hash) && value.empty?
end