rutter 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68f5fb5ae85793cb16d135928e3a26cf043a3eda09338f8c2a03117a16985b2d
4
- data.tar.gz: c9766d83b10a8b6f103f26cc100ca753ecace464770fb739eccd4ecb2f2a876e
3
+ metadata.gz: 12a5714cb11c9589f34960ce325c4a095f4d5c8dbeb15d95e8f9b9be560d15d8
4
+ data.tar.gz: f182eb9db7eb3205a003379fd937864e2dda69b615be63e37ad4cfbc8a523402
5
5
  SHA512:
6
- metadata.gz: 1b0bcaae9355facffd058bf84021a4d62d1365312c254fd4e2ead08b6d907c68791572f47dc3451d35fcda32fb777ff3c94c5785b270cc290187aef3ce3e3260
7
- data.tar.gz: 10e3b47288658c8dd7068447339fa54981684e225542c4c05ac7440dc2fa3f2112c8461ac849b855e16c740e4357d0f7e673a49eabac946a762c21b028882d3a
6
+ metadata.gz: '0885bcb1e09323e8d3e4753a04a2fd192d726c68d4546ba820fa29ae9d9492c847c296022b6e34cb4123808fa87633c5728b2042467d0e1e79ec1d9b679b200a'
7
+ data.tar.gz: 928d8dfd4b82989f196f0a0f25fcf734953c8b77f0884697f756382117de1aaa51f7049e832dc627d1a6dabea43143046ebf08e4e842497a899bd253cee45372
@@ -1,8 +1,7 @@
1
- require:
2
- - rubocop-performance
3
-
1
+ require: rubocop-performance
4
2
  AllCops:
5
3
  TargetRubyVersion: 2.5
4
+ NewCops: enable
6
5
  DisplayCopNames: true
7
6
  Exclude:
8
7
  - "*.gemspec"
@@ -14,6 +13,9 @@ Performance:
14
13
  Style/StringLiterals:
15
14
  EnforcedStyle: double_quotes
16
15
 
16
+ Style/ClassAndModuleChildren:
17
+ Enabled: false
18
+
17
19
  Layout/EmptyLineAfterGuardClause:
18
20
  Enabled: false
19
21
 
@@ -27,9 +29,3 @@ Metrics/BlockLength:
27
29
  Metrics/ModuleLength:
28
30
  Exclude:
29
31
  - "spec/**/*"
30
-
31
- ClassAndModuleChildren:
32
- Enabled: false
33
-
34
- Naming/UncommunicativeMethodParamName:
35
- MinNameLength: 2
@@ -3,20 +3,15 @@ script: "bundle exec rake spec:coverage"
3
3
  cache: bundler
4
4
  env:
5
5
  global:
6
- - CC_TEST_REPORTER_ID=c1b17cd36cd4d025298f11b2a3ee678e4977c3969986f9ee497d6984a2c82c56
6
+ - CODECOV_TOKEN="21653af2-874b-4597-942d-14ddc7c2f8fa"
7
7
  before_install:
8
8
  - "gem update --system"
9
- before_script:
10
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
11
- - chmod +x ./cc-test-reporter
12
- - ./cc-test-reporter before-build
13
- after_script:
14
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
15
9
  rvm:
10
+ - 2.7.0
16
11
  - 2.6.0
17
12
  - 2.5.0
18
- - jruby-9.2.0.0
19
13
  - ruby-head
14
+ - jruby
20
15
  - jruby-head
21
16
  - truffleruby
22
17
  matrix:
data/Gemfile CHANGED
@@ -3,8 +3,8 @@
3
3
  source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
- unless ENV["CI"]
7
- gem "byebug", platform: :mri
8
- end
6
+ gem "rubocop"
7
+ gem "rubocop-performance"
9
8
 
10
9
  gem "simplecov"
10
+ gem 'codecov', require: false
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Tobias Sandelius
3
+ Copyright (c) 2019-2020 Tobias Sandelius
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,13 +1,9 @@
1
1
  # Rutter
2
2
 
3
- HTTP router for Rack.
4
-
5
- ## Status
6
-
7
- Under development, not ready for prime-time just yet.
3
+ HTTP router for Ramverk and Rack.
8
4
 
9
5
  [![Build Status](https://travis-ci.org/sandelius/rutter.svg?branch=master)](https://travis-ci.org/sandelius/rutter)
10
- [![Test Coverage](https://codeclimate.com/github/sandelius/rutter/badges/coverage.svg)](https://codeclimate.com/github/sandelius/rutter/coverage)
6
+ [![codecov](https://codecov.io/gh/sandelius/rutter/branch/master/graph/badge.svg)](https://codecov.io/gh/sandelius/rutter)
11
7
  [![Inline docs](http://inch-ci.org/github/sandelius/rutter.svg?branch=master)](http://inch-ci.org/github/sandelius/rutter)
12
8
 
13
9
  ## Installation
@@ -91,10 +91,12 @@ module Rutter
91
91
  # Application to mount.
92
92
  # @param at [String]
93
93
  # Path prefix to match.
94
+ # @param host [Regexp]
95
+ # Match the given host pattern.
94
96
  #
95
97
  # @return [Rutter::Mount]
96
- def mount(app, at:)
97
- route = Mount.new(at, app)
98
+ def mount(app, at:, host: nil)
99
+ route = Mount.new(at, app, host: host)
98
100
  @flat_map << route
99
101
  VERBS.each { |verb| @verb_map[verb] << route }
100
102
  route
@@ -131,12 +133,12 @@ module Rutter
131
133
  # # => "/login?return_to=/"
132
134
  # router.path(:book, id: 82)
133
135
  # # => "/books/82"
134
- def path(name, *args)
136
+ def path(name, **args)
135
137
  unless (route = @named_map[name])
136
138
  raise "No route called '#{name}' was found"
137
139
  end
138
140
 
139
- route.expand(*args)
141
+ route.expand(**args)
140
142
  end
141
143
 
142
144
  # Generates a full URL from the given arguments.
@@ -178,7 +180,7 @@ module Rutter
178
180
  host += "#{args.delete(:subdomain)}." if args.key?(:subdomain)
179
181
  host += @uri.host
180
182
  host += ":#{@uri.port}" if @uri.port != 80 && @uri.port != 443
181
- host + path(name, args)
183
+ host + path(name, **args)
182
184
  end
183
185
 
184
186
  # Add a new, frozen, route to the map.
@@ -7,6 +7,15 @@ module Rutter
7
7
  #
8
8
  # @private
9
9
  class Mount < Route
10
+ # @see Rutter::Route#initialize
11
+ #
12
+ # @private
13
+ def initialize(path, endpoint, constraints = nil, host: nil)
14
+ @host = host
15
+
16
+ super(path, endpoint, constraints)
17
+ end
18
+
10
19
  # Matches the app pattern against environment.
11
20
  #
12
21
  # @param env [Hash]
@@ -15,7 +24,22 @@ module Rutter
15
24
  # @return [nil, String]
16
25
  # Returns the matching substring or nil on no match.
17
26
  def match?(env)
27
+ return if @host && !@host.match?(host(env))
28
+
18
29
  @pattern.peek(env["PATH_INFO"])
19
30
  end
31
+
32
+ private
33
+
34
+ # @private
35
+ def host(env)
36
+ env["rutter.parsed_host"] ||= begin
37
+ if (forwarded = env["HTTP_X_FORWARDED_HOST"])
38
+ forwarded.split(/,\s?/).last
39
+ else
40
+ env["HTTP_HOST"] || env["SERVER_NAME"] || env["SERVER_ADDR"]
41
+ end
42
+ end
43
+ end
20
44
  end
21
45
  end
@@ -53,10 +53,11 @@ module Rutter
53
53
  protected
54
54
 
55
55
  # @private
56
- def method_missing(method_name, *args)
56
+ def method_missing(method_name, **args)
57
57
  named_route, type = method_name.to_s.split(/\_(path|url)\z/)
58
58
  return super unless type
59
- @router.public_send(type, named_route.to_sym, *args)
59
+
60
+ @router.public_send(type, named_route.to_sym, **args)
60
61
  end
61
62
 
62
63
  # @private
@@ -34,8 +34,8 @@ module Rutter
34
34
  end
35
35
 
36
36
  # @see Rutter::Builder#mount
37
- def mount(app, at:)
38
- @router.mount app, at: Naming.join(@path, at)
37
+ def mount(app, at:, host: nil)
38
+ @router.mount app, at: Naming.join(@path, at), host: host
39
39
  end
40
40
 
41
41
  # @see Rutter::Builder#scope
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Rutter
4
4
  # Current version number.
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
@@ -21,8 +21,6 @@ RSpec.describe "Rack compatible", type: :request do
21
21
  .to eq(200)
22
22
  expect(last_response.body)
23
23
  .to eq("I say, hello-world")
24
- expect(last_response.headers["Content-Length"])
25
- .to eq("18")
26
24
  end
27
25
  end
28
26
 
@@ -2,10 +2,6 @@
2
2
 
3
3
  require "bundler/setup"
4
4
 
5
- unless ENV["CI"]
6
- require "byebug"
7
- end
8
-
9
5
  if ENV["COVERAGE"] == "true"
10
6
  require "simplecov"
11
7
 
@@ -13,6 +9,11 @@ if ENV["COVERAGE"] == "true"
13
9
  command_name "spec"
14
10
  add_filter "spec"
15
11
  end
12
+
13
+ if ENV["CODECOV_TOKEN"]
14
+ require "codecov"
15
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
16
+ end
16
17
  end
17
18
 
18
19
  # Require support (helper) modules
@@ -18,6 +18,23 @@ module Rutter
18
18
  expect(route.match?(env_for("/admin/books")))
19
19
  .to eq("/admin")
20
20
  end
21
+
22
+ it "matches host if given" do
23
+ route = router.mount endpoint, at: "/v1", host: /\Aapi\./
24
+
25
+ expect(route.match?(env_for("http://example.com/v1/books")))
26
+ .to be_nil
27
+ expect(route.match?(env_for("http://api.example.com/v1/books")))
28
+ .to eq("/v1")
29
+ end
30
+
31
+ it "HTTP_X_FORWARDED_HOST are supported" do
32
+ route = router.mount endpoint, at: "/v1", host: /\Aapi\./
33
+ env = env_for("/v1/books", "HTTP_X_FORWARDED_HOST" => "api.example.com")
34
+
35
+ expect(route.match?(env))
36
+ .to eq("/v1")
37
+ end
21
38
  end
22
39
 
23
40
  describe "#path" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rutter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Sandelius
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-28 00:00:00.000000000 Z
11
+ date: 2020-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustermann
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: 2.5.0
150
150
  requirements: []
151
- rubygems_version: 3.0.3
151
+ rubygems_version: 3.1.2
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: HTTP router for Rack.