Sha256: b502f6e90e54c8021ea714934b16aab86e183dd99af47e4690766139a8325edb

Contents?: true

Size: 1.62 KB

Versions: 27

Compression:

Stored size: 1.62 KB

Contents

module Restfully
  module HTTP
    class Response
      include Helper
      
      attr_reader :io, :code, :head
      
      def initialize(session, code, head, body)
        @session = session
        @io = StringIO.new
        case body
        when String
          @io << body
        else
          body.each{|chunk| @io << chunk}
        end
        @io.rewind
        
        @code = code
        @head = sanitize_head(head)
      end
      
      def body
        @io.rewind
        @body = @io.read
        @io.rewind
        @body
      end
      
      def io
        @io
      end
      
      def media_type
        @media_type ||= begin
          m = MediaType.find(head['Content-Type'])
          raise Error, "Cannot find a media-type for content-type=#{head['Content-Type'].inspect}" if m.nil?
          @session.logger.debug "Using media-type #{m.inspect}"
          m.new(io, @session)
        end
      end
      
      # TODO: we could also search for Link headers here.
      def links
        media_type.links
      end
      
      def property(key)
        media_type.property(key)
      end
      
      def allow?(http_method)
        http_method = http_method.downcase.to_sym
        return true if http_method == :get
        (
          media_type.respond_to?(:allow?) && 
          media_type.allow?(http_method)
        ) || (
          head['Allow'] &&
          head['Allow'].split(/\s*,\s*/).map{|m| 
            m.downcase.to_sym
          }.include?(http_method)
        )          
      end
      
      def inspect
        "#{code}, head=#{head.inspect}, body=#{body.inspect}"
      end
      
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
restfully-1.3.0 lib/restfully/http/response.rb
restfully-1.2.0 lib/restfully/http/response.rb
restfully-1.1.1 lib/restfully/http/response.rb
restfully-1.1.0 lib/restfully/http/response.rb
restfully-1.0.8 lib/restfully/http/response.rb
restfully-1.0.7 lib/restfully/http/response.rb
restfully-1.0.6 lib/restfully/http/response.rb
restfully-1.0.5 lib/restfully/http/response.rb
restfully-1.0.4 lib/restfully/http/response.rb
restfully-1.0.3 lib/restfully/http/response.rb
restfully-1.0.2 lib/restfully/http/response.rb
restfully-1.0.1 lib/restfully/http/response.rb
restfully-1.0.0 lib/restfully/http/response.rb
restfully-1.0.0.rc2 lib/restfully/http/response.rb
restfully-1.0.0.rc1 lib/restfully/http/response.rb
restfully-0.8.8 lib/restfully/http/response.rb
restfully-0.8.7 lib/restfully/http/response.rb
restfully-0.8.6 lib/restfully/http/response.rb
restfully-0.8.5 lib/restfully/http/response.rb
restfully-0.8.4 lib/restfully/http/response.rb