Class: FaradaySimulation::Stub
- Inherits:
-
Faraday::Adapter::Test::Stub
- Object
- Faraday::Adapter::Test::Stub
- FaradaySimulation::Stub
- Defined in:
- lib/faraday_simulation/stub.rb
Instance Attribute Summary collapse
-
#segment_names ⇒ Object
Returns the value of attribute segment_names.
-
#segments ⇒ Object
Returns the value of attribute segments.
Instance Method Summary collapse
-
#initialize(full, body, block) ⇒ Stub
constructor
A new instance of Stub.
- #matches?(request_uri, request_body) ⇒ Boolean
- #process_path ⇒ Object
Constructor Details
#initialize(full, body, block) ⇒ Stub
Returns a new instance of Stub.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/faraday_simulation/stub.rb', line 6 def initialize(full, body, block) self.segment_names = [] query = nil if full.kind_of?(String) self.path, query = full.split('?') process_path else self.path = full end self.params = query ? Faraday::Utils.parse_nested_query(query) : {} self.body = body self.block = Proc.new { |env| env[:params].update(body_params(env)) segment_names.each { |name| env[:params][name] = self.segments.shift } env[:segments] = self.segments block.call(env) } end |
Instance Attribute Details
#segment_names ⇒ Object
Returns the value of attribute segment_names.
3 4 5 |
# File 'lib/faraday_simulation/stub.rb', line 3 def segment_names @segment_names end |
#segments ⇒ Object
Returns the value of attribute segments.
3 4 5 |
# File 'lib/faraday_simulation/stub.rb', line 3 def segments @segments end |
Instance Method Details
#matches?(request_uri, request_body) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/faraday_simulation/stub.rb', line 28 def matches?(request_uri, request_body) request_path, request_query = request_uri.split('?') request_params = request_query ? Faraday::Utils.parse_nested_query(request_query) : {} md = request_path.match(self.path) if ( (md && md[0] == request_path) && params_match?(request_params) && (body.to_s.size.zero? || request_body == body) ) self.segments = md.to_a.slice(1, md.size).collect { |s| CGI.unescape(s) } true else false end end |
#process_path ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/faraday_simulation/stub.rb', line 47 def process_path self.path += '/' unless path.match(/\/$/) self.path.gsub!(/:([^\/\.]+)/) { self.segment_names << $1 '([^\/\.]+)' } self.path = Regexp.new(self.path) if segment_names.any? end |