Module: Pact::PactFile

Extended by:
PactFile
Included in:
ConsumerContract, PactFile
Defined in:
lib/pact/consumer_contract/pact_file.rb

Instance Method Summary collapse

Instance Method Details

#read(uri, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pact/consumer_contract/pact_file.rb', line 7

def read uri, options = {}
  uri_string = uri.to_s
  pact = render_pact(uri_string, options)
  if options[:save_pactfile_to_tmp]
    save_pactfile_to_tmp pact, ::File.basename(uri_string)
  end
  pact
rescue StandardError => e
  $stderr.puts "Error reading file from #{uri}"
  $stderr.puts "#{e.to_s} #{e.backtrace.join("\n")}"
  raise e
end

#render_pact(uri_string, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pact/consumer_contract/pact_file.rb', line 25

def render_pact uri_string, options
  uri_obj = URI(uri_string)
   = uri_obj.userinfo
  if()
    options[:username] = uri_obj.user unless options[:username]
    options[:password] = uri_obj.password unless options[:password]
    uri_string = uri_string.sub("#{uri_user_info}@", '')
  end
  open_options = options[:username] ? {http_basic_authentication:[options[:username],options[:password]]} : {}
  open(uri_string, open_options) { | file | file.read }
end

#save_pactfile_to_tmp(pact, name) ⇒ Object



20
21
22
23
# File 'lib/pact/consumer_contract/pact_file.rb', line 20

def save_pactfile_to_tmp pact, name
  ::FileUtils.mkdir_p Pact.configuration.tmp_dir
  ::File.open(Pact.configuration.tmp_dir + "/#{name}", "w") { |file|  file << pact}
end