Class: HttpClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, max_batch_size = 25, buffer_delay = 10, http_headers = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



154
155
156
157
158
159
160
161
162
# File 'lib/blest.rb', line 154

def initialize(url, max_batch_size = 25, buffer_delay = 10, http_headers = {})
  @url = url
  @max_batch_size = max_batch_size
  @buffer_delay = buffer_delay
  @http_headers = http_headers
  @queue = Queue.new
  @futures = {}
  @lock = Mutex.new
end

Instance Attribute Details

#buffer_delayObject

Returns the value of attribute buffer_delay.



152
153
154
# File 'lib/blest.rb', line 152

def buffer_delay
  @buffer_delay
end

#futuresObject (readonly)

Returns the value of attribute futures.



151
152
153
# File 'lib/blest.rb', line 151

def futures
  @futures
end

#headersObject

Returns the value of attribute headers.



152
153
154
# File 'lib/blest.rb', line 152

def headers
  @headers
end

#max_batch_sizeObject

Returns the value of attribute max_batch_size.



152
153
154
# File 'lib/blest.rb', line 152

def max_batch_size
  @max_batch_size
end

#queueObject (readonly)

Returns the value of attribute queue.



151
152
153
# File 'lib/blest.rb', line 151

def queue
  @queue
end

#urlObject

Returns the value of attribute url.



152
153
154
# File 'lib/blest.rb', line 152

def url
  @url
end

Instance Method Details

#request(route, body = nil, headers = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/blest.rb', line 164

def request(route, body=nil, headers=nil)
  uuid = SecureRandom.uuid()
  future = Concurrent::Promises.resolvable_future
  @lock.synchronize do
    @futures[uuid] = future
  end

  @queue.push({ uuid: uuid, data: [uuid, route, body, headers] })
  process_timeout()
  future
end