Class: HTTPAdapter::RackAdapter
- Inherits:
-
Object
- Object
- HTTPAdapter::RackAdapter
show all
- Includes:
- HTTPAdapter
- Defined in:
- lib/httpadapter/adapters/rack.rb
Instance Method Summary
collapse
#adapt_request, #adapt_response, #specialize_request, #specialize_response, #transmit, verified_request, verified_response
Constructor Details
#initialize(options = {}) ⇒ RackAdapter
Returns a new instance of RackAdapter.
26
27
28
29
30
31
|
# File 'lib/httpadapter/adapters/rack.rb', line 26
def initialize(options={})
options = {
:error_stream => $stderr
}.merge(options)
@error_stream = options[:error_stream]
end
|
Instance Method Details
#convert_request_from_a(request_ary) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/httpadapter/adapters/rack.rb', line 51
def convert_request_from_a(request_ary)
method, uri, , body = request_ary
env = {}
method = method.to_s.upcase
uri = Addressable::URI.parse(uri)
body_io = StringIO.new
body.each do |chunk|
unless chunk.kind_of?(String)
raise TypeError, "Expected String, got #{chunk.class}."
end
body_io.write(chunk)
end
body_io.rewind
env['REQUEST_METHOD'] = method
env['SERVER_NAME'] = uri.host || ''
env['SERVER_PORT'] = uri.port || '80'
env['SCRIPT_NAME'] = ''
env['PATH_INFO'] = uri.path
env['QUERY_STRING'] = uri.query || ''
env['rack.version'] = Rack::VERSION
env['rack.input'] = body_io
env['rack.errors'] = @error_stream
env['rack.multithread'] = true env['rack.multiprocess'] = true env['rack.run_once'] = false
env['rack.url_scheme'] = uri.scheme || 'http'
.each do |, value|
case .downcase
when 'content-length'
env['CONTENT_LENGTH'] = value
when 'content-type'
env['CONTENT_TYPE'] = value
end
env['HTTP_' + .gsub(/\-/, "_").upcase] = value
end
request = Rack::Request.new(env)
return request
end
|
#convert_request_to_a(request_obj) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/httpadapter/adapters/rack.rb', line 33
def convert_request_to_a(request_obj)
unless request_obj.kind_of?(Rack::Request)
raise TypeError, "Expected Rack::Request, got #{request_obj.class}."
end
method = request_obj.request_method.to_s.upcase
uri = Addressable::URI.parse(request_obj.url.to_s).normalize.to_s
= []
request_obj.env.each do |parameter, value|
next if parameter !~ /^HTTP_/
= (parameter.gsub(/^HTTP_/, '').split('_').map do |chunk|
chunk.capitalize
end).join('-')
<< [, value]
end
return [method, uri, , request_obj.body]
end
|
#convert_response_from_a(request_ary) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/httpadapter/adapters/rack.rb', line 103
def convert_response_from_a(request_ary)
status, , body = request_ary
status = status.to_i
body.each do |chunk|
unless chunk.kind_of?(String)
raise TypeError, "Expected String, got #{chunk.class}."
end
end
response = Rack::Response.new(body, status, Hash[])
return response
end
|
#convert_response_to_a(response_obj) ⇒ Object
96
97
98
99
100
101
|
# File 'lib/httpadapter/adapters/rack.rb', line 96
def convert_response_to_a(response_obj)
unless response_obj.kind_of?(Rack::Response)
raise TypeError, "Expected Rack::Response, got #{response_obj.class}."
end
return response_obj.finish
end
|
#fetch_resource(request_ary, connection = nil) ⇒ Object
116
117
118
119
|
# File 'lib/httpadapter/adapters/rack.rb', line 116
def fetch_resource(request_ary, connection=nil)
raise NotImplementedError,
'No HTTP client implementation available to transmit a Rack::Request.'
end
|