Class: NurseAndrea::HttpClient

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

Constant Summary collapse

REJECTION_WARNING_THRESHOLD =
5
REJECTION_STATUSES =
[ 401, 403, 422, 429 ].freeze
@@consecutive_rejections =
0
@@warned_for_error =
nil
@@mutex =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



23
24
25
# File 'lib/nurse_andrea/http_client.rb', line 23

def initialize
  @config = NurseAndrea.config
end

Class Method Details

.reset_rejection_state!Object



15
16
17
18
19
20
# File 'lib/nurse_andrea/http_client.rb', line 15

def reset_rejection_state!
  @@mutex.synchronize do
    @@consecutive_rejections = 0
    @@warned_for_error       = nil
  end
end

Instance Method Details

#post(url, body) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nurse_andrea/http_client.rb', line 27

def post(url, body)
  uri  = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl     = uri.scheme == "https"
  http.open_timeout = @config.timeout
  http.read_timeout = @config.timeout

  request = Net::HTTP::Post.new(uri.path)
  build_headers.each { |k, v| request[k] = v }
  request.body = body.to_json

  response = http.request(request)
  handle_response(response, uri)

  response.code.to_i.between?(200, 299)
rescue => e
  warn "[NurseAndrea] HTTP error posting to #{url}: #{e.class}: #{e.message}" if @config.debug
  false
end