Class: Dialed::HTTP::Client

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

Direct Known Subclasses

ExplicitClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_builder = ConnectionBuilder.apply_defaults) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
# File 'lib/dialed/http/client.rb', line 28

def initialize(connection_builder = ConnectionBuilder.apply_defaults)
  @connection_builder = connection_builder
  @async_task_count = 0
  @closed = false
end

Instance Attribute Details

#connection_builderObject (readonly)

Returns the value of attribute connection_builder.



67
68
69
# File 'lib/dialed/http/client.rb', line 67

def connection_builder
  @connection_builder
end

#waiterObject

Returns the value of attribute waiter.



26
27
28
# File 'lib/dialed/http/client.rb', line 26

def waiter
  @waiter
end

Class Method Details

.build(&block) ⇒ Object



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

def self.build(&block)
  ExplicitClient.new create_connection_builder(&block)
end

.create_connection_builder(&block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/dialed/http/client.rb', line 10

def self.create_connection_builder(&block)
  if block_given?
    connection_builder = ConnectionBuilder.new
    block.call(connection_builder)
    return connection_builder
  end
  ConnectionBuilder.apply_defaults
end

Instance Method Details

#async(&block) ⇒ Object



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

def async(&block)
  Async do |task|
    waiter = Async::Waiter.new(parent: task)
    waiting_client = dup
    waiting_client.waiter = waiter
    arr = []
    implicit = block.call(waiting_client, arr)
    if arr.empty?
      waiter.wait(waiter.instance_variable_get(:@done).count)
      implicit
    else
      enum = Enumerator::Lazy.new(arr) do |yielder, *values|
        if values.size == 1
          value = values.first
          value.wait
          yielder << value.result
        else
          values.each(&:wait)
          yielder << values.map(&:result)
        end
      end
      enum
    end
  end
end

#closeObject



19
20
21
22
23
24
# File 'lib/dialed/http/client.rb', line 19

def close
  return if @closed

  @closed = true
  with_dialer(&:hangup!)
end

#get(location, query: {}, **kwargs) ⇒ Object



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

def get(location, query: {}, **kwargs)
  with_dialer do |dialer|
    response = dialer.call('GET', location, **kwargs)
    response
  end
end

#with_dialer(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dialed/http/client.rb', line 69

def with_dialer(&block)
  if waiter
    waiter.async do
      fetch_dialer(&block)
    end
  elsif Async::Task.current?
    fetch_dialer do |dialer|
      block.call(dialer)
    end
  else
    Sync do
      fetch_dialer do |dialer|
        dialer.start_session do |session|
          block.call(session)
        end
      end
    end
  end
end