mustermann-grape 1.0.2 → 1.1.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: 53d8315ebe7d6f7d074dc0477fdac54e0f8c420450f30406c8d742f274dfd72f
4
- data.tar.gz: a9bc253a7d11d2c84d027703b186a1e7b04b980820416565af140024f65e6b2f
3
+ metadata.gz: 6ccb4995f214f92cb170ebaaf19eb1eb25936ab3d578020cf08a36a0adf50101
4
+ data.tar.gz: fb6d1a61a8ab987fba38e6b356c4ff6dffb59961de9767a03cde70f0b41b3b95
5
5
  SHA512:
6
- metadata.gz: acde3ddbc4e0580de9232ae572f9a94cc5b80b5a56a9c5e75edbd7abb165dfeef1cd59ea40bf00aa3231beb8565d68273980af99fe817f1da5d58b47af77f41b
7
- data.tar.gz: b804417c751c40c11bae887170748f640a7973c2343e26a9b3c15af466f963e7d67439f264d8b3b813ab61f6da347360cd9d85f656e620e52ab867861f81fc1d
6
+ metadata.gz: da5216e8f3905d70bf4f42893c67519171cf538f13c380fc4e1ea07ba516ba2b5b5e1ac728d76da7a5abdc72449ee6c2fcebea708a15346e70d4492dab150175
7
+ data.tar.gz: 34259f47c0ac7accb57e222cd47290bb8a45981a169ed4399ab321321e65c90f27a878475d25f57fd50aa6c952c373b10a40b212df3eb07d5a11486d5da2ae65
@@ -9,7 +9,7 @@ jobs:
9
9
 
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: ['2.5', '2.6', '2.7']
12
+ ruby-version: ['2.6', '2.7', '3.0']
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v3
@@ -19,5 +19,4 @@ jobs:
19
19
  ruby-version: ${{ matrix.ruby-version }}
20
20
  bundler-cache: true
21
21
  - name: Run tests
22
- run: bundle exec rake
23
-
22
+ run: bundle exec rspec
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ### 1.1.0 (2023/11/24)
2
+
3
+ #### Fixes
4
+
5
+ * [#21](https://github.com/ruby-grape/mustermann-grape/pull/21): Introducing the `params` option to `Mustermann::Grape` to enhance matching by offering detailed information regarding the parameter types. - [@jcagarcia](https://github.com/jcagarcia).
data/README.md CHANGED
@@ -7,12 +7,17 @@ This gem implements the `grape` pattern type for Mustermann.
7
7
  ## Overview
8
8
 
9
9
  **Supported options:**
10
- `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, `converters` and `ignore_unknown_options`
10
+ `capture`, `converters`, `except`, `greedy`, `ignore_unknown_options`, `params`, `space_matches_plus` and `uri_decode`
11
11
 
12
12
  ``` ruby
13
13
  require 'mustermann/grape'
14
14
 
15
15
  Mustermann.new('/:id', type: :grape).params('/foo') # => { id: 'foo' }
16
+
17
+ # Providing params option
18
+ Mustermann.new('/:id', type: :grape, params: {"id"=>{:type=>"Integer"}}).params('/1') # => { id: '1'}
19
+ Mustermann.new('/:id', type: :grape, params: {"id"=>{:type=>"Integer"}}).params('/foo') # => nil
20
+ Mustermann.new('/:id', type: :grape, params: {"id"=>{:type=>"String"}}).params('/foo') # => { id: 'foo'}
16
21
  ```
17
22
 
18
23
  ## Syntax
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MustermannGrape
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -13,11 +13,28 @@ module Mustermann
13
13
  # @see file:README.md#grape Syntax description in the README
14
14
  class Grape < AST::Pattern
15
15
  register :grape
16
+ supported_options :params
16
17
 
17
18
  on(nil, '?', ')') { |c| unexpected(c) }
18
19
 
19
20
  on('*') { |_c| scan(/\w+/) ? node(:named_splat, buffer.matched) : node(:splat) }
20
- on(':') { |_c| node(:capture, constraint: "[^/\\?#\.]") { scan(/\w+/) } }
21
+ on(':') do |_c|
22
+ param_name = scan(/\w+/)
23
+ return node(:capture, param_name, constraint: "[^/\\?#\.]") { scan(/\w+/) } unless pattern
24
+
25
+ params_opt = pattern.options[:params]
26
+ if params_opt && params_opt[param_name] && params_opt[param_name][:type]
27
+ param_type = params_opt[param_name][:type]
28
+ case(param_type)
29
+ when "Integer"
30
+ node(:capture, param_name, constraint: /\d/) { scan(/\w+/) }
31
+ else
32
+ node(:capture, param_name, constraint: "[^/\\?#\.]") { scan(/\w+/) }
33
+ end
34
+ else
35
+ node(:capture, param_name, constraint: "[^/\\?#\.]") { scan(/\w+/) }
36
+ end
37
+ end
21
38
  on('\\') { |_c| node(:char, expect(/./)) }
22
39
  on('(') { |_c| node(:optional, node(:group) { read unless scan(')') }) }
23
40
  on('|') { |_c| node(:or) }
data/spec/grape_spec.rb CHANGED
@@ -751,4 +751,33 @@ describe Mustermann::Grape do
751
751
  example { pattern.peek_params('/foo bar') .should be_nil }
752
752
  end
753
753
  end
754
+
755
+ context 'when params option is provided' do
756
+ context 'and they contain parameters information' do
757
+ pattern '/foo/:id', params: {"id"=>{:type=>"Integer"}} do
758
+ it { should match('/foo/1') }
759
+ it { should_not match('/foo/bar') }
760
+ end
761
+
762
+ context 'and inherit routes are present' do
763
+ pattern '/foo/:id/bar/:reference', params: {"id"=>{:type=>"Integer"}, "reference"=>{:type=>"String"}} do
764
+ it { should match('/foo/1/bar/wadus') }
765
+ it { should_not match('/foo/bar/bar/wadus') }
766
+ end
767
+
768
+ pattern '/foo/:id/bar/:reference', params: {"id"=>{:type=>"Integer"}, "reference"=>{:type=>"Integer"}} do
769
+ it { should match('/foo/1/bar/1') }
770
+ it { should_not match('/foo/1/bar/wadus') }
771
+ it { should_not match('/foo/bar/bar/1') }
772
+ end
773
+ end
774
+ end
775
+
776
+ context 'and they do NOT contain parameters information' do
777
+ pattern '/foo/:id' do
778
+ it { should match('/foo/1') }
779
+ it { should match('/foo/bar') }
780
+ end
781
+ end
782
+ end
754
783
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustermann-grape
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - namusyaka
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-05-03 00:00:00.000000000 Z
13
+ date: 2023-11-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mustermann
@@ -34,6 +34,8 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - ".github/workflows/tests.yml"
36
36
  - ".gitignore"
37
+ - ".rspec"
38
+ - CHANGELOG.md
37
39
  - Gemfile
38
40
  - LICENSE
39
41
  - README.md
@@ -62,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
64
  - !ruby/object:Gem::Version
63
65
  version: '0'
64
66
  requirements: []
65
- rubygems_version: 3.1.3
67
+ rubygems_version: 3.3.7
66
68
  signing_key:
67
69
  specification_version: 4
68
70
  summary: Grape syntax for Mustermann