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

Returns a new instance of Faker.



10
11
12
13
14
15
16
# File 'lib/web/faker.rb', line 10

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)


19
20
21
22
23
# File 'lib/web/faker.rb', line 19

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

#record(code, body, headers) ⇒ Object

Given a response, marshall down and record in redis



26
27
28
29
30
31
32
33
# File 'lib/web/faker.rb', line 26

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



37
38
39
40
41
42
43
# File 'lib/web/faker.rb', line 37

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