Class: Rack::Client::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rack/client/adapter/base.rb

Direct Known Subclasses

Simple

Constant Summary collapse

ASCII_ENCODING =
'ASCII-8BIT'

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/rack/client/adapter/base.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#build_env(request_method, url, headers = {}, body = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rack/client/adapter/base.rb', line 30

def build_env(request_method, url,  headers = {}, body = nil)
  env = Headers.new(headers).to_env

  env.update 'REQUEST_METHOD' => request_method

  env['CONTENT_TYPE'] ||= 'application/x-www-form-urlencoded'

  uri = URI.parse(url)

  path_info = uri.path.empty? ? '/' : uri.path

  env.update 'PATH_INFO'    => path_info
  env.update 'REQUEST_URI'  => uri.to_s
  env.update 'SERVER_NAME'  => uri.host.to_s
  env.update 'SERVER_PORT'  => uri.port.to_s
  env.update 'SCRIPT_NAME'  => ''
  env.update 'QUERY_STRING' => uri.query.to_s

  input  = ensure_acceptable_input(body)
  errors = StringIO.new

  [ input, errors ].each do |io|
    io.set_encoding(ASCII_ENCODING) if io.respond_to?(:set_encoding)
  end

  env.update 'rack.input'         => input
  env.update 'rack.errors'        => errors
  env.update 'rack.url_scheme'    => uri.scheme || 'http'
  env.update 'rack.version'       => Rack::VERSION
  env.update 'rack.multithread'   => true
  env.update 'rack.multiprocess'  => true
  env.update 'rack.run_once'      => false

  env.update 'HTTPS'  => env["rack.url_scheme"] == "https" ? "on" : "off"

  env
end

#ensure_acceptable_input(body) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/client/adapter/base.rb', line 68

def ensure_acceptable_input(body)
  if %w[gets each read rewind].all? {|m| body.respond_to?(m.to_sym) }
    body
  elsif body.respond_to?(:each)
    input = StringIO.new
    body.each {|chunk| input << chunk }
    input.rewind
    input
  else
    input = StringIO.new(body.to_s)
    input.rewind
    input
  end
end

#request(method, url, headers = {}, body = nil) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rack/client/adapter/base.rb', line 22

def request(method, url, headers = {}, body = nil)
  if block_given?
    call(build_env(method.upcase, url, headers, body)) {|tuple| yield *tuple }
  else
    return *call(build_env(method.upcase, url, headers, body))
  end
end