class Almodovar::HttpClient
Attributes
auth_type[RW]
client[RW]
headers[RW]
password[RW]
username[RW]
Public Class Methods
new()
click to toggle source
# File lib/almodovar/http_client.rb, line 19 def initialize @client = HTTPClient.new end
Public Instance Methods
delete(uri, query = {}, headers = {})
click to toggle source
# File lib/almodovar/http_client.rb, line 35 def delete(uri, query = {}, headers = {}) request(:delete, uri, query: query, headers: merge_headers(headers)) end
get(uri, query = {}, headers = {})
click to toggle source
# File lib/almodovar/http_client.rb, line 23 def get(uri, query = {}, headers = {}) request(:get, uri, query: query, headers: merge_headers(headers)) end
post(uri, data, query = {}, headers = {})
click to toggle source
# File lib/almodovar/http_client.rb, line 27 def post(uri, data, query = {}, headers = {}) request(:post, uri, body: data, query: query, headers: merge_headers(headers)) end
put(uri, data, query = {}, headers = {})
click to toggle source
# File lib/almodovar/http_client.rb, line 31 def put(uri, data, query = {}, headers = {}) request(:put, uri, body: data, query: query, headers: merge_headers(headers)) end
Private Instance Methods
default_headers()
click to toggle source
# File lib/almodovar/http_client.rb, line 47 def default_headers defaults = Almodovar::default_options[:headers] || {} defaults = defaults.is_a?(Proc) ? defaults.call() : defaults end
domain_for(uri)
click to toggle source
# File lib/almodovar/http_client.rb, line 56 def domain_for(uri) scheme_chunk = uri.scheme ? "#{uri.scheme}://" : '' port_chunk = uri.port ? ":#{uri.port}" : '' host_chunk = uri.host.downcase "#{scheme_chunk}#{host_chunk}#{port_chunk}" end
merge_headers(req_headers)
click to toggle source
# File lib/almodovar/http_client.rb, line 41 def merge_headers(req_headers) (default_headers || {}). merge(self.headers ||= {}). merge(req_headers) end
previous_context()
click to toggle source
# File lib/almodovar/http_client.rb, line 63 def previous_context @previous_auth_context end
previous_context=(context)
click to toggle source
# File lib/almodovar/http_client.rb, line 67 def previous_context=(context) @previous_auth_context = context end
request(method, uri, options = {})
click to toggle source
# File lib/almodovar/http_client.rb, line 78 def request(method, uri, options = {}) uri = Addressable::URI.parse(uri) if (requires_auth?) domain = domain_for(uri) set_client_auth(domain) end request_options = { body: options[:body], header: options[:headers].stringify_keys || {}, follow_redirect: true } request_options[:query] = options[:query] if options[:query].present? client.request(method, uri, request_options) rescue HTTPClient::SendTimeoutError => e raise SendTimeoutError.new(e) rescue HTTPClient::ReceiveTimeoutError => e raise ReceiveTimeoutError.new(e) rescue HTTPClient::ConnectTimeoutError => e raise ConnectTimeoutError.new(e) rescue HTTPClient::TimeoutError => e raise TimeoutError.new(e) end
requires_auth?()
click to toggle source
# File lib/almodovar/http_client.rb, line 52 def requires_auth? username && password end
set_client_auth(domain)
click to toggle source
# File lib/almodovar/http_client.rb, line 71 def set_client_auth(domain) context = AuthContext.new(username, password, domain) if (context.differs_from?(previous_context)) client.set_auth(domain, username, password) end end