class Fixturama::Changes::Request

@private Stub an HTTP(S) request using Webmock

Constants

HTTP_METHODS

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
   # File lib/fixturama/changes/request.rb
21 def initialize(options)
22   @options = options
23   with_error { @options = Hash(@options).transform_keys(&:to_sym) }
24 end

Public Instance Methods

call(example) click to toggle source
   # File lib/fixturama/changes/request.rb
10 def call(example)
11   stub = example.stub_request(http_method, uri)
12   stub = stub.with(request) if request.any?
13   stub.to_return { |_| responses.next }
14   self
15 end

Private Instance Methods

basic_auth() click to toggle source
   # File lib/fixturama/changes/request.rb
57 def basic_auth
58   with_error("basic auth") do
59     value = options[:auth] || options[:basic_auth]
60     Hash(value).transform_keys(&:to_s).values_at("user", "pass") if value
61   end
62 end
body() click to toggle source
   # File lib/fixturama/changes/request.rb
53 def body
54   with_error("body") { maybe_regexp options[:body] }
55 end
headers() click to toggle source
   # File lib/fixturama/changes/request.rb
41 def headers
42   with_error("headers") do
43     Hash(options[:headers]).transform_keys(&:to_s) if options.key? :headers
44   end
45 end
http_method() click to toggle source
   # File lib/fixturama/changes/request.rb
28 def http_method
29   with_error("http method") do
30     value = with_error("method") { options[:method]&.to_sym&.downcase }
31     value ||= :any
32     raise("Invalid HTTP method") unless HTTP_METHODS.include?(value)
33     value
34   end
35 end
maybe_regexp(str) click to toggle source
   # File lib/fixturama/changes/request.rb
84 def maybe_regexp(str)
85   return unless str
86 
87   str = str.to_s
88   str[%r{\A/.*/\z}] ? Regexp.new(str[1..-2]) : str
89 end
query() click to toggle source
   # File lib/fixturama/changes/request.rb
47 def query
48   with_error("query") do
49     Hash(options[:query]).transform_keys(&:to_s) if options.key?(:query)
50   end
51 end
request() click to toggle source
   # File lib/fixturama/changes/request.rb
64 def request
65   @request ||= {
66     headers: headers,
67     body: body,
68     query: query,
69     basic_auth: basic_auth
70   }.select { |_, val| val }
71 end
responses() click to toggle source
   # File lib/fixturama/changes/request.rb
73 def responses
74   @responses ||= Responses.new(options[:response] || options[:responses])
75 end
uri() click to toggle source
   # File lib/fixturama/changes/request.rb
37 def uri
38   with_error("uri") { maybe_regexp(options[:uri] || options[:url]) }
39 end
with_error(part = nil) { || ... } click to toggle source
   # File lib/fixturama/changes/request.rb
77 def with_error(part = nil)
78   yield
79 rescue StandardError => err
80   part = ["a valid request", part].compact.join(" ")
81   raise Fixturama::FixtureError.new(part, options, err)
82 end