class Songkick::Transport::Request

Constants

TRUNCATED_PARAM_LENGTH

Attributes

endpoint[R]
error[R]
http_method[R]
params[R]
path[R]
response[R]
start_time[R]
timeout[R]
verb[R]

Public Class Methods

new(endpoint, verb, path, params, headers = {}, timeout = DEFAULT_TIMEOUT) click to toggle source
# File lib/songkick/transport/request.rb, line 16
def initialize(endpoint, verb, path, params, headers = {}, timeout = DEFAULT_TIMEOUT)
  @endpoint   = endpoint
  @verb       = verb.to_s.downcase
  @path       = path
  @headers    = Headers.new(headers)
  @params     = params
  @timeout    = timeout
  @start_time = start_time || Time.now
  @multipart  = Serialization.multipart?(params)

  if use_body?
    @headers['Content-Type'] ||= @multipart ? multipart_request[:content_type] : FORM_ENCODING
  end
end

Public Instance Methods

body() click to toggle source
# File lib/songkick/transport/request.rb, line 58
def body
  return nil unless use_body?
  if @multipart
    multipart_request[:body]
  else
    Serialization.build_query_string(params)
  end
end
duration() click to toggle source
# File lib/songkick/transport/request.rb, line 41
def duration
  return nil unless @end_time
  (@end_time.to_f - @start_time.to_f) * 1000
end
error=(error) click to toggle source
# File lib/songkick/transport/request.rb, line 36
def error=(error)
  @error = error
  @end_time = Time.now
end
headers() click to toggle source
# File lib/songkick/transport/request.rb, line 46
def headers
  @headers.to_hash
end
multipart?() click to toggle source
# File lib/songkick/transport/request.rb, line 54
def multipart?
  @multipart
end
response=(response) click to toggle source
# File lib/songkick/transport/request.rb, line 31
def response=(response)
  @response = response
  @end_time = Time.now
end
to_s() click to toggle source
# File lib/songkick/transport/request.rb, line 72
def to_s
  url = String === @endpoint ?
        Serialization.build_url(@verb, @endpoint, @path, @params, true) :
        @endpoint.to_s

  command = "#{@verb.upcase} '#{url}'"
  @headers.each do |key, value|
    value = Serialization::SANITIZED_VALUE if Serialization.sanitize?(key)
    command << " -H '#{key}: #{value}'"
  end
  return command unless use_body?
  sanitized_params = Serialization.build_query_string(params, false, true)
  if String === sanitized_params
    command << " -d '#{sanitized_params}'"
    return command
  end
  sanitized_params = sanitized_params.inject({}) do |result, param|
    key, value = param
    if value.respond_to?(:length) && value.length > TRUNCATED_PARAM_LENGTH
      result[key] = "#{value[0...TRUNCATED_PARAM_LENGTH]}[TRUNCATED]"
    else
      result[key] = value
    end
    result
  end
  query = sanitized_params.map { |p| p.join('=') }.join('&')
  command << " -d '#{query}'"
  command
end
url() click to toggle source
# File lib/songkick/transport/request.rb, line 67
def url
  Serialization.build_url(@verb, @endpoint, @path, @params)
end
use_body?() click to toggle source
# File lib/songkick/transport/request.rb, line 50
def use_body?
  USE_BODY.include?(@verb)
end

Private Instance Methods

multipart_request() click to toggle source
# File lib/songkick/transport/request.rb, line 104
def multipart_request
  return nil unless @multipart
  @multipart_request ||= Serialization.serialize_multipart(params)
end