1+ require 'pact/hal/entity'
2+
13module Pact
24 module Provider
35 module Help
46 class PactDiff
57 class PrintPactDiffError < StandardError ; end
68
7- attr_reader :pact_json , :output
9+ attr_reader :pact_source , :output
810
9- def initialize pact_json
10- @pact_json = pact_json
11+ def initialize pact_source
12+ @pact_source = pact_source
1113 end
1214
13- def self . call pact_json
14- new ( pact_json ) . call
15+ def self . call pact_source
16+ new ( pact_source ) . call
1517 end
1618
1719 def call
1820 begin
19- if diff_rel && diff_url
20- header + "\n " + get_diff
21- end
21+ header + "\n " + get_diff
2222 rescue PrintPactDiffError => e
2323 return e . message
2424 end
@@ -30,35 +30,13 @@ def header
3030 "The following changes have been made since the previous distinct version of this pact, and may be responsible for verification failure:\n "
3131 end
3232
33- def pact_hash
34- @pact_hash ||= json_load ( pact_json )
35- end
36-
37- def links
38- pact_hash [ '_links' ] || pact_hash [ 'links' ]
39- end
40-
41- def diff_rel
42- return nil unless links
43- key = links . keys . find { | key | key =~ /diff/ && key =~ /distinct/ && key =~ /previous/ }
44- key ? links [ key ] : nil
45- end
46-
47- def diff_url
48- diff_rel [ 'href' ]
49- end
50-
5133 def get_diff
5234 begin
53- URI . open ( diff_url ) { | file | file . read }
35+ pact_source . hal_entity . _link! ( "pb:diff-previous-distinct" ) . get! ( nil , "Accept" => "text/plain" ) . body
5436 rescue StandardError => e
55- raise PrintPactDiffError . new ( "Tried to retrieve diff with previous pact from #{ diff_url } , but received response code #{ e } ." )
37+ raise PrintPactDiffError . new ( "Tried to retrieve diff with previous pact, but received error #{ e . class } #{ e . message } ." )
5638 end
5739 end
58-
59- def json_load json
60- JSON . load ( json , nil , { max_nesting : 50 } )
61- end
6240 end
6341 end
6442 end
0 commit comments