Class: StaleFish::Fixture
- Inherits:
-
Object
- Object
- StaleFish::Fixture
- Defined in:
- lib/stale_fish/fixture.rb
Instance Attribute Summary collapse
-
#check_against ⇒ Object
Returns the value of attribute check_against.
-
#file ⇒ Object
Returns the value of attribute file.
-
#last_updated_at ⇒ Object
Returns the value of attribute last_updated_at.
-
#name ⇒ Object
Returns the value of attribute name.
-
#request_type ⇒ Object
Returns the value of attribute request_type.
-
#update_interval ⇒ Object
Returns the value of attribute update_interval.
-
#update_method ⇒ Object
Returns the value of attribute update_method.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Fixture
constructor
A new instance of Fixture.
- #is_stale? ⇒ Boolean
- #register_lock! ⇒ Object
- #to_yaml ⇒ Object
- #update! ⇒ Object
- #update_by_method ⇒ Object
- #update_by_network ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Fixture
Returns a new instance of Fixture.
8 9 10 11 12 |
# File 'lib/stale_fish/fixture.rb', line 8 def initialize(attributes={}) attributes.each do |key, value| self.send("#{key}=", value) end end |
Instance Attribute Details
#check_against ⇒ Object
Returns the value of attribute check_against.
5 6 7 |
# File 'lib/stale_fish/fixture.rb', line 5 def check_against @check_against end |
#file ⇒ Object
Returns the value of attribute file.
3 4 5 |
# File 'lib/stale_fish/fixture.rb', line 3 def file @file end |
#last_updated_at ⇒ Object
Returns the value of attribute last_updated_at.
3 4 5 |
# File 'lib/stale_fish/fixture.rb', line 3 def last_updated_at @last_updated_at end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/stale_fish/fixture.rb', line 3 def name @name end |
#request_type ⇒ Object
Returns the value of attribute request_type.
5 6 7 |
# File 'lib/stale_fish/fixture.rb', line 5 def request_type @request_type end |
#update_interval ⇒ Object
Returns the value of attribute update_interval.
4 5 6 |
# File 'lib/stale_fish/fixture.rb', line 4 def update_interval @update_interval end |
#update_method ⇒ Object
Returns the value of attribute update_method.
6 7 8 |
# File 'lib/stale_fish/fixture.rb', line 6 def update_method @update_method end |
Instance Method Details
#is_stale? ⇒ Boolean
14 15 16 17 18 19 20 |
# File 'lib/stale_fish/fixture.rb', line 14 def is_stale? if last_updated_at.nil? || (last_updated_at + eval(update_interval)) < DateTime.now return true else return false end end |
#register_lock! ⇒ Object
48 49 50 51 52 53 |
# File 'lib/stale_fish/fixture.rb', line 48 def register_lock! uri, type = build_uri, request_type.downcase.to_sym FakeWeb.register_uri(type, uri, :body => File.join(File.dirname(StaleFish.configuration), file)) return FakeWeb.registered_uri?(type, uri) end |
#to_yaml ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/stale_fish/fixture.rb', line 55 def to_yaml # update_interval.inspect trick is to prevent Fixnum being written yaml = "" yaml << <<-EOF - #{name}: file: '#{file}' update_interval: #{update_interval.inspect.gsub(/ /, '.').gsub(/"/, '')} check_against: #{check_against} request_type: #{request_type} EOF if update_method yaml << <<-EOF update_method: #{update_method} EOF end yaml << <<-EOF last_updated_at: #{last_updated_at} EOF return yaml end |
#update! ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/stale_fish/fixture.rb', line 22 def update! response = if update_method update_by_method else update_by_network end write_response_to_file(response) self.last_updated_at = DateTime.now end |
#update_by_method ⇒ Object
32 33 34 |
# File 'lib/stale_fish/fixture.rb', line 32 def update_by_method return eval(update_method) end |
#update_by_network ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/stale_fish/fixture.rb', line 36 def update_by_network uri, type = URI.parse(check_against), request_type.downcase.to_sym Net::HTTP.start(uri.host) do |http| response = if type == :post http.post(uri.path) else http.get(uri.path) end return response.body end end |