Module: Weary::Requestable
Instance Method Summary collapse
-
#adapter(connection = nil) ⇒ Object
An accessor to set a Weary::Adapter to use to forward the connection.
-
#has_middleware? ⇒ Boolean
Should the Request use one or more Rack::Middleware?.
-
#headers(hash = nil) ⇒ Object
An accessor to set HTTP request headers.
-
#pass_values_onto_requestable(requestable) ⇒ Object
Pass Requestable values on to another Requestable object (including Middleware).
-
#use(middleware, *args, &block) ⇒ Object
Send a Rack middleware to be used by the Request.
-
#user_agent(agent) ⇒ Object
Convenience method to set a User Agent Header.
Instance Method Details
#adapter(connection = nil) ⇒ Object
An accessor to set a Weary::Adapter to use to forward the connection. When a request is made, it will be passed along to this adapter to get the eventual Response. Defaults to Weary::Adapter::NetHttp.
connection - An optional Weary::Adapter.
Returns the Weary::Adapter.
11 12 13 14 |
# File 'lib/weary/requestable.rb', line 11 def adapter(connection=nil) @connection = connection unless connection.nil? @connection ||= Weary::Adapter::NetHttp end |
#has_middleware? ⇒ Boolean
Should the Request use one or more Rack::Middleware?
49 50 51 |
# File 'lib/weary/requestable.rb', line 49 def has_middleware? !@middlewares.nil? && !@middlewares.empty? end |
#headers(hash = nil) ⇒ Object
An accessor to set HTTP request headers.
hash - An optional Hash of key/value pairs that are sent as HTTP
request headers when a resource's request is performed.
Returns a Hash of the headers.
22 23 24 25 |
# File 'lib/weary/requestable.rb', line 22 def headers(hash=nil) @headers = hash unless hash.nil? @headers ||= {} end |
#pass_values_onto_requestable(requestable) ⇒ Object
Pass Requestable values on to another Requestable object (including Middleware).
requestable - Another Requestable object.
Returns the Requestable object.
59 60 61 62 63 64 65 66 |
# File 'lib/weary/requestable.rb', line 59 def pass_values_onto_requestable(requestable) requestable.headers self.headers unless @headers.nil? requestable.adapter self.adapter unless @connection.nil? if has_middleware? @middlewares.each {|middleware| requestable.use *middleware } end requestable end |
#use(middleware, *args, &block) ⇒ Object
Send a Rack middleware to be used by the Request.
middleware - An object that implements the rack middleware interface. args - Zero or more optional arguments to send to the middleware. block - An optional block to send to the middleware.
Returns an Array of middlewares.
34 35 36 37 |
# File 'lib/weary/requestable.rb', line 34 def use(middleware, *args, &block) @middlewares ||= [] @middlewares << [middleware, args.compact, block] end |
#user_agent(agent) ⇒ Object
Convenience method to set a User Agent Header
agent - A user agent String. See Weary::USER_AGENTS for some help
Returns the updated headers Hash.
44 45 46 |
# File 'lib/weary/requestable.rb', line 44 def user_agent(agent) headers.update 'User-Agent' => agent end |