module Zenbox # Sends out the notice to Zenbox class Sender CUSTOMERS_URI = '/customers.json'.freeze HTTP_ERRORS = [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Errno::ECONNREFUSED].freeze 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 # Sends the customer data off to Zenbox for processing. # # @param [String] data The JSON notice to be sent off 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 attr_reader :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol, :host, :port, :secure, :use_system_ssl_cert_chain, :http_open_timeout, :http_read_timeout alias_method :secure?, :secure alias_method :use_system_ssl_cert_chain?, :use_system_ssl_cert_chain private def url URI.parse("#{protocol}://#{host}:#{port}").merge(CUSTOMERS_URI) end def log(level, message, response = nil) logger.send level, LOG_PREFIX + message if logger Zenbox.report_response_body(response.body) if response && response.respond_to?(:body) end def logger Zenbox.logger end def setup_http_connection http = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass). new(url.host, url.port) http.read_timeout = http_read_timeout http.open_timeout = http_open_timeout if secure? http.use_ssl = true # if use_system_ssl_cert_chain? && File.exist?(OpenSSL::X509::DEFAULT_CERT_FILE) # http.ca_file = OpenSSL::X509::DEFAULT_CERT_FILE # else # http.ca_file = Sender.local_cert_path # ca-bundle.crt built from source, see resources/README.md # end http.ca_file = Zenbox.configuration.ca_bundle_path http.verify_mode = OpenSSL::SSL::VERIFY_PEER else http.use_ssl = false end http rescue => e log :error, "[Zenbox::Sender#setup_http_connection] Failure initializing the HTTP connection.\nError: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}" raise e end end end