Class: Fluke::Watcher

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/fluke/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#threadObject

Returns the value of attribute thread.



9
10
11
# File 'lib/fluke/watcher.rb', line 9

def thread
  @thread
end

Instance Method Details

#generate_urlObject



48
49
50
51
52
53
54
# File 'lib/fluke/watcher.rb', line 48

def generate_url
  if @generate_url
    return @url_generator.call(self.url)
  else
    return self.url
  end
end

#last_result(checksum = nil) ⇒ Object



64
65
66
67
# File 'lib/fluke/watcher.rb', line 64

def last_result(checksum = nil)
  cond = [ "checksum = ?", checksum ] if checksum
  results.find(:first, :conditions => cond, :order => "created_at desc")
end

#runObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/fluke/watcher.rb', line 11

def run
  generated_url = self.generate_url
  Fluke::log { "#{self}: generated URL: #{generated_url}" }

  if generated_url =~ /^file\:\/\/(.+)$/
    run_file File.expand_path($1)
  else
    run_http generated_url
  end
end

#run_file(path) ⇒ Object



22
23
24
# File 'lib/fluke/watcher.rb', line 22

def run_file(path)
  result = Result.from_response :watcher => self, :content => File.read(path), :generated_url => "file://#{path}"
end

#run_http(generated_url) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/fluke/watcher.rb', line 26

def run_http(generated_url)
  hc = HTTPClient.new :proxy => Fluke.conf[:proxy_string], :user_agent => "Fluke/#{Fluke::VERSION}"
  http_method = self.meth.downcase.to_sym
  unless hc.respond_to?(http_method)
    Fluke::log { "#{self}: invalid method '#{http_method}', converting to :get" }
    http_method = :get
  end
  res = hc.send(http_method, generated_url, { 'Cache-Control' => 'no-cache', 'Pragma' => 'no-cache' })
  result = Result.from_response :watcher => self, :content => res.body.dump.dup, :generated_url => generated_url, :mime_type => res.header['Content-Type']
end

#to_sObject



60
61
62
# File 'lib/fluke/watcher.rb', line 60

def to_s
  "Watcher<#{name}>"
end

#url_generator(&block) ⇒ Object



37
38
39
40
# File 'lib/fluke/watcher.rb', line 37

def url_generator(&block)
  @generate_url = true
  @url_generator = block
end

#url_strftime(offset = 0) ⇒ Object



42
43
44
45
46
# File 'lib/fluke/watcher.rb', line 42

def url_strftime(offset = 0)
  self.url_generator do |raw|
    (Time.now + offset/1000.000).strftime(raw)
  end
end

#waitObject



56
57
58
# File 'lib/fluke/watcher.rb', line 56

def wait
  sleep self.delay/1000.000
end