diff --git a/.standard.yml b/.standard.yml index 94e02a5..c19ce03 100644 --- a/.standard.yml +++ b/.standard.yml @@ -1,2 +1 @@ -ignore: # default: [] - - 'test/sample/node_modules/**/*' +ruby_version: 2.7 diff --git a/gemfiles/rails_6.0.gemfile b/gemfiles/rails_6.0.gemfile index 30dbdb0..cfe6556 100644 --- a/gemfiles/rails_6.0.gemfile +++ b/gemfiles/rails_6.0.gemfile @@ -1,12 +1,12 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'rails', '~> 6.0.0' +gem "rails", "~> 6.0.0" group :development, :test do - gem 'guard', require: false - gem 'guard-minitest', require: false - gem 'pry' - gem 'pry-byebug', platforms: [:mri] + gem "guard", require: false + gem "guard-minitest", require: false + gem "pry" + gem "pry-byebug", platforms: [:mri] end -gemspec path: '../' +gemspec path: "../" diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile index 92b61d6..f41ad23 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_6.1.gemfile @@ -1,12 +1,12 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'rails', '~> 6.1.0' +gem "rails", "~> 6.1.0" group :development, :test do - gem 'guard', require: false - gem 'guard-minitest', require: false - gem 'pry' - gem 'pry-byebug', platforms: [:mri] + gem "guard", require: false + gem "guard-minitest", require: false + gem "pry" + gem "pry-byebug", platforms: [:mri] end -gemspec path: '../' +gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index 5b97222..02bc456 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -1,12 +1,12 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'rails', '~> 7.0.0' +gem "rails", "~> 7.0.0" group :development, :test do - gem 'guard', require: false - gem 'guard-minitest', require: false - gem 'pry' - gem 'pry-byebug', platforms: [:mri] + gem "guard", require: false + gem "guard-minitest", require: false + gem "pry" + gem "pry-byebug", platforms: [:mri] end -gemspec path: '../' +gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile index 6a9b5e8..a4e671d 100644 --- a/gemfiles/rails_7.1.gemfile +++ b/gemfiles/rails_7.1.gemfile @@ -1,12 +1,12 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'rails', '~> 7.1.0' +gem "rails", "~> 7.1.0" group :development, :test do - gem 'guard', require: false - gem 'guard-minitest', require: false - gem 'pry' - gem 'pry-byebug', platforms: [:mri] + gem "guard", require: false + gem "guard-minitest", require: false + gem "pry" + gem "pry-byebug", platforms: [:mri] end -gemspec path: '../' +gemspec path: "../" diff --git a/lib/serviceworker/handlers.rb b/lib/serviceworker/handlers.rb index 9514b48..7f5b3d4 100644 --- a/lib/serviceworker/handlers.rb +++ b/lib/serviceworker/handlers.rb @@ -25,7 +25,7 @@ def ===(other) def handler_for_name(name) available_handlers = %w[sprockets webpacker rack] if available_handlers.include?(name.to_s) - send("#{name}_handler") + send(:"#{name}_handler") else raise ServiceWorker::Error, "Unknown handler #{name.inspect}. Please use one of #{available_handlers.inspect}" diff --git a/lib/serviceworker/handlers/webpacker_handler.rb b/lib/serviceworker/handlers/webpacker_handler.rb index 1b8aa42..bf5c0c3 100644 --- a/lib/serviceworker/handlers/webpacker_handler.rb +++ b/lib/serviceworker/handlers/webpacker_handler.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'rack/file' +require "rack/file" begin - require 'webpacker' + require "webpacker" rescue LoadError # ignore end @@ -11,15 +11,15 @@ module ServiceWorker module Handlers class WebpackerHandler def call(env) - path_info = env.fetch('serviceworker.asset_name') + path_info = env.fetch("serviceworker.asset_name") path = Webpacker.manifest.lookup(path_info) if Webpacker.dev_server.running? proxy = Webpacker::DevServerProxy.new - proxy.call(env.merge('PATH_INFO' => path)) + proxy.call(env.merge("PATH_INFO" => path)) else - file_server.call(env.merge('PATH_INFO' => path)) + file_server.call(env.merge("PATH_INFO" => path)) end end diff --git a/lib/serviceworker/route.rb b/lib/serviceworker/route.rb index 29ad87d..d00c8f1 100644 --- a/lib/serviceworker/route.rb +++ b/lib/serviceworker/route.rb @@ -4,11 +4,11 @@ module ServiceWorker class Route attr_reader :path_pattern, :asset_pattern, :options - RouteMatch = Struct.new(:path, :asset_name, :headers, :options) { + RouteMatch = Struct.new(:path, :asset_name, :headers, :options) do def to_s asset_name end - } + end def self.webpacker?(options) options.key?(:pack) && Handlers.webpacker? @@ -54,9 +54,9 @@ def resolver class AssetResolver PATH_INFO = "PATH_INFO" DEFAULT_WILDCARD_NAME = :paths - WILDCARD_PATTERN = %r{/\*([^/]*)}.freeze - NAMED_SEGMENTS_PATTERN = %r{/([^/]*):([^:$/]+)}.freeze - LEADING_SLASH_PATTERN = %r{^/}.freeze + WILDCARD_PATTERN = %r{/\*([^/]*)} + NAMED_SEGMENTS_PATTERN = %r{/([^/]*):([^:$/]+)} + LEADING_SLASH_PATTERN = %r{^/} INTERPOLATION_PATTERN = Regexp.union( /%%/, /%\{(\w+)\}/ # matches placeholders like "%{foo}" @@ -112,14 +112,14 @@ def path_captures(regexp, path) params = if @wildcard_name {@wildcard_name => path_match[1].to_s.split("/")} else - Hash[path_match.names.map(&:to_sym).zip(path_match.captures)] + path_match.names.map(&:to_sym).zip(path_match.captures).to_h end params.delete(:format) if params.key?(:format) && params[:format].nil? params end def interpolate_captures(string, captures) - string.gsub(INTERPOLATION_PATTERN) { |match| + string.gsub(INTERPOLATION_PATTERN) do |match| if match == "%%" "%" else @@ -128,7 +128,7 @@ def interpolate_captures(string, captures) value = value.call(captures) if value.respond_to?(:call) Regexp.last_match(3) ? format("%#{Regexp.last_match(3)}", value) : value end - }.gsub(LEADING_SLASH_PATTERN, "") + end.gsub(LEADING_SLASH_PATTERN, "") end end end diff --git a/serviceworker-rails.gemspec b/serviceworker-rails.gemspec index b68ebf4..0684e43 100644 --- a/serviceworker-rails.gemspec +++ b/serviceworker-rails.gemspec @@ -1,33 +1,33 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'serviceworker/rails/version' +require "serviceworker/rails/version" Gem::Specification.new do |spec| - spec.name = 'serviceworker-rails' + spec.name = "serviceworker-rails" spec.version = ServiceWorker::Rails::VERSION - spec.authors = ['Ross Kaffenberger'] - spec.email = ['rosskaff@gmail.com'] + spec.authors = ["Ross Kaffenberger"] + spec.email = ["rosskaff@gmail.com"] - spec.summary = 'ServiceWorker for Rails 5+' - spec.description = 'Integrates ServiceWorker into the Rails asset pipeline.' - spec.homepage = 'https://github.com/rossta/serviceworker-rails' - spec.license = 'MIT' + spec.summary = "ServiceWorker for Rails 5+" + spec.description = "Integrates ServiceWorker into the Rails asset pipeline." + spec.homepage = "https://github.com/rossta/serviceworker-rails" + spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = 'exe' + spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ['lib'] + spec.require_paths = ["lib"] - spec.add_dependency 'railties', '>= 6.0' + spec.add_dependency "railties", ">= 6.0" - spec.add_development_dependency 'minitest', '~> 5.0' - spec.add_development_dependency 'rack-test' - spec.add_development_dependency 'rails' - spec.add_development_dependency 'rake' - spec.add_development_dependency 'simplecov' - spec.add_development_dependency 'sprockets', '~> 3.0' - spec.add_development_dependency 'sprockets-rails' - spec.add_development_dependency 'standard' + spec.add_development_dependency "minitest", "~> 5.0" + spec.add_development_dependency "rack-test" + spec.add_development_dependency "rails" + spec.add_development_dependency "rake" + spec.add_development_dependency "simplecov" + spec.add_development_dependency "sprockets", "~> 3.0" + spec.add_development_dependency "sprockets-rails" + spec.add_development_dependency "standard" end diff --git a/test/sample/config/initializers/serviceworker.rb b/test/sample/config/initializers/serviceworker.rb index 8fa1dcb..38913ce 100644 --- a/test/sample/config/initializers/serviceworker.rb +++ b/test/sample/config/initializers/serviceworker.rb @@ -5,7 +5,7 @@ config.serviceworker.routes.draw do match "/header-serviceworker.js" => "another/serviceworker.js", - :headers => {"X-Resource-Header" => "A resource"} + :headers => {"X-Resource-Header" => "A resource"} match "/nested/serviceworker.js", asset: "another/serviceworker.js" diff --git a/test/serviceworker/rack_integration_test.rb b/test/serviceworker/rack_integration_test.rb index 50eb8d3..2de3aa3 100644 --- a/test/serviceworker/rack_integration_test.rb +++ b/test/serviceworker/rack_integration_test.rb @@ -13,7 +13,7 @@ def router ServiceWorker::Router.new do match "/serviceworker.js" => "assets/serviceworker.js" match "/cacheable-serviceworker.js" => "assets/serviceworker.js", - :headers => {"Cache-Control" => "public, max-age=12345"} + :headers => {"Cache-Control" => "public, max-age=12345"} end end diff --git a/test/serviceworker/route_test.rb b/test/serviceworker/route_test.rb index 966fe7d..0ae5895 100644 --- a/test/serviceworker/route_test.rb +++ b/test/serviceworker/route_test.rb @@ -20,38 +20,38 @@ def test_initialize_route_with_asset def test_match match "/*", "foo", "/", "foo" - match "/*", "%{paths}", "/", "" + match "/*", "%s", "/", "" match "/*", "foo", "/foo", "foo" - match "/*", "%{paths}", "/foo", "foo" + match "/*", "%s", "/foo", "foo" match "/*", "foo", "/foo/bar/baz", "foo" - match "/*", "%{paths}", "/foo/bar/baz", "foo/bar/baz" + match "/*", "%s", "/foo/bar/baz", "foo/bar/baz" match "/*/foobar.js", "foobar.js", "/not/found/foo/bar.js", nil match "/*/foobar.js", "foobar.js", "/is/found/foobar.js", "foobar.js" match "/*stuff", "foo", "/", "foo" - match "/*stuff", "%{stuff}/foo", "/", "foo" - match "/*stuff", "%{stuff}/bar", "/foo", "foo/bar" - match "/*stuff", "%{stuff}/bar", "/foo/", "foo/bar" - match "/*stuff", "%{stuff}/boo", "/foo/bar/baz", "foo/bar/baz/boo" - - match "/foo/*", "%{paths}", "/foo", "" - match "/foo/*", "%{paths}", "/foo/bar", "bar" - match "/foo/*", "%{paths}", "/foo/bar/baz", "bar/baz" - match "/foo/*stuff", "%{stuff}", "/", nil - match "/foo/*stuff", "%{stuff}", "/foo", "" - match "/foo/*stuff", "%{stuff}", "/foo/bar/baz", "bar/baz" + match "/*stuff", "%s/foo", "/", "foo" + match "/*stuff", "%s/bar", "/foo", "foo/bar" + match "/*stuff", "%s/bar", "/foo/", "foo/bar" + match "/*stuff", "%s/boo", "/foo/bar/baz", "foo/bar/baz/boo" + + match "/foo/*", "%s", "/foo", "" + match "/foo/*", "%s", "/foo/bar", "bar" + match "/foo/*", "%s", "/foo/bar/baz", "bar/baz" + match "/foo/*stuff", "%s", "/", nil + match "/foo/*stuff", "%s", "/foo", "" + match "/foo/*stuff", "%s", "/foo/bar/baz", "bar/baz" match "/", "", "/", "" match "/", "", "/foo", nil match "/foo", "", "/", nil match "/foo", "", "/foo", "" match "/:id", nil, "/42", ":id" match "/:id", nil, "/", nil - match "/posts/:id", "%{id}", "/posts/42", "42" - match "/posts/:id", "%{id}", "/posts", nil - match "/:x/:y", "%{x}/%{y}", "/a/b", "a/b" - match "/posts/:id", "%{id}.js", "/posts/42.js", "42.js" - match "/api/v:version", "%{version}", "/api/v2", "2" - match "/api/v:version/things", "%{version}", "/api/v2/things", "2" + match "/posts/:id", "%s", "/posts/42", "42" + match "/posts/:id", "%s", "/posts", nil + match "/:x/:y", "%s/%s", "/a/b", "a/b" + match "/posts/:id", "%s.js", "/posts/42.js", "42.js" + match "/api/v:version", "%s", "/api/v2", "2" + match "/api/v:version/things", "%s", "/api/v2/things", "2" end def test_match_route_pack_true