Sha256: 27ca6fd4d2a0d1d2d7e602595a82078bf92e8b9926247199702243b19313a703

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

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]}
        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
        error(http_status, body)
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cryx-g5k-0.2.3 lib/g5k/sinatra/helpers.rb