added chainable module, a la http gem

This commit is contained in:
HoneyryderChuck 2017-12-07 21:15:39 +00:00
parent a74f225a3b
commit 2742f4f9af
3 changed files with 73 additions and 0 deletions

View File

@ -12,6 +12,7 @@ require "httpx/connection"
require "httpx/headers"
require "httpx/request"
require "httpx/response"
require "httpx/chainable"
require "httpx/client"
module HTTPX
@ -40,4 +41,5 @@ module HTTPX
end
end
extend Chainable
end

69
lib/httpx/chainable.rb Normal file
View File

@ -0,0 +1,69 @@
# frozen_string_literal: true
module HTTPX
module Chainable
def head(uri, **options)
request(:head, uri, **options)
end
def get(uri, **options)
request(:get, uri, **options)
end
def post(uri, **options)
request(:post, uri, **options)
end
def put(uri, **options)
request(:put, uri, **options)
end
def delete(uri, **options)
request(:delete, uri, **options)
end
def trace(uri, **options)
request(:trace, uri, **options)
end
def options(uri, **options)
request(:options, uri, **options)
end
def connect(uri, **options)
request(:connect, uri, **options)
end
def patch(uri, **options)
request(:patch, uri, **options)
end
def request(verb, uri, **options)
branch(**options).request(verb, uri)
end
def timeout(klass, **options)
branch(timeout: Timeout.by(klass, **options))
end
def headers(headers)
branch(default_options.with_headers(headers))
end
def encoding(encoding)
branch(default_options.with_encoding(encoding))
end
def accept(type)
headers("accept" => String(type))
end
private
# :nodoc:
def branch(options)
Client.new(options)
end
end
end

View File

@ -2,6 +2,8 @@
module HTTPX
class Client
include Chainable
def initialize(**options)
@default_options = self.class.default_options.merge(options)
@connection = Connection.new(@default_options)