Class: FaradayMiddleware::RackCompatible

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday_middleware/rack_compatible.rb

Overview

Wraps a handler originally written for Rack to make it compatible with Faraday.

Experimental. Only handles changes in request headers.

Constant Summary collapse

NonPrefixedHeaders =
%w[CONTENT_LENGTH CONTENT_TYPE]

Instance Method Summary collapse

Constructor Details

#initialize(app, rack_handler, *args) ⇒ RackCompatible

Returns a new instance of RackCompatible.



8
9
10
11
12
13
14
15
16
17
# File 'lib/faraday_middleware/rack_compatible.rb', line 8

def initialize(app, rack_handler, *args)
  # tiny middleware that decomposes a Faraday::Response to standard Rack
  # array: [status, headers, body]
  compatible_app = lambda do |env|
    restore_env(env)
    response = app.call(env)
    [response.status, response.headers, Array(response.body)]
  end
  @rack = rack_handler.new(compatible_app, *args)
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
# File 'lib/faraday_middleware/rack_compatible.rb', line 19

def call(env)
  prepare_env(env)
  rack_response = @rack.call(env)
  finalize_response(env, rack_response)
end

#finalize_response(env, rack_response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/faraday_middleware/rack_compatible.rb', line 68

def finalize_response(env, rack_response)
  status, headers, body = rack_response
  body = body.inject() { |str, part| str << part }
  headers = Faraday::Utils::Headers.new(headers) unless Faraday::Utils::Headers === headers

  env.update :status => status.to_i,
             :body => body,
             :response_headers => headers

  env[:response] ||= Faraday::Response.new(env)
  env[:response]
end

#headers_to_rack(env) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/faraday_middleware/rack_compatible.rb', line 43

def headers_to_rack(env)
  env[:request_headers].each do |name, value|
    name = name.upcase.tr('-', '_')
    name = "HTTP_#{name}" unless NonPrefixedHeaders.include? name
    env[name] = value
  end
end

#prepare_env(env) ⇒ Object

faraday to rack-compatible



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/faraday_middleware/rack_compatible.rb', line 28

def prepare_env(env)
  headers_to_rack(env)

  url = env[:url]
  env['rack.url_scheme'] = url.scheme
  env['PATH_INFO'] = url.path
  env['SERVER_PORT'] = url.respond_to?(:inferred_port) ? url.inferred_port : url.port
  env['QUERY_STRING'] = url.query
  env['REQUEST_METHOD'] = env[:method].to_s.upcase

  env['rack.errors'] ||= StringIO.new

  env
end

#restore_env(env) ⇒ Object

rack to faraday-compatible



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faraday_middleware/rack_compatible.rb', line 52

def restore_env(env)
  headers = env[:request_headers]
  headers.clear

  env.each do |name, value|
    next unless String === name
    if NonPrefixedHeaders.include? name or name.index('HTTP_') == 0
      name = name.sub(/^HTTP_/, '').downcase.tr('_', '-')
      headers[name] = value
    end
  end

  env[:method] = env['REQUEST_METHOD'].downcase.to_sym
  env
end