class Fixturama::Changes::Request::Response

@private Store data for a response to the corresponding request

Attributes

repeat[R]

@!attribute [r] repeat A number of times the response to be repeated @return [Integer]

to_h[R]

@!attribute [r] to_h A hash for the +to_respond(…)+ part of the stub @return [Hash<Symbol, Object>]

Public Class Methods

new(options) click to toggle source
   # File lib/fixturama/changes/request/response.rb
17 def initialize(options)
18   @options = options
19   @options = with_error { Hash(options).transform_keys(&:to_sym) }
20   @to_h    = build_hash
21   @repeat  = build_repeat
22 end

Private Instance Methods

body() click to toggle source
   # File lib/fixturama/changes/request/response.rb
39 def body
40   with_error("body") do
41     case @options[:body]
42     when NilClass then nil
43     when Hash then JSON.dump(@options[:body])
44     else @options[:body].to_s
45     end
46   end
47 end
build_hash() click to toggle source
   # File lib/fixturama/changes/request/response.rb
31 def build_hash
32   { status: status, body: body, headers: headers }.select { |_, v| v }
33 end
build_repeat() click to toggle source
   # File lib/fixturama/changes/request/response.rb
24 def build_repeat
25   with_error("number of repeats") do
26     value = @options.fetch(:repeat, 1).to_i
27     value.positive? ? value : raise("Wrong value")
28   end
29 end
headers() click to toggle source
   # File lib/fixturama/changes/request/response.rb
49 def headers
50   with_error("headers") do
51     Hash(@options[:headers]).map { |k, v| [k.to_s, v.to_s] }.to_h
52   end
53 end
status() click to toggle source
   # File lib/fixturama/changes/request/response.rb
35 def status
36   with_error("status") { @options[:status]&.to_i } || 200
37 end
with_error(part = nil) { || ... } click to toggle source
   # File lib/fixturama/changes/request/response.rb
55 def with_error(part = nil)
56   yield
57 rescue StandardError => err
58   object = ["a response", part].compact.join(" ")
59   raise Fixturama::FixtureError.new(object, options, err)
60 end