module Hyperkit::Connection
Network layer for API clients.
Constants
- CONVENIENCE_HEADERS
Header keys that can be passed in options hash to {#get},{#head}
Public Instance Methods
Hypermedia agent for the GitHub API
@return [Sawyer::Agent]
# File lib/hyperkit/connection.rb, line 89 def agent @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http| http.headers[:accept] = default_media_type http.headers[:content_type] = "application/json" http.headers[:user_agent] = user_agent end end
Make a HTTP DELETE request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 73 def delete(url, options = {}) request :delete, url, options end
Make a HTTP GET request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 37 def get(url, options = {}) request :get, url, parse_query_and_convenience_headers(options) end
Make a HTTP HEAD request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 82 def head(url, options = {}) request :head, url, parse_query_and_convenience_headers(options) end
Response
for last HTTP request
@return [Sawyer::Response]
# File lib/hyperkit/connection.rb, line 107 def last_response @last_response if defined? @last_response end
Make a HTTP PATCH request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 64 def patch(url, options = {}) request :patch, url, options end
Make a HTTP POST request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 46 def post(url, options = {}) request :post, url, options end
Make a HTTP PUT request
@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 55 def put(url, options = {}) request :put, url, options end
Fetch the root resource for the API
@return [Sawyer::Resource]
# File lib/hyperkit/connection.rb, line 100 def root get "/" end
Protected Instance Methods
# File lib/hyperkit/connection.rb, line 113 def endpoint api_endpoint end
Private Instance Methods
Executes the request, checking if it was successful
@return [Boolean] True on success, false otherwise
# File lib/hyperkit/connection.rb, line 146 def boolean_from_response(method, path, options = {}) request(method, path, options) @last_response.status == 204 rescue Hyperkit::NotFound false end
# File lib/hyperkit/connection.rb, line 179 def parse_query_and_convenience_headers(options) headers = options.delete(:headers) { Hash.new } CONVENIENCE_HEADERS.each do |h| if header = options.delete(h) headers[h] = header end end query = options.delete(:query) opts = {:query => options} opts[:query].merge!(query) if query && query.is_a?(Hash) opts[:headers] = headers unless headers.empty? opts end
# File lib/hyperkit/connection.rb, line 123 def request(method, path, data, options = {}) if data.is_a?(Hash) options[:query] = data.delete(:query) || {} options[:headers] = data.delete(:headers) || {} url_encode = data.delete(:url_encode) || true if accept = data.delete(:accept) options[:headers][:accept] = accept end if data[:raw_body] data = data[:raw_body] end end path = URI::Parser.new.escape(path.to_s) if url_encode @last_response = response = agent.call(method, path, data, options) response.data end
# File lib/hyperkit/connection.rb, line 119 def reset_agent @agent = nil end
# File lib/hyperkit/connection.rb, line 154 def sawyer_options opts = { :links_parser => Sawyer::LinkParsers::Simple.new, } conn_opts = {} conn_opts[:builder] = @middleware if @middleware conn_opts[:proxy] = @proxy if @proxy conn_opts[:ssl] = { verify: verify_ssl } if client_cert && File.exist?(client_cert) conn_opts[:ssl][:client_cert] = OpenSSL::X509::Certificate.new(File.read(client_cert)) end if client_key && File.exist?(client_key) conn_opts[:ssl][:client_key] = OpenSSL::PKey.read(File.read(client_key)) end opts[:faraday] = Faraday.new(conn_opts) opts end