Class: Zenbox::Sender

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

Overview

Sends out the notice to Zenbox

Constant Summary collapse

CUSTOMERS_URI =
'/customers.json'.freeze
HTTP_ERRORS =
[Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Errno::ECONNREFUSED].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sender

Returns a new instance of Sender.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zenbox/sender.rb', line 15

def initialize(options = {})
  [ :proxy_host,
    :proxy_port,
    :proxy_user,
    :proxy_pass,
    :protocol,
    :host,
    :port,
    :secure,
    :use_system_ssl_cert_chain,
    :http_open_timeout,
    :http_read_timeout
  ].each do |option|
    instance_variable_set("@#{option}", options[option])
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def host
  @host
end

#http_open_timeoutObject (readonly)

Returns the value of attribute http_open_timeout.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutObject (readonly)

Returns the value of attribute http_read_timeout.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def http_read_timeout
  @http_read_timeout
end

#portObject (readonly)

Returns the value of attribute port.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def protocol
  @protocol
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def proxy_host
  @proxy_host
end

#proxy_passObject (readonly)

Returns the value of attribute proxy_pass.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def proxy_pass
  @proxy_pass
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def proxy_port
  @proxy_port
end

#proxy_userObject (readonly)

Returns the value of attribute proxy_user.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def proxy_user
  @proxy_user
end

#secureObject (readonly) Also known as: secure?

Returns the value of attribute secure.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def secure
  @secure
end

#use_system_ssl_cert_chainObject (readonly) Also known as: use_system_ssl_cert_chain?

Returns the value of attribute use_system_ssl_cert_chain.



64
65
66
# File 'lib/zenbox/sender.rb', line 64

def use_system_ssl_cert_chain
  @use_system_ssl_cert_chain
end

Instance Method Details

#send_to_zenbox(data) ⇒ Object

Sends the customer data off to Zenbox for processing.

Parameters:

  • data (String)

    The JSON notice to be sent off



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
# File 'lib/zenbox/sender.rb', line 35

def send_to_zenbox(data)
  http = setup_http_connection

  # Set up POST request with formdata and headers
  request = Net::HTTP::Post.new(url.path)
  request.set_form_data(data)
  request.add_field('Accept', 'text/json, application/json')

  response = begin
               http.request(request)
             rescue *HTTP_ERRORS => e
               log :error, "Timeout while contacting the Zenbox server."
               nil
             end

  case response
  when Net::HTTPSuccess then
    log :info, "Success: #{response.class}", response
    true
  else
    log :error, "Failure: #{response.class}", response
    false
  end

rescue => e
  log :error, "[Zenbox::Sender#send_to_zenbox] Cannot send notification. Error: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
  nil
end