Class: TripwireNotifier::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/tripwire_notifier/sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Sender

Returns a new instance of Sender.



5
6
7
# File 'lib/tripwire_notifier/sender.rb', line 5

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



3
4
5
# File 'lib/tripwire_notifier/sender.rb', line 3

def configuration
  @configuration
end

Instance Method Details

#hostObject



17
18
19
# File 'lib/tripwire_notifier/sender.rb', line 17

def host
  'api.tripwireapp.com'
end

#portObject



13
14
15
# File 'lib/tripwire_notifier/sender.rb', line 13

def port
  self.configuration.secure? ? 443 : 80
end

#protocolObject



9
10
11
# File 'lib/tripwire_notifier/sender.rb', line 9

def protocol
  self.configuration.secure? ? 'https' : 'http'
end

#send_to_tripwire(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tripwire_notifier/sender.rb', line 25

def send_to_tripwire(data)
  uri = self.uri

  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = self.configuration.timeout_in_seconds
  http.use_ssl      = self.configuration.secure?
  http.verify_mode  = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(data)

  begin
    response = http.request(request)
  rescue Exception => ex
    warn "Could not submit tripwireapp notification: #{ex.class} - #{ex.message}"
  end
end

#uriObject



21
22
23
# File 'lib/tripwire_notifier/sender.rb', line 21

def uri
  URI.parse("#{protocol}://#{host}:#{port}").merge('/')
end