Class: Fluke::Watcher
- Inherits:
-
Object
- Object
- Fluke::Watcher
- Includes:
- DataMapper::Resource
- Defined in:
- lib/fluke/watcher.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #content_extractor(&block) ⇒ Object
- #extract_content(content) ⇒ Object
- #generate_url ⇒ Object
- #last_result(checksum = nil) ⇒ Object
- #run ⇒ Object
- #run_file(path) ⇒ Object
- #run_http(generated_url) ⇒ Object
- #to_s ⇒ Object
- #url_generator(&block) ⇒ Object
- #url_strftime(offset = 0) ⇒ Object
- #wait ⇒ Object
Instance Attribute Details
#thread ⇒ Object
Returns the value of attribute thread.
19 20 21 |
# File 'lib/fluke/watcher.rb', line 19 def thread @thread end |
Instance Method Details
#content_extractor(&block) ⇒ Object
80 81 82 83 |
# File 'lib/fluke/watcher.rb', line 80 def content_extractor(&block) @extract_content = true @content_extractor = block end |
#extract_content(content) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/fluke/watcher.rb', line 85 def extract_content(content) if @extract_content return @content_extractor.call(content) else return content end end |
#generate_url ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fluke/watcher.rb', line 68 def generate_url if @generate_url if @url_generator.arity then return @url_generator.call(self.url) else return @url_generator.call end else return self.url end end |
#last_result(checksum = nil) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/fluke/watcher.rb', line 103 def last_result(checksum = nil) if checksum results.last(:checksum => checksum, :order => [ :created_at.desc ]) else results.last(:order => [ :created_at.desc ]) end end |
#run ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fluke/watcher.rb', line 21 def run Thread.new do begin generated_url = self.generate_url Fluke::log { "#{self}: generated URL: #{generated_url}" } Thread.current[:result] = \ if generated_url =~ /^file\:\/\/(.+)$/ run_file File.($1) else run_http generated_url end rescue => e Fluke.log { "#{self}: failed to run: #{e.inspect}; backtrace: #{e.backtrace.join("; ")}" } end end end |
#run_file(path) ⇒ Object
39 40 41 42 |
# File 'lib/fluke/watcher.rb', line 39 def run_file(path) result = Result.from_response \ :watcher => self, :content => extract_content(File.read(path)), :generated_url => "file://#{path}" end |
#run_http(generated_url) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fluke/watcher.rb', line 44 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 => extract_content(res.body.dump.dup), :generated_url => generated_url, :mime_type => res.header['Content-Type'] end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/fluke/watcher.rb', line 99 def to_s "Watcher<#{name}>" end |
#url_generator(&block) ⇒ Object
57 58 59 60 |
# File 'lib/fluke/watcher.rb', line 57 def url_generator(&block) @generate_url = true @url_generator = block end |
#url_strftime(offset = 0) ⇒ Object
62 63 64 65 66 |
# File 'lib/fluke/watcher.rb', line 62 def url_strftime(offset = 0) self.url_generator do |raw| (Time.now + offset/1000.000).strftime(raw) end end |