Class: PactBroker::Client::Matrix

Inherits:
BaseClient show all
Defined in:
lib/pact_broker/client/matrix.rb,
lib/pact_broker/client/matrix/query.rb,
lib/pact_broker/client/matrix/resource.rb,
lib/pact_broker/client/matrix/formatter.rb,
lib/pact_broker/client/matrix/json_formatter.rb,
lib/pact_broker/client/matrix/text_formatter.rb,
lib/pact_broker/client/matrix/abbreviate_version_number.rb

Defined Under Namespace

Classes: AbbreviateVersionNumber, Formatter, JsonFormatter, Query, Resource, TextFormatter

Constant Summary

Constants inherited from BaseClient

BaseClient::ERROR_CODE_MAPPING

Instance Attribute Summary

Attributes inherited from BaseClient

#base_url, #client_options

Instance Method Summary collapse

Methods inherited from BaseClient

#default_get_headers, #default_patch_headers, #default_put_headers, #default_request_headers, #initialize, #patch, #put, #url_for_relation, #verbose?

Methods included from StringToSymbol

#string_keys_to_symbols

Methods included from UrlHelpers

#encode_param, #encode_query_param

Constructor Details

This class inherits a constructor from PactBroker::Client::BaseClient

Instance Method Details

#convert_selector_hashes_to_params(selectors) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/pact_broker/client/matrix.rb', line 69

def convert_selector_hashes_to_params(selectors)
  selectors.collect do |selector|
    { pacticipant: selector[:pacticipant] }.tap do | hash |
      hash[:version] = selector[:version] if selector[:version]
      hash[:latest] = 'true' if selector[:latest]
      hash[:tag] = selector[:tag] if selector[:tag]
      hash[:branch] = selector[:branch] if selector[:branch]
    end
  end
end

#get(selectors, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pact_broker/client/matrix.rb', line 9

def get selectors, options = {}
  latestby = selectors.size == 1 ? 'cvp' : 'cvpv'
  query = {
    q: convert_selector_hashes_to_params(selectors),
    latestby: latestby
  }.merge(query_options(options, selectors))
  response = self.class.get("/matrix", query: query, headers: default_get_headers)
  response = handle_response(response) do
    Matrix::Resource.new(JSON.parse(response.body, symbolize_names: true))
  end
end

#handle_response(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pact_broker/client/matrix.rb', line 21

def handle_response response
  if response.success?
    yield
  elsif response.code == 401
    message = "Authentication failed"
    if response.body && response.body.size > 0
      message = message + ": #{response.body}"
    end
    raise Error.new(message)
  elsif response.code == 404
    raise Error.new("Matrix resource not found at #{base_url}/matrix. Please upgrade your Broker to the latest version.")
  else
    error_message = nil
    begin
      errors = JSON.parse(response.body)['errors']
      error_message = if errors.is_a?(Array)
        errors.join("\n")
      elsif errors.is_a?(Hash)
        errors.collect{ |key, value| "#{key}: #{value}" }.join("\n")
      else
        response.body
      end
    rescue
      raise Error.new("status=#{response.code} #{response.body}")
    end
    raise Error.new(error_message)
  end
end

#query_options(options, selectors) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pact_broker/client/matrix.rb', line 50

def query_options(options, selectors)
  opts = {}
  if options.key?(:success)
    opts[:success] = [*options[:success]]
  end
  opts[:limit] = options[:limit] if options[:limit]
  opts[:environment] = options[:to_environment] if options[:to_environment]
  if options[:to_tag]
    opts[:latest] = 'true'
    opts[:tag] = options[:to_tag]
  elsif selectors.size == 1 && !options[:to_environment]
    opts[:latest] = 'true'
  end
  if options[:ignore_selectors] && options[:ignore_selectors].any?
    opts[:ignore] = convert_selector_hashes_to_params(options[:ignore_selectors])
  end
  opts
end