Sha256: deb599a3b55afaa7d5a1104aca2114deebb605d4c9d22e97e28195fe470f864d

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# ==== FollowLink 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::FollowLinkExecutor.new("http://restfulie.com") #this class includes FollowLink module.
# @executor.at('/custom/songs').accepts('application/atom+xml').follow(201).post!("custom").code
class Restfulie::Client::Feature::FollowRequest
  
  def follow(code = nil)
    unless code.nil? or follow_codes.include?(code)
      follow_codes << code
    end
    self
  end

  def execute(flow, request, response, env)
    resp = flow.continue(request, response, env)
    if !resp.respond_to?(:code)
      return resp
    end
    code = resp.code.to_i
    if follow_codes.include?(code)
      if code==201 && !resp.body.empty?
        resp
      else
        location = resp.response.headers['location'] || resp.response.headers['Location']
        raise Error::AutoFollowWithoutLocationError.new(request, resp) unless location
        # use the first location available
        location = location[0]
        Restfulie.at(location).accepts(request.headers['Accept']).get
      end
    else
      resp
    end
  end

  protected

    def follow_codes
      @follow_codes ||= [201,301,302,303,307]        
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restfulie-1.0.0.beta1 lib/restfulie/client/feature/follow_request.rb
restfulie-0.1.0.beta1 lib/restfulie/client/feature/follow_request.rb