blob: 5fc2e3ffc0d8203fc45fe346030bcb4ba3540cc4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
52
53
|
# frozen_string_literal: true
module Provider
module ContractSourceHelper
QA_PACT_BROKER_HOST = "http://localhost:9292/pacts"
PREFIX_PATHS = {
rake: "../../../contracts/contracts/project",
spec: "../contracts/project"
}.freeze
SUB_PATH_REGEX = %r{project/(?<file_path>.*?)_helper.rb}.freeze
class << self
def contract_location(requester, file_path)
raise ArgumentError, 'requester must be :rake or :spec' unless [:rake, :spec].include? requester
relevant_path = file_path.match(SUB_PATH_REGEX)[:file_path].split('/')
ENV["PACT_BROKER"] ? pact_broker_url(relevant_path) : local_contract_location(requester, relevant_path)
end
def pact_broker_url(file_path)
provider_url = "provider/#{construct_provider_url_path(file_path)}"
consumer_url = "consumer/#{construct_consumer_url_path(file_path)}"
"#{QA_PACT_BROKER_HOST}/#{provider_url}/#{consumer_url}/latest"
end
def construct_provider_url_path(file_path)
split_name = file_path[2].split('_')
split_name[0] = split_name[0].upcase
split_name.join("%20")
end
def construct_consumer_url_path(file_path)
"#{file_path[0].split('_').map(&:capitalize).join}%23#{file_path[1]}"
end
def local_contract_location(requester, file_path)
contract_path = construct_local_contract_path(file_path)
prefix_path = requester == :rake ? File.expand_path(PREFIX_PATHS[requester], __dir__) : PREFIX_PATHS[requester]
"#{prefix_path}#{contract_path}"
end
def construct_local_contract_path(file_path)
contract_file_name = "#{file_path[0].tr('_', '')}##{file_path[1]}-#{file_path[2]}.json"
"/#{file_path[0]}/#{file_path[1]}/#{contract_file_name}"
end
end
end
end
|