Class: Dialed::HTTP::Dialer

Inherits:
Object
  • Object
show all
Defined in:
lib/dialed/http/dialer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder = ConnectionBuilder.apply_defaults, lazy: true, &block) ⇒ Dialer

Returns a new instance of Dialer.



8
9
10
11
12
13
# File 'lib/dialed/http/dialer.rb', line 8

def initialize(builder = ConnectionBuilder.apply_defaults, lazy: true, &block)
  @builder = builder
  @connection = NilConnection.new
  @lazy = lazy
  start_session(&block) if block_given?
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/dialed/http/dialer.rb', line 6

def connection
  @connection
end

Instance Method Details

#call(verb, location, *args, proxy_uri: nil, **kwargs) ⇒ Object



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
# File 'lib/dialed/http/dialer.rb', line 40

def call(verb, location, *args, proxy_uri: nil, **kwargs)
  location_uri = Addressable::URI.parse(location)
  request = Request.new(verb, location_uri.path, *args, **kwargs)
  response = (
    if connection.open?
      response = request.call(connection)
      Response.new(response)
    elsif lazy?
      @builder.uri = location_uri
      @builder.proxy_uri = proxy_uri if proxy_uri
      success = attempt_connection!
      raise Dialed::Error, "Failed to connect to #{location}. connection status: #{connection.open?}" unless success

      Response.new(request.call(connection))
    else
      success = attempt_connection!
      raise Dialed::Error, "Failed to connect to #{location}. connection status: #{connection.open?}" unless success

      Response.new(request.call(connection))
    end

  )

  return response unless block_given?

  begin
    yield response
  ensure
    response.close
  end
end

#connected?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dialed/http/dialer.rb', line 15

def connected?
  connection.open?
end

#current_hostObject



27
28
29
30
31
# File 'lib/dialed/http/dialer.rb', line 27

def current_host
  return nil unless on_a_call?

  connection.remote_host
end

#disconnected?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dialed/http/dialer.rb', line 23

def disconnected?
  connection.closed?
end

#hangup!Object



72
73
74
75
# File 'lib/dialed/http/dialer.rb', line 72

def hangup!
  connection.close if connection.open?
  @connection = NilConnection.new
end

#lazy?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/dialed/http/dialer.rb', line 19

def lazy?
  @lazy
end

#ready?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/dialed/http/dialer.rb', line 77

def ready?
  raise 'Expected connection not to be actually nil' if connection.nil?
  return false if connection.nil_connection?
  return false if connection.open?
  return false unless @builder.valid?

  true
end

#start_session(&block) ⇒ Object



33
34
35
36
37
38
# File 'lib/dialed/http/dialer.rb', line 33

def start_session(&block)
  attempt_connection!
  block.call(self)
ensure
  hangup!
end