Class Rufus::Verbs::EndPoint

  1. lib/rufus/verbs/endpoint.rb
Parent: Object

An EndPoint can be used to share common options among a set of requests.

ep = EndPoint.new(
  :host => "restful.server",
  :port => 7080,
  :resource => "inventory/tools")

res = ep.get :id => 1
  # still a silver bullet ?

res = ep.get :id => 0
  # where did the hammer go ?

When a request gets prepared, the option values will be looked up in (1) its local (request) options, then (2) in the EndPoint options.

Methods

public class

  1. new
  2. request

public instance

  1. delete
  2. get
  3. head
  4. options
  5. post
  6. put
  7. request

Attributes

opts [R] The endpoint initialization opts (Hash instance)

Public class methods

new (opts)
[show source]
    # File lib/rufus/verbs/endpoint.rb, line 71
71:     def initialize (opts)
72: 
73:       @opts = opts
74: 
75:       compute_target @opts
76: 
77:       @opts[:http_basic_authentication] =
78:         opts[:http_basic_authentication] || opts[:hba]
79: 
80:       @opts[:user_agent] ||= USER_AGENT
81: 
82:       @opts[:proxy] ||= ENV['HTTP_PROXY']
83: 
84:       prepare_cookie_jar
85:     end
request (method, args, &block)

This is the method called by the module methods verbs.

For example,

RufusVerbs.get(args)

calls

RufusVerbs::EndPoint.request(:get, args)
[show source]
     # File lib/rufus/verbs/endpoint.rb, line 128
128:     def self.request (method, args, &block)
129: 
130:       opts = extract_opts args
131: 
132:       EndPoint.new(opts).request(method, opts, &block)
133:     end

Public instance methods

delete (*args)
[show source]
     # File lib/rufus/verbs/endpoint.rb, line 102
102:     def delete (*args)
103: 
104:       request :delete, args
105:     end
get (*args)
[show source]
    # File lib/rufus/verbs/endpoint.rb, line 87
87:     def get (*args)
88: 
89:       request :get, args
90:     end
head (*args)
[show source]
     # File lib/rufus/verbs/endpoint.rb, line 107
107:     def head (*args)
108: 
109:       request :head, args
110:     end
options (*args)
[show source]
     # File lib/rufus/verbs/endpoint.rb, line 112
112:     def options (*args)
113: 
114:       request :options, args
115:     end
post (*args, &block)
[show source]
    # File lib/rufus/verbs/endpoint.rb, line 92
92:     def post (*args, &block)
93: 
94:       request :post, args, &block
95:     end
put (*args, &block)
[show source]
     # File lib/rufus/verbs/endpoint.rb, line 97
 97:     def put (*args, &block)
 98: 
 99:       request :put, args, &block
100:     end
request (method, args, &block)

The instance methods get, post, put and delete ultimately calls this request() method. All the work is done here.

[show source]
     # File lib/rufus/verbs/endpoint.rb, line 139
139:     def request (method, args, &block)
140: 
141:       # prepare request
142: 
143:       opts = EndPoint.extract_opts args
144: 
145:       compute_target opts
146: 
147:       req = create_request method, opts
148: 
149:       add_payload(req, opts, &block) if method == :post or method == :put
150: 
151:       add_authentication(req, opts)
152: 
153:       add_conditional_headers(req, opts) if method == :get
154: 
155:       mention_cookies(req, opts)
156:         # if the :cookies option is disabled (the default)
157:         # will have no effect
158: 
159:       vlog_request opts, req
160: 
161:       return req if o(opts, :dry_run) == true
162: 
163:       # trigger request
164: 
165:       http = prepare_http opts
166: 
167:       vlog_http opts, http
168: 
169:       res = nil
170: 
171:       http.start do
172:         res = http.request req
173:       end
174: 
175:       # handle response
176: 
177:       class << res
178:         attr_accessor :request
179:       end
180:       res.request = req
181: 
182:       vlog_response opts, res
183: 
184:       register_cookies res, opts
185:         # if the :cookies option is disabled (the default)
186:         # will have no effect
187: 
188:       return res if o(opts, :raw_response)
189: 
190:       check_authentication_info res, opts
191:         # used in case of :digest_authentication
192:         # will have no effect else
193: 
194:       res = handle_response method, res, opts
195: 
196:       return parse_options(res) if method == :options
197: 
198:       return res.body if o(opts, :body)
199: 
200:       res
201:     end