Sha256: 4346384f8f49d1f95e32850d2d3b09b5d9d1d3ca2f1713173d5ae86ff4fba311

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

module G5K
  module Sinatra
    module Helpers
      def returning(thing = nil)
        yield thing
        thing
      end

      def provides *formats
        if params[:format].nil? || !formats.include?(params[:format].to_sym)
          throw :halt, [406, "The accepted types are: #{formats.join(", ")}"]
        end
      end

      def formatted_error(http_status, options = {})
        parser = options.delete(:parser)
        format = options.delete(:format) || params[:format] || "txt"
        body = {:code => (options[:code] || http_status), :message => options[:message], :title => options[:title]}
        if defined?(::Sinatra::Application.logger)
          ::Sinatra::Application.logger.error(body.inspect)
        end
        content_type format.to_sym, :charset => 'utf-8'
        if (parser)
          body = parser.dump(body)
        else
          body = body.map{|(k,v)| [k,v].join("=")}.sort.join(";")
        end
        [http_status, body]
      end

      def compute_etag(*differentiators)
        Digest::SHA1.hexdigest(differentiators.join("."))
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cryx-g5k-0.2.10 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.11 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.5 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.6 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.7 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.8 lib/g5k/sinatra/helpers.rb
cryx-g5k-0.2.9 lib/g5k/sinatra/helpers.rb