Sha256: 12b860a511dbd10fed667cdf93f7986330dcd35f3b06a06ede57412599bbb6cc

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Restfulie
  module Client
    module HTTP #:nodoc:
      # ==== RequestFollow follow new location of a document usually with response codes 201,301,302,303 and 307. You can also configure other codes.
      # 
      # ==== Example:
      # @executor = ::Restfulie::Client::HTTP::RequestFollowExecutor.new("http://restfulie.com") #this class includes RequestFollow module.
      # @executor.at('/custom/songs').accepts('application/atom+xml').follow(201).post!("custom").code
      module RequestFollow
        include RequestBuilder
  
        def follow(code = nil)
          @follow ||= true # turn on follow redirection
          follow_codes << code unless code.nil? or follow_codes.include?(code)
          self
        end
  
        def request!(method, path, *args)#:nodoc:
          begin
            response = super
          rescue Error::Redirection => e
            raise e unless @follow # normal behavior for bang methods is to raise the exception
            response = e.response
            if follow_codes.include?(response.code)
              location = response.headers['location'] || response.headers['Location']
              raise Error::AutoFollowWithoutLocationError.new(self, response) unless location
              self.host = location
              response = super(:get, location, headers)
            end
            response
          end
        end
  
        protected
  
          def follow_codes
            @follow_codes ||= [201,301,302,303,307]        
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
restfulie-0.9.3 lib/restfulie/client/http/request_follow.rb
restfulie-0.9.1 lib/restfulie/client/http/request_follow.rb
restfulie-0.8.1 lib/restfulie/client/http/request_follow.rb