class Squash::Uploader

Class that handles communicating with the Squash API.

By default, transmission is done with `Net::HTTP`. If this is unpalatable to you, reimplement {#http_post}.

Constants

DEFAULT_CONFIGURATION

Default configuration options. See {#initialize}.

Attributes

host[RW]

@return [String] The host name and scheme of the Squash server (e.g.,

"https://squash.mycompany.com").
options[RW]

@return [Hash<Symbol, Object>] Additional options for uploading.

Public Class Methods

new(host, options={}) click to toggle source

Creates a new Squash uploader that will communicate with a given host.

@param [String] host The host name and scheme of the Squash server (e.g.,

"https://squash.mycompany.com").

@param [Hash] options Additional options. @option options [Fixnum] :open_timeout (15) The number of seconds to wait

when opening a connection to the Squash server.

@option options [Fixnum] :read_timeout (15) The number of seconds to wait

when waiting for data from the Squash server.

@option options [true, false] :skip_verification (false) If `true`, SSL

peer verification will not be performed.

@option options [Array<Class, Fixnum>] :success ([Net::HTTPSuccess]) A

list of subclasses of `Net::HTTPResponse` or response codes that are
considered successful and will not raise an exception.
# File lib/squash/uploader.rb, line 58
def initialize(host, options={})
  @host    = host
  @options = DEFAULT_CONFIGURATION.merge(options)
end

Public Instance Methods

transmit(path, data) click to toggle source

Transmits information to Squash.

@param [String] path The path portion of the URL, with leading slash. @param [Hash] data Data to JSON-serialize and place in the request body.

# File lib/squash/uploader.rb, line 68
def transmit(path, data)
  http_post (host + path),
            {'Content-Type' => 'application/json'},
            [data.to_json]
end

Protected Instance Methods

http_post(url, headers, bodies) click to toggle source

Override this method to use your favorite HTTP library. This method receives an array of bodies. It is intended that each element of the array be transmitted as a separate POST request, not that the bodies be concatenated and sent as one request.

A response of code found in the `:success` option is considered successful.

@param [String] url The URL to POST to. @param [Hash<String, String>] headers The request headers. @param [Array<String>] bodies The bodies of each request to POST. @raise [StandardError] If a response other than 2xx or 422 is returned.

# File lib/squash/uploader.rb, line 88
def http_post(url, headers, bodies)
  uri               = URI.parse(url)
  http              = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl      = (uri.scheme == 'https')
  http.open_timeout = options[:open_timeout]
  http.read_timeout = options[:read_timeout]
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:skip_verification]

  http.start do |session|
    bodies.each do |body|
      request = Net::HTTP::Post.new(uri.request_uri)
      headers.each { |k, v| request.add_field k, v }
      request.body = body
      response     = session.request(request)

      if options[:success].none? { |cl|
        if cl.kind_of?(Class)
          response.kind_of?(cl)
        elsif cl.kind_of?(Fixnum) || cl.kind_of?(String)
          response.code.to_i == cl.to_i
        else
          raise ArgumentError, "Unknown :success value #{cl}"
        end
      }
        raise "Unexpected response from Squash host: #{response.code}"
      end
    end
  end
end