Class: FaradaySimulation::Stub

Inherits:
Faraday::Adapter::Test::Stub
  • Object
show all
Defined in:
lib/faraday_simulation/stub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_namesObject

Returns the value of attribute segment_names.



3
4
5
# File 'lib/faraday_simulation/stub.rb', line 3

def segment_names
  @segment_names
end

#segmentsObject

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

Returns:

  • (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_pathObject



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