lib/rufus/verbs/endpoint.rb in rufus-verbs-0.2 vs lib/rufus/verbs/endpoint.rb in rufus-verbs-0.3

- old
+ new

@@ -35,15 +35,18 @@ require 'uri' require 'yaml' # for StringIO (at least for now) require 'net/http' require 'zlib' +require 'rufus/verbs/version' +require 'rufus/verbs/cookies' +require 'rufus/verbs/digest' + module Rufus module Verbs - VERSION = "0.2" USER_AGENT = "Ruby rufus-verbs #{VERSION}" # # An EndPoint can be used to share common options among a set of # requests. @@ -62,10 +65,13 @@ # When a request gets prepared, the option values will be looked up # in (1) its local (request) options, then (2) in the EndPoint options. # class EndPoint + include CookieMixin + include DigestAuthMixin + # # The endpoint initialization opts (Hash instance) # attr_reader :opts @@ -79,10 +85,12 @@ opts[:http_basic_authentication] || opts[:hba] @opts[:user_agent] ||= USER_AGENT @opts[:proxy] ||= ENV['HTTP_PROXY'] + + prepare_cookie_jar end def get (*args) request :get, args @@ -125,10 +133,12 @@ # The instance methods get, post, put and delete ultimately calls # this request() method. All the work is done here. # def request (method, args, &block) + # prepare request + opts = EndPoint.extract_opts args compute_target opts req = create_request method, opts @@ -137,22 +147,38 @@ add_authentication(req, opts) add_conditional_headers(req, opts) if method == :get + mention_cookies(req, opts) + # if the :cookies option is disabled (the default) + # will have no effect + return req if o(opts, :dry_run) == true + # trigger request + http = prepare_http opts res = nil http.start do res = http.request req end + # handle response + + register_cookies res, opts + # if the :cookies option is disabled (the default) + # will have no effect + return res if o(opts, :raw_response) + check_authentication_info res, opts + # used in case of :digest_authentication + # will have no effect else + res = handle_response method, res, opts return res.body if o(opts, :body) res @@ -185,12 +211,12 @@ # [endpoint] @opts. # def o (opts, key) keys = Array key - keys.each { |k| (v = opts[k] and return v) } - keys.each { |k| (v = @opts[k] and return v) } + keys.each { |k| (v = opts[k]; return v if v != nil) } + keys.each { |k| (v = @opts[k]; return v if v != nil) } nil end # # Returns scheme, host, port, path, query @@ -282,18 +308,24 @@ # # This comment is too much... Just read the code... # def add_authentication (req, opts) - a = opts[:http_basic_authentication] + b = o(opts, :http_basic_authentication) + d = o(opts, :digest_authentication) + o = o(opts, :auth) - if a + if b and b != false - req.basic_auth a[0], a[1] + req.basic_auth b[0], b[1] - elsif opts[:auth] + elsif d and d != false - opts[:auth].call req + digest_auth req, opts + + elsif o and o != false + + o.call req end end # # In that base class, it's empty.