Class: WatchJob

Inherits:
Object
  • Object
show all
Includes:
LogHelper, WatchConfig
Defined in:
lib/file_watcher/watch_job.rb

Constant Summary collapse

@@registered_jobs =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LogHelper

#log

Methods included from WatchConfig

included

Instance Attribute Details

#watch_actionObject

Returns the value of attribute watch_action.



11
12
13
# File 'lib/file_watcher/watch_job.rb', line 11

def watch_action
  @watch_action
end

#watch_criteriaObject

Returns the value of attribute watch_criteria.



11
12
13
# File 'lib/file_watcher/watch_job.rb', line 11

def watch_criteria
  @watch_criteria
end

Class Method Details

.registered_jobsObject



15
16
17
# File 'lib/file_watcher/watch_job.rb', line 15

def self.registered_jobs
  @@registered_jobs 
end

Instance Method Details

#do_post(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/file_watcher/watch_job.rb', line 19

def do_post event
  if watch_action[:http][:ssl] 
    protocol = 'https'
  else
    protocol = 'http'
  end

  url = "#{protocol}://#{watch_action[:http][:hostname]}:#{watch_action[:http][:port]}#{watch_action[:http][:uri]}"
  uri = URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port)
  if watch_action[:http][:ssl] 
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  body = watch_action[:http][:body].merge({ :args => watch_action[:http][:args].call(event.name) } )

  log "URL: #{url}"
  log "body: #{body.to_json}"

  log "Path: #{uri.path}"
  log "URI: #{uri.inspect}"
  log "Port: #{uri.port}"

  req = Net::HTTP::Post.new(uri.path)
  if watch_action[:http][:auth_user]
    log "Setting auth..."
    req.basic_auth watch_action[:http][:auth_user], watch_action[:http][:auth_pass]
  end

  req.set_form_data({'body' => body.to_json })
  log "SENDING: #{req.inspect}"
  res = http.request(req)
  log "Res: #{res.inspect}"
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    # OK
    log "Req was ok"
  else
    res.error!
  end

  log "Response: #{res}"
end

#event_handler(event) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/file_watcher/watch_job.rb', line 65

def event_handler event
  return if watch_criteria[:file_glob] && !event.name =~ watch_criteria[:file_glob]
    
  if watch_action.keys.first == :http && watch_action[:http][:method] == :post
    return do_post event
  end

  raise "Error: unsupported watch action in: #{watch_action.inspect}"
end