class Pact::PactBroker::FetchPacts

Constants

ALL_PROVIDER_TAG_RELATION
HREF
LATEST_PROVIDER_RELATION
LATEST_PROVIDER_TAG_RELATION
PACTS
PB_PACTS

Attributes

broker_base_url[R]
http_client[R]
http_client_options[R]
index_entity[R]
provider[R]
tags[R]

Public Class Methods

call(provider, tags, broker_base_url, http_client_options) click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 32
def self.call(provider, tags, broker_base_url, http_client_options)
  new(provider, tags, broker_base_url, http_client_options).call
end
new(provider, tags, broker_base_url, http_client_options) click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 18
def initialize(provider, tags, broker_base_url, http_client_options)
  @provider = provider
  @tags = (tags || []).collect do |tag|
    if tag.is_a?(String)
      { name: tag, all: false, fallback: nil }
    else
      tag
    end
  end
  @http_client_options = http_client_options
  @broker_base_url = broker_base_url
  @http_client = Pact::Hal::HttpClient.new(http_client_options)
end

Public Instance Methods

call() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 36
def call
  log_message
  if index.success?
    if any_tags?
      tagged_pacts_for_provider
    else
      latest_pacts_for_provider
    end
  else
    raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}")
  end
end

Private Instance Methods

any_tags?() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 51
def any_tags?
  tags && tags.any?
end
index() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 74
def index
  @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get.assert_success!
end
latest_pacts_for_provider() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 78
def latest_pacts_for_provider
  link = index_entity._link!(LATEST_PROVIDER_RELATION)
  pact_urls(link.expand(provider: provider).get.assert_success!)
end
log_message() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 89
def log_message
  message = "INFO: Fetching pacts for #{provider} from #{broker_base_url}"
  if tags.any?
    desc = tags.collect do |tag|
      all_or_latest = tag[:all] ? "all" : "latest"
      name = tag[:fallback] ? "#{tag[:name]} (or #{tag[:fallback]} if not found)" : tag[:name]
      "#{all_or_latest} #{name}"
    end.join(", ")
    message << " for tags: #{desc}"
  end
  Pact.configuration.output_stream.puts message
end
pact_urls(link_by_provider) click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 83
def pact_urls(link_by_provider)
  link_by_provider.assert_success!.fetch(PB_PACTS, PACTS).collect do |pact|
    Pact::Provider::PactURI.new(pact[HREF], http_client_options)
  end
end
tagged_pacts_for_provider() click to toggle source
# File lib/pact/pact_broker/fetch_pacts.rb, line 55
def tagged_pacts_for_provider
  tags.collect do |tag|
    link = link_for(tag)
    urls = pact_urls(link.expand(provider: provider, tag: tag[:name]).get)
    if urls.empty? && tag[:fallback]
      urls = pact_urls(link.expand(provider: provider, tag: tag[:fallback]).get)
    end
    urls
  end.flatten
end