Class: RestFtpDaemon::Notification

Inherits:
Common
  • Object
show all
Defined in:
lib/rest-ftp-daemon/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotification

Returns a new instance of Notification.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rest-ftp-daemon/notification.rb', line 14

def initialize
#def initialize(job, signal, error, status)
  # Grab params
  #@job = job
  # @signal = signal
  # @error = error
  # @status = status
  @status = {}
  @error = 0
  @message = nil

  # Generate a random key
  @key = SecureRandom.hex(2)

  # Call super
  super

end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/rest-ftp-daemon/notification.rb', line 7

def error
  @error
end

#jobObject

Returns the value of attribute job.



11
12
13
# File 'lib/rest-ftp-daemon/notification.rb', line 11

def job
  @job
end

#job_idObject

Returns the value of attribute job_id.



5
6
7
# File 'lib/rest-ftp-daemon/notification.rb', line 5

def job_id
  @job_id
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/rest-ftp-daemon/notification.rb', line 12

def key
  @key
end

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/rest-ftp-daemon/notification.rb', line 8

def message
  @message
end

#signalObject

Returns the value of attribute signal.



6
7
8
# File 'lib/rest-ftp-daemon/notification.rb', line 6

def signal
  @signal
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/rest-ftp-daemon/notification.rb', line 9

def status
  @status
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/rest-ftp-daemon/notification.rb', line 10

def url
  @url
end

Instance Method Details

#notifyObject

def status key, val

@status[key.to_s] = val

end



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rest-ftp-daemon/notification.rb', line 42

def notify
  # Check context
  raise NotificationMissingUrl unless @url
  raise NotificationMissingSignal unless @signal
  #sraise NotifImpossible unless @status

  # Params
  params = {
    id: @job_id,
    host: get_hostname,
    signal: @signal,
    error: @error,
    }

  # Add status only if present
  params["status"] = @status unless @status.empty?
  params["message"] = @message unless @message.to_s.blank?

  # Log this notification
  info "notify params: #{params.inspect}"

  # Prepare query
  uri = URI(@url)
  headers = {"Content-Type" => "application/json",
            "Accept" => "application/json"}

  # Post the notification
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.post(uri.path, params.to_json, headers)

  info "notify reply: #{response.body.strip}"
end

#prognameObject



33
34
35
# File 'lib/rest-ftp-daemon/notification.rb', line 33

def progname
  "NOTIF #{@key}"
end