Class: Nadir::Transport::HTTPAsync

Inherits:
Object
  • Object
show all
Defined in:
lib/nadir/transport/http_async.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTTPAsync

Returns a new instance of HTTPAsync.



9
10
11
12
# File 'lib/nadir/transport/http_async.rb', line 9

def initialize
  @api_key = Nadir.config.api_key
  @api_url = Nadir.config.api_url
end

Class Method Details

.shut_downObject



19
20
21
22
# File 'lib/nadir/transport/http_async.rb', line 19

def shut_down
  thread_pool.shutdown
  thread_pool.wait_for_termination
end

.thread_poolObject



15
16
17
# File 'lib/nadir/transport/http_async.rb', line 15

def thread_pool
  @thread_pool ||= Concurrent::ThreadPoolExecutor.new(max_threads: 5)
end

Instance Method Details

#deliver(params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nadir/transport/http_async.rb', line 25

def deliver(params)
  self.class.thread_pool.post do
    uri = URI("#{@api_url}/faults")
    post = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
    post.body = {fault: params, api_key: @api_key}.to_json

    request = Net::HTTP.new(uri.hostname, uri.port)
    request.use_ssl = true
    response = request.start do |http|
      http.request post
    end
  end
end