Sha256: ef1f1c26836f7dd962a73d92ced44daabbca05244826ae39bd1e77d29e2b642c

Contents?: true

Size: 990 Bytes

Versions: 6

Compression:

Stored size: 990 Bytes

Contents

# frozen_string_literal: true
module ServiceWorker
  class Router
    PATH_INFO = "PATH_INFO".freeze

    def self.default
      new.draw_default
    end

    attr_reader :routes

    def initialize
      @routes = []

      draw(&Proc.new) if block_given?
    end

    def draw(&block)
      return self unless block_given?

      if block.arity == 1
        yield(self)
      else
        instance_eval(&block)
      end

      self
    end

    def draw_default
      draw { get "/serviceworker.js" }
    end

    def match(path, *args)
      if path.is_a?(Hash)
        opts = path.to_a
        path, asset = opts.shift
        args = [asset, opts.to_h]
      end

      Route.new(path, *args).tap do |route|
        @routes << route
      end
    end
    alias get match

    def any?
      @routes.any?
    end

    def match_route(env)
      path = env[PATH_INFO]
      @routes.each do |route|
        match = route.match(path) and return match
      end
      nil
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
serviceworker-rails-0.5.5 lib/serviceworker/router.rb
serviceworker-rails-0.5.4 lib/serviceworker/router.rb
serviceworker-rails-0.5.3 lib/serviceworker/router.rb
serviceworker-rails-0.5.2 lib/serviceworker/router.rb
serviceworker-rails-0.5.1 lib/serviceworker/router.rb
serviceworker-rails-0.4.0 lib/serviceworker/router.rb