Class: HTTPI::Adapter::Rack
Overview
Class Attribute Summary collapse
-
.mounted_apps ⇒ Object
Returns the value of attribute mounted_apps.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.mount(host, application) ⇒ Object
Attaches Rack endpoint at specified host.
-
.unmount(host) ⇒ Object
Removes Rack endpoint.
Instance Method Summary collapse
-
#initialize(request) ⇒ Rack
constructor
A new instance of Rack.
-
#request(method) ⇒ Object
Executes arbitrary HTTP requests.
Methods inherited from Base
Constructor Details
#initialize(request) ⇒ Rack
Returns a new instance of Rack.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/httpi/adapter/rack.rb', line 40 def initialize(request) @app = self.class.mounted_apps[request.url.host] if @app.nil? = "Application '#{request.url.host}' not mounted: "; += "use `HTTPI::Adapter::Rack.mount('#{request.url.host}', RackApplicationClass)`" raise end @request = request @client = ::Rack::MockRequest.new(@app) end |
Class Attribute Details
.mounted_apps ⇒ Object
Returns the value of attribute mounted_apps.
24 25 26 |
# File 'lib/httpi/adapter/rack.rb', line 24 def mounted_apps @mounted_apps end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/httpi/adapter/rack.rb', line 21 def client @client end |
Class Method Details
.mount(host, application) ⇒ Object
Attaches Rack endpoint at specified host. Endpoint will be acessible at / url.
31 32 33 |
# File 'lib/httpi/adapter/rack.rb', line 31 def self.mount(host, application) self.mounted_apps[host] = application end |
.unmount(host) ⇒ Object
Removes Rack endpoint.
36 37 38 |
# File 'lib/httpi/adapter/rack.rb', line 36 def self.unmount(host) self.mounted_apps.delete(host) end |
Instance Method Details
#request(method) ⇒ Object
Executes arbitrary HTTP requests. You have to mount required Rack application before you can use it.
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 95 96 |
# File 'lib/httpi/adapter/rack.rb', line 60 def request(method) unless REQUEST_METHODS.include? method raise NotSupportedError, "Rack adapter does not support custom HTTP methods" end if @request.proxy raise NotSupportedError, "Rack adapter does not support proxying" end if @request.auth.http? if @request.auth.basic? basic_auth = @request.auth.basic.join(':') encoded = Base64.encode64(basic_auth).gsub('\n', '') @request.headers['Authorization'] = "Basic #{encoded}" else raise NotSupportedError, "Rack adapter does not support HTTP #{@request.auth.type} auth" end end if @request.auth.ssl? raise NotSupportedError, "Rack adapter does not support SSL client auth" end if @request.on_body raise NotSupportedError, "Rack adapter does not support response streaming" end env = {} @request.headers.each do |header, value| env["HTTP_#{header.gsub('-', '_').upcase}"] = value end response = @client.request(method.to_s.upcase, @request.url.to_s, { :fatal => true, :input => @request.body.to_s }.merge(env)) Response.new(response.status, response.headers, response.body) end |