DEAD 项目,请勿使用
Hurley is a ruby gem with no runtime dependencies that provides a common interface for working with different HTTP adapters. It is an evolution of Faraday, with rethought internals.
Hurley revolves around three main classes: Client, Request, and Response. A Client sets the default properties for all HTTP requests, including the base url, headers, and options.
…By default, a Hurley::Client uses a Hurley::Connection instance to make
requests with net/http. You can swap the connection with any object that
responds to #call with a Request, and returns a Response. This will not
interrupt other client properties or callbacks.
client = Hurley::Client.new "https://api.github.com"
client.connection = lambda do |req|
# return a Hurley::Response!
endHurley joins a Client endpoint with a given request URL to produce the final URL that is requested.
…Hurley uses Hurley::Query::Nested for all query encoding and decoding by
default. This can be changed globally, per client, or per request. Typically
you won't create Hurley::Query instances manually, and will use
Hurley::Query.parse for parsing.
…A Client's Header is passed down to each request. Header keys can be overridden
by the request. Headers are stored internally in canonical form, which is
capitalized with dashes: "Content-Type", for example.
See Hurley::Header for all of the common header keys that have symbolized
shortcuts.
client = Hurley::Client.new "https://api.com"
client.header[:content_type] = "application/json"
# Same as:
client.header["content-type"] = "application/json"
client.header["Content-Type"] = "application/json"
client.get "/something.atom" do |req|
# Default user agent
req.header[:user_agent] # => "Hurley v#{Hurley::VERSION}"
# Change a header
req.header[:content_type] = "application/atom"
endHurley will encode form bodies for you, while setting default Content-Type and
Content-Length values as necessary. Multipart forms are supported too, using
Hurley::UploadIO objects.
# Works with HTTP verbs: post, put, and patch
# Send a=1 with Content-Type: application/x-www-form-urlencoded
client.post("/form", :a => 1)
# Send a=1 with Content-Type: text/plain
client.post("/form", {:a => 1}, "text/plain")
# Send file with Content-Type: multipart/form-data
client.post("/multipart", :file => Hurley::UploadIO.new("filename.txt", "text/plain"))The default query parser (Hurley::Query::Nested) is used by default. You can
change it globally, per client, or per request. See the "URLs" section.
Clients can define "before callbacks" that yield a Request, or "after callbacks" that yield a Response. Multiple callbacks of the same type are added in order.
client.before_call do |req|
# modify request before it's called
end
client.before_callbacks # => ["#"]
client.after_call do |res|
# modify response after it's called
end
# You can set a name to identify the callback
client.before_call :upcase do |req|
req.body.upcase!
end
client.before_callbacks # => [:upcase]
# You can also pass an object that responds to #call and #name.
class Upcaser
def name
:upcaser
end
def call(req)
req.body.upcase!
end
end
client.before_call(Upcaser.new)
client.before_callbacks # => [:upcaser]A Request object can take a callback that receives the response body in chunks as they are read from the socket. Hurley connections that don't support streaming will yield the entire response body once.
client.get "big-file" do |req|
req.on_body do |res, chunk|
puts "#{res.status_code}: #{chunk}"
end
# This streams the body for 200 or 201 responses only:
req.on_body(200, 201) do |res, chunk|
puts "#{res.status_code}: #{chunk}"
end
endHurley includes a Test connection object for testing. This lets you make requests without hitting a real endpoint.
require "hurley"
require "hurley/test"
client = Hurley::Client.new "https://api.github.com"
client.connection = Hurley::Test.new do |test|
# Verbs head, get, put, post, patch, delete, and options are supported.
test.get "/user" do |req|
# req is a Hurley::Request
# Return a Rack-compatible response.
[200, {"Content-Type" => "application/json"}, %({"id": 1})]
end
end
client.get("/user").body # => {"id": 1}暂无开放 Issues,或尚未同步最近议题。