Class: Restfully::HTTP::Request
- Inherits:
-
Object
- Object
- Restfully::HTTP::Request
- Includes:
- Helper
- Defined in:
- lib/restfully/http/request.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#retry_on_error ⇒ Object
Returns the value of attribute retry_on_error.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#wait_before_retry ⇒ Object
Returns the value of attribute wait_before_retry.
Instance Method Summary collapse
- #execute! ⇒ Object
- #forced_cache? ⇒ Boolean
-
#initialize(session, method, path, options = {}) ⇒ Request
constructor
A new instance of Request.
- #inspect ⇒ Object
- #no_cache! ⇒ Object
- #no_cache? ⇒ Boolean
- #remove_no_cache! ⇒ Object
- #retry! ⇒ Object
-
#update!(options = {}) ⇒ Object
Updates the request header and query parameters.
Methods included from Helper
#sanitize_head, #sanitize_query
Constructor Details
#initialize(session, method, path, options = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/restfully/http/request.rb', line 12 def initialize(session, method, path, = {}) @session = session @attempts = 0 request = .symbolize_keys @retry_on_error = request[:retry_on_error] || session.config[:retry_on_error] @wait_before_retry = request[:wait_before_retry] || session.config[:wait_before_retry] request[:method] = method request[:head] = sanitize_head(@session.default_headers).merge( build_head(request) ) origin_content_type = request[:head].delete('Origin-Content-Type') request[:head]['Content-Type'] ||= origin_content_type if origin_content_type request[:uri] = @session.uri_to(path) if request[:query] request[:uri].query_values = sanitize_query(request[:query]) end request[:body] = if [:post, :put].include?(request[:method]) build_body(request) end @method, @uri, @head, @body = request.values_at( :method, :uri, :head, :body ) end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def attempts @attempts end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def body @body end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def head @head end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def method @method end |
#retry_on_error ⇒ Object
Returns the value of attribute retry_on_error.
10 11 12 |
# File 'lib/restfully/http/request.rb', line 10 def retry_on_error @retry_on_error end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def session @session end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
9 10 11 |
# File 'lib/restfully/http/request.rb', line 9 def uri @uri end |
#wait_before_retry ⇒ Object
Returns the value of attribute wait_before_retry.
10 11 12 |
# File 'lib/restfully/http/request.rb', line 10 def wait_before_retry @wait_before_retry end |
Instance Method Details
#execute! ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/restfully/http/request.rb', line 78 def execute! session.logger.debug self.inspect resource = RestClient::Resource.new( uri.to_s, @session..merge({:headers => head}) ) begin reqcode, reqhead, reqbody = resource.send(method, body || {}) response = Response.new(session, reqcode, reqhead, reqbody) session.logger.debug response.inspect response rescue Errno::ECONNREFUSED => e retry! || raise(e) end end |
#forced_cache? ⇒ Boolean
74 75 76 |
# File 'lib/restfully/http/request.rb', line 74 def forced_cache? !!@forced_cache end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/restfully/http/request.rb', line 61 def inspect "#{method.to_s.upcase} #{uri.to_s}, head=#{head.inspect}, body=#{body.inspect}" end |
#no_cache! ⇒ Object
69 70 71 72 |
# File 'lib/restfully/http/request.rb', line 69 def no_cache! @forced_cache = true head['Cache-Control'] = 'no-cache' end |
#no_cache? ⇒ Boolean
65 66 67 |
# File 'lib/restfully/http/request.rb', line 65 def no_cache? head['Cache-Control'] && head['Cache-Control'].include?('no-cache') end |
#remove_no_cache! ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/restfully/http/request.rb', line 106 def remove_no_cache! @forced_cache = false if head['Cache-Control'] head['Cache-Control'] = head['Cache-Control'].split(/\s+,\s+/).reject{|v| v =~ /no-cache/i }.join(",") end end |
#retry! ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/restfully/http/request.rb', line 95 def retry! if @attempts < @retry_on_error @attempts+=1 session.logger.warn "Encountered connection or server error. Retrying in #{@wait_before_retry}s... [#{@attempts}/#{@retry_on_error}]" sleep @wait_before_retry if @wait_before_retry > 0 execute! else false end end |
#update!(options = {}) ⇒ Object
Updates the request header and query parameters. Returns nil if no changes were made, otherwise self.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/restfully/http/request.rb', line 46 def update!( = {}) objects_that_may_be_updated = [@uri, @head] old_hash = objects_that_may_be_updated.map(&:hash) opts = .symbolize_keys @head.merge!(build_head(opts)) if opts[:query] @uri.query_values = sanitize_query(opts[:query]) end if old_hash == objects_that_may_be_updated.map(&:hash) nil else self end end |