Class: Web::Faker

Inherits:
Object
  • Object
show all
Defined in:
lib/web/faker.rb

Overview

A class for representing one faked response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, body, headers) ⇒ Faker

method is expected to be a symbol, downcase url is expected to be a full url, trailing slash chopped off body is expected to be nil or a string headers is expected to be a hash



14
15
16
17
18
19
20
# File 'lib/web/faker.rb', line 14

def initialize(method, url, body, headers)
  @key = "#{method}:#{url}"
  @cache = Web.cache
  # keep these around
  @url = url
  @method = method
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/web/faker.rb', line 8

def cache
  @cache
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/web/faker.rb', line 8

def key
  @key
end

Instance Method Details

#desired?Boolean

whether or not this is a key we want

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/web/faker.rb', line 23

def desired?
  @match = Web.registered.detect do |opt|
    opt[:regex] =~ @url &&
    (opt[:method] === @method || opt[:method] == :any)
  end
end

#record(code, body, headers) ⇒ Object

Given a response, marshall down and record in redis code is expected to be Fixnum body is expected to be a string or nil headers is expected to be a hash



34
35
36
37
38
39
40
41
# File 'lib/web/faker.rb', line 34

def record(code, body, headers)
  # save and return the response
  res = Web::Response.new code, body, headers
  # Allow expireation to be set
  expires = @match.has_key?(:expire) ? @match[:expire].to_i : nil
  cache.set(key, res.dump, expires)
  res
end

#response_forObject

Get the mashalled form from redis and reconstruct into a Web::Response



45
46
47
48
49
50
51
# File 'lib/web/faker.rb', line 45

def response_for
  if data = cache.get(key)
    Web::Response.load(data)
  else
    nil
  end
end