Sha256: 36e08ab3fff834cbd40e0f351920b5d55a6cfbe1532b3938c91c4249f718b364

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 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, env)
    resp = flow.continue(request, env)
    if !resp.respond_to?(:code)
      return resp
    end
    
    if should_follow?(resp)
      follow_this resp, request
    else
      resp
    end
  end

  protected
  
  def should_follow?(response)
    code = response.code.to_i
    if code==201 && !response.body.empty?
      false
    else
      follow_codes.include?(code)
    end
  end
  
  def follow_this(response, request)
    location = extract_location(response)
    req = Restfulie.at(location)
    if accept = request.headers['Accept']
      req.accepts(accept)
    end
    req.get
  end

  def extract_location(response)
    location = response.response.headers['location'] || response.response.headers['Location']
    if location.nil?
      raise Restfulie::Client::HTTP::Error::AutoFollowWithoutLocationError.new(request, response)
    end
    location.first
  end
  
    def follow_codes
      @follow_codes ||= [201,301,302,303,307]        
    end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
restfulie-nosqlite-1.0.4 lib/restfulie/client/feature/follow_request.rb
restfulie-1.1.1 lib/restfulie/client/feature/follow_request.rb
restfulie-1.1.0 lib/restfulie/client/feature/follow_request.rb
restfulie-nosqlite-1.0.3 lib/restfulie/client/feature/follow_request.rb
restfulie-1.0.3 lib/restfulie/client/feature/follow_request.rb