Class: Web::Faker
- Inherits:
-
Object
- Object
- Web::Faker
- Defined in:
- lib/web/faker.rb
Overview
A class for representing one faked response
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#desired? ⇒ Boolean
whether or not this is a key we want.
-
#initialize(method, url, body, headers) ⇒ Faker
constructor
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.
-
#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.
-
#response_for ⇒ Object
Get the mashalled form from redis and reconstruct into a Web::Response.
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
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
8 9 10 |
# File 'lib/web/faker.rb', line 8 def cache @cache end |
#key ⇒ Object (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
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 |