class Pact::Retreaty::Consumer

Constants

COMMON_CONFIG
DOWNLOAD_CONFIG
UPLOAD_CONFIG

Public Class Methods

create(options = {}) { |consumer| ... } click to toggle source
# File lib/pact/retreaty/consumer.rb, line 12
def self.create(options = {})
  options[:vcs_id] ||= -> { current_vcs_id }
  new(options).tap { |consumer| yield consumer if block_given? }
end
new(options = {}) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 38
def initialize(options = {})
  options.each do |k, v|
    send("#{k}=", v)
  end
end

Public Instance Methods

best_pact_uri() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 27
def best_pact_uri
  verify_config!(:download)

  catch(:uri_found) do
    realised_vcs_fallbacks.each { |vcs_id| uri_for_pactfile_and_vcs_id(vcs_id) }
    fail("Retreaty couldn't find a suitable contract for version #{version} of #{name}, under #{realised_vcs_fallbacks.join(' or ')}")
  end
end
upload_pacts() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 17
def upload_pacts
  verify_config!(:upload)

  spec_files.each do |path|
    key = upload_key_for_path(path)
    puts "uploading #{key}"
    s3_client.put_object(bucket: s3_bucket, key: key, body: File.read(path))
  end
end

Private Instance Methods

current_vcs_id() click to toggle source

TODO - handle cases where git isn’t present - configure with a proc?

# File lib/pact/retreaty/consumer.rb, line 81
def current_vcs_id #current_branch
  `git symbolic-ref -q --short HEAD`.strip
end
pact_dir() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 85
def pact_dir
  Pact.configuration.pact_dir
end
realised_vcs_fallbacks() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 76
def realised_vcs_fallbacks
  vcs_fallbacks.map {|id| id == :vcs_id ? current_vcs_id : id }
end
s3_client() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 89
def s3_client
  @client ||= Aws::S3::Client.new(region: s3_region, credentials: s3_credentials)
end
s3_credentials() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 93
def s3_credentials
  Aws::Credentials.new(access_key_id, access_secret)
end
s3_path(filename, vcs_id) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 72
def s3_path(filename, vcs_id)
  [name, version, vcs_id, filename].compact.join('/')
end
s3_summary_for_vcs_id(vcs_id) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 49
def s3_summary_for_vcs_id(vcs_id)
  Aws::S3::ObjectSummary.new(bucket_name: s3_bucket, key: s3_path(pactfile, vcs_id), client: s3_client)
end
spec_files() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 60
def spec_files
  Dir.glob(File.join(pact_dir, '*.json'))
end
upload_key_for_path(path) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 64
def upload_key_for_path(path)
  s3_path(File.basename(path), upload_vcs_id)
end
upload_vcs_id() click to toggle source
# File lib/pact/retreaty/consumer.rb, line 68
def upload_vcs_id
  instance_exec(&vcs_id)
end
uri_for_pactfile_and_vcs_id(vcs_id) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 44
def uri_for_pactfile_and_vcs_id(vcs_id)
  summary = s3_summary_for_vcs_id(vcs_id)
  throw(:uri_found, summary.presigned_url(:get)) if summary.exists?
end
verify_config!(context) click to toggle source
# File lib/pact/retreaty/consumer.rb, line 53
def verify_config!(context)
  required_fields = COMMON_CONFIG + (context == :download ? DOWNLOAD_CONFIG : UPLOAD_CONFIG)
  unless required_fields.select {|field| send(field).nil? }.empty?
    fail("Retreaty requires configuration for #{required_fields.join(', ')} to #{context}")
  end
end