Class: RestClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/restclient/components.rb

Overview

Reopen the RestClient::Request class to add a level of indirection in order to create the stack of Rack middleware.

Instance Method Summary collapse

Instance Method Details

#execute(&block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/restclient/components.rb', line 100

def execute(&block)
  uri = URI.parse(@url)
  # minimal rack spec
  env = {
    "restclient.hash" => {
      :request => self,
      :error => nil,
      :block => block
    },
    "REQUEST_METHOD" => @method.to_s.upcase,
    "SCRIPT_NAME" => "",
    "PATH_INFO" => uri.path || "/",
    "QUERY_STRING" => uri.query || "",
    "SERVER_NAME" => uri.host,
    "SERVER_PORT" => uri.port.to_s,
    "rack.version" => ::Rack::VERSION,
    "rack.run_once" => false,
    "rack.multithread" => true,
    "rack.multiprocess" => true,
    "rack.url_scheme" => uri.scheme,
    "rack.input" => payload || StringIO.new().set_encoding("ASCII-8BIT"),
    "rack.errors" => $stderr
  }
  @processed_headers.each do |key, value|
    env.merge!("HTTP_"+key.to_s.gsub("-", "_").upcase => value)
  end
  if content_type = env.delete('HTTP_CONTENT_TYPE')
    env['CONTENT_TYPE'] = content_type
  end
  if content_length = env.delete('HTTP_CONTENT_LENGTH')
    env['CONTENT_LENGTH'] = content_length
  end
  stack = RestClient::RACK_APP
  RestClient.components.each do |(component, args)|
    if (args || []).empty?
      stack = component.new(stack)
    else
      stack = component.new(stack, *args)
    end
  end
  response = stack.call(env)
  # allow to use the response block, even if not using the Compatibility component
  unless RestClient.enabled?(RestClient::Rack::Compatibility)
    if block = env['restclient.hash'][:block]
      block.call(response)
    end
  end
  response
end

#original_executeObject



99
# File 'lib/restclient/components.rb', line 99

alias_method :original_execute, :execute