Class: StaleFish::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/stale_fish/fixture.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_againstObject

Returns the value of attribute check_against.



5
6
7
# File 'lib/stale_fish/fixture.rb', line 5

def check_against
  @check_against
end

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/stale_fish/fixture.rb', line 3

def file
  @file
end

#last_updated_atObject

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

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/stale_fish/fixture.rb', line 3

def name
  @name
end

#request_typeObject

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_intervalObject

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_methodObject

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

Returns:

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



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_methodObject



32
33
34
# File 'lib/stale_fish/fixture.rb', line 32

def update_by_method
  return eval(update_method)
end

#update_by_networkObject



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