Class: Faraday::Adapter::EMSynchrony

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/em_synchrony.rb

Defined Under Namespace

Classes: Header

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_TYPE, DEFAULT_BOUNDARY, FORM_TYPE, MULTIPART_TYPE

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#create_multipart, #full_path_for, #has_multipart?, #process_body_for_request, #process_to_params

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants, #lookup_module, #register_lookup_modules

Methods inherited from Middleware

#initialize, loaded?, setup_parallel_manager

Constructor Details

This class inherits a constructor from Faraday::Middleware

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
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
67
68
69
70
71
72
73
74
75
# File 'lib/faraday/adapter/em_synchrony.rb', line 22

def call(env)
  process_body_for_request(env)
  
  request = EventMachine::HttpRequest.new(URI::parse(env[:url].to_s))
  
  options = {:head => env[:request_headers]}

  if env[:body]
    if env[:body].respond_to? :read
      options[:body] = env[:body].read
    else
      options[:body] = env[:body]
    end
  end

  if req = env[:request]
    if proxy = req[:proxy]
      uri = Addressable::URI.parse(proxy[:uri])
      options[:proxy] = {
        :host => uri.host,
        :port => uri.port
      }
      if proxy[:username] && proxy[:password]
        options[:proxy][:authorization] = [proxy[:username], proxy[:password]]
      end
    end

    # only one timeout currently supported by em http request
    if req[:timeout] or req[:open_timeout]
      options[:timeout] = [req[:timeout] || 0, req[:open_timeout] || 0].max
    end
  end

  client = nil
  block = lambda { request.send env[:method].to_s.downcase.to_sym, options }
  if !EM.reactor_running?
    EM.run {
      Fiber.new do
        client = block.call
        EM.stop
      end.resume
    }
  else
    client = block.call
  end

  env.update(:status           => client.response_header.http_status.to_i,
             :response_headers => Header.new(client),
             :body             => client.response)

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, "connection refused"
end