Module: Weary::Requestable

Included in:
Client, Client, Request, Resource
Defined in:
lib/weary/requestable.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) adapter(connection = nil)

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

- (Boolean) has_middleware?

Should the Request use one or more Rack::Middleware?

Returns:

  • (Boolean)


49
50
51
# File 'lib/weary/requestable.rb', line 49

def has_middleware?
  !@middlewares.nil? && !@middlewares.empty?
end

- (Object) headers(hash = nil)

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

- (Object) pass_values_onto_requestable(requestable)

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

- (Object) use(middleware, *args, &block)

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

- (Object) user_agent(agent)

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