Class: Songkick::Transport::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/songkick/transport/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, verb, path, params, headers = {}, timeout = DEFAULT_TIMEOUT) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def endpoint
  @endpoint
end

#errorObject

Returns the value of attribute error.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def error
  @error
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def path
  @path
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def response
  @response
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def start_time
  @start_time
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def timeout
  @timeout
end

#verbObject (readonly) Also known as: http_method

Returns the value of attribute verb.



5
6
7
# File 'lib/songkick/transport/request.rb', line 5

def verb
  @verb
end

Instance Method Details

#bodyObject



58
59
60
61
62
63
64
65
# 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

#durationObject



41
42
43
44
# 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

#headersObject



46
47
48
# File 'lib/songkick/transport/request.rb', line 46

def headers
  @headers.to_hash
end

#multipart?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/songkick/transport/request.rb', line 54

def multipart?
  @multipart
end

#to_sObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/songkick/transport/request.rb', line 71

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?
  query = Serialization.build_query_string(params, true, true)
  command << " -d '#{query}'"
  command
end

#urlObject



67
68
69
# File 'lib/songkick/transport/request.rb', line 67

def url
  Serialization.build_url(@verb, @endpoint, @path, @params)
end

#use_body?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/songkick/transport/request.rb', line 50

def use_body?
  USE_BODY.include?(@verb)
end