Class: NurseAndrea::HttpClient
- Inherits:
-
Object
- Object
- NurseAndrea::HttpClient
- Defined in:
- lib/nurse_andrea/http_client.rb
Instance Method Summary collapse
-
#initialize ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(url, body) ⇒ Object
Constructor Details
#initialize ⇒ HttpClient
Returns a new instance of HttpClient.
7 8 9 10 |
# File 'lib/nurse_andrea/http_client.rb', line 7 def initialize @api_key = NurseAndrea.config.api_key @timeout = NurseAndrea.config.timeout end |
Instance Method Details
#post(url, body) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nurse_andrea/http_client.rb', line 12 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 = @timeout http.read_timeout = @timeout request = Net::HTTP::Post.new(uri.path) request["Content-Type"] = "application/json" request["Authorization"] = "Bearer #{@api_key}" request["User-Agent"] = "nurse_andrea-ruby/#{NurseAndrea::VERSION}" request.body = body.to_json response = http.request(request) success = response.code.to_i.between?(200, 299) if NurseAndrea.config.debug && !success warn "[NurseAndrea] HTTP #{response.code} from #{uri}: #{response.body.to_s[0..200]}" end success rescue => e warn "[NurseAndrea] HTTP error posting to #{url}: #{e.class}: #{e.}" if NurseAndrea.config.debug false end |