Sha256: 79fd3959d7422e8eac5681d0c1d9f5cc9216ae17ee11a4a65c8b779a874502cb
Contents?: true
Size: 1.41 KB
Versions: 43
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true module Nonnative class HTTPClient def initialize(host) @host = host end protected def get(pathname, headers = {}, timeout = 60) with_exception do uri = URI.join(host, pathname) RestClient::Request.execute(method: :get, url: uri.to_s, headers: headers, timeout: timeout) end end def post(pathname, payload, headers = {}, timeout = 60) with_exception do uri = URI.join(host, pathname) RestClient::Request.execute(method: :post, url: uri.to_s, payload: payload.to_json, headers: headers, timeout: timeout) end end def delete(pathname, headers = {}, timeout = 60) with_exception do uri = URI.join(host, pathname) RestClient::Request.execute(method: :delete, url: uri.to_s, headers: headers, timeout: timeout) end end def put(pathname, payload, headers = {}, timeout = 60) with_exception do uri = URI.join(host, pathname) RestClient::Request.execute(method: :put, url: uri.to_s, payload: payload.to_json, headers: headers, timeout: timeout) end end private attr_reader :host def with_exception yield rescue RestClient::Exceptions::ReadTimeout => e raise e rescue RestClient::ExceptionWithResponse => e e.response end end end
Version data entries
43 entries across 43 versions & 1 rubygems