Class: Twilio::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/framework/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/twilio-ruby/framework/request.rb', line 7

def initialize(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
  @host = host
  @port = port
  @url = url
  @method = method
  @params = params
  @data = data
  @headers = headers
  @auth = auth
  @timeout = timeout
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def auth
  @auth
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def host
  @host
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def params
  @params
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/twilio-ruby/framework/request.rb', line 5

def url
  @url
end

Instance Method Details

#to_sObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/twilio-ruby/framework/request.rb', line 19

def to_s
  auth = @auth.nil? ? '' : '(' + @auth.join(',') + ')'

  params = ''
  unless @params.nil? || @params.empty?
    params = '?' + @params.each.map { |key, value| "#{CGI.escape(key)}=#{CGI.escape(value)}" }.join('&')
  end

  headers = ''
  unless @headers.nil? || @headers.empty?
    headers = "\n" + @headers.each.map { |key, value| "-H \"#{key}\": \"#{value}\"" }.join("\n")
  end

  data = ''
  unless @data.nil? || @data.empty?
    data = @method.equal?('GET') ? "\n -G" : "\n"
    data += case @headers['Content-Type']
            when 'application/x-www-form-urlencoded'
              @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n")
            when 'application/json'
              "-d '#{JSON.generate(@data)}'"
            else
              @data.each.map { |key, value| "-d \"#{key}\"=\"#{value}\"" }.join("\n")
            end
  end

  "#{auth} #{@method} #{@url}#{params}#{data}#{headers}"
end