Sha256: b7b0f62986f156f9e1bae27c239f9b4d0baf3250de50c97ba92d83261160c650

Contents?: true

Size: 1.84 KB

Versions: 20

Compression:

Stored size: 1.84 KB

Contents

module HoptoadNotifier
  # Sends out the notice to Hoptoad
  class Sender

    NOTICES_URI = '/notifier_api/v2/notices/'.freeze

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

    # Sends the notice data off to Hoptoad for processing.
    #
    # @param [String] data The XML notice to be sent off
    def send_to_hoptoad(data)
      logger.debug { "Sending request to #{url.to_s}:\n#{data}" } if logger

      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
      http.use_ssl      = secure

      response = begin
                   http.post(url.path, data, HEADERS)
                 rescue TimeoutError => e
                   log :error, "Timeout while contacting the Hoptoad server."
                   nil
                 end

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

    private

    attr_reader :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol,
      :host, :port, :secure, :http_open_timeout, :http_read_timeout

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

    def log(level, message, response = nil)
      logger.send level, LOG_PREFIX + message if logger
      HoptoadNotifier.report_environment_info
      HoptoadNotifier.report_response_body(response.body) if response && response.respond_to?(:body)
    end

    def logger
      HoptoadNotifier.logger
    end

  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
hoptoad_notifier-2.3.7 lib/hoptoad_notifier/sender.rb
riscfuture-hoptoad_notifier-2.3.6 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.6 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.5 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.4 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.3 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.2 lib/hoptoad_notifier/sender.rb
jordan-brough-hoptoad_notifier-2.3.0 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.3.0 lib/hoptoad_notifier/sender.rb
hatless-hoptoad_notifier-2.2.6.1 lib/hoptoad_notifier/sender.rb
hatless-hoptoad_notifier-2.2.6.001 lib/hoptoad_notifier/sender.rb
hatless-hoptoad_notifier-2.2.6 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.2.6 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.2.5 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.2.2 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.2.0 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.1.3 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.1.2 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.1.1 lib/hoptoad_notifier/sender.rb
hoptoad_notifier-2.1.0 lib/hoptoad_notifier/sender.rb