Sha256: 975e441d6c9d66a159bb01d97671d711388e71c8da4722fd37472658c8b0e2e2

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require "syro"

require "syro/versioning/version"

class Syro # :nodoc:

  # Include on `Syro::Deck` to enable API versioning
  module Versioning

    # Describe a version block inside a Syro app.
    #
    # @param number [Integer] The minimum version number to match.
    # @param block The route to process if version matches.
    # @returns [void]
    #
    # @example
    #     Syro.new(AppDeck) {
    #       version(20150101) {
    #         get {
    #           res.write "Version 2015-01-01"
    #         }
    #       }
    #       version(1) {
    #         get {
    #           res.write "Version 1"
    #         }
    #       }
    #     }
    #
    def version(number,&block)
      return if number > request_version
      instance_eval(&block)
    end

    # Fetch the version for the current request's Accept header
    #
    # @returns [Integer] The matched version number
    #
    def request_version
      @request_version ||= if match = /version=(\d+)/.match(req.env['HTTP_ACCEPT'])
        match[1].to_i
      else
        now = Time.now
        now.day + 100*now.month + 10000*now.year
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
syro-versioning-0.1.0 lib/syro/versioning.rb