rails-routes-prettier 0.1.0 → 1.0.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: 215b333061f883042ac0637f1fe843190ceacbd5de06b593bea8e66f772f6e2a
4
- data.tar.gz: ea1de7273e437492e2ff3edb4a8a5af5295f230a815e07ff99425ee62869e744
3
+ metadata.gz: 405a6227c2f35a4232a4f2c5a5a85eef6d47002f41b538ce05a28e220e0e2501
4
+ data.tar.gz: f01a0ac7faf3ebe1906413a21418914d918e401d15dd9309832d09d0916de715
5
5
  SHA512:
6
- metadata.gz: 30d06a490816c3acda127c4d28a7ce99492439074b9eff88de5d2e8cedd14aa52e70f55068fbce1024457770c77d39c388feda2a41426575ab8c7ac2f8d9529f
7
- data.tar.gz: 4b3f9d8897635dfa8a95a4504b491b26f021db35776aa6901d4856e90ba81ea4026e544b47ba92e91a6b3fd98119271a178df4db3d0ab5f435221d7f1a0d03c4
6
+ metadata.gz: 2bd0a12417f08f5b356be8fc26273ad72598c3b2f0d5db2f237bf9ee3f48745b61e36d97b57a86135aa65ed445f8a83026a3a7fe3e35ecb61a6ccc1730bf6d9c
7
+ data.tar.gz: 4d52e9ee8397756b051559ad367c4135e945540c859aab20509dba81f362dc6d27b110c2396581ea304397e801da531b9a4d562bc2835649e1fc3042808ccd7f
data/README.md CHANGED
@@ -27,7 +27,28 @@ gem install rails-routes-prettier
27
27
 
28
28
  ## Usage
29
29
 
30
- TODO: Write usage instructions here
30
+ To view the routes in your application, you can use the following code:
31
+
32
+ ```ruby
33
+ Rails::Routes::Prettier.all
34
+ # => [#<Route name: 'root', verb: 'GET', path: '/', controller: 'SwaggerController', action: 'index' req_options: {}>, ...]
35
+ ```
36
+
37
+ This will display all of the routes in your application in a clean and readable format. You can also search for routes by certain fields using the following methods:
38
+
39
+ - `find_by_name`: Searches for routes by name.
40
+ - `find_by_verb`: Searches for routes by HTTP verb.
41
+ - `find_by_path`: Searches for routes by path.
42
+ - `find_by_req_options`: Searches for routes by requirements options.
43
+ - `find_by_controller`: Searches for routes by controller.
44
+ - `find_by_action`: Searches for routes by action.
45
+
46
+ For example, you can search for a route with the verb `GET` by using the following code:
47
+
48
+ ```ruby
49
+ Rails::Routes::Prettier.find_by_verb('GET')
50
+ # => [#<Route name: 'root', verb: 'GET', path: '/', controller: 'SwaggerController', action: 'index' req_options: {}>, ...]
51
+ ```
31
52
 
32
53
  ## Development
33
54
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Routes
5
+ module Prettier
6
+ class Formatter
7
+ attr_reader :routes
8
+
9
+ def initialize
10
+ @routes = []
11
+ end
12
+
13
+ def section_title(title); end
14
+
15
+ def header(routes); end
16
+
17
+ def result
18
+ routes
19
+ end
20
+
21
+ def section(routes)
22
+ routes.map do |route|
23
+ @routes << Rails::Routes::Prettier::Objects::Route.new(route)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Routes
5
+ module Prettier
6
+ module Objects
7
+ class Route
8
+ attr_reader :route
9
+
10
+ def initialize(route)
11
+ @route = route
12
+ end
13
+
14
+ def name
15
+ route[:name]
16
+ end
17
+
18
+ def verb
19
+ route[:verb]
20
+ end
21
+
22
+ def path
23
+ route[:path].gsub(/(\(\.:format\))$/, '')
24
+ end
25
+
26
+ def reqs
27
+ route[:reqs]
28
+ end
29
+
30
+ def controller
31
+ return nil if redirect? || custom_router? || basic_auth?
32
+
33
+ reqs.split('#').first.split('/').map(&:camelize).join('::').concat('Controller')
34
+ end
35
+
36
+ def action
37
+ return nil if redirect? || custom_router? || basic_auth?
38
+
39
+ reqs.split('#').last.split(' ').first
40
+ end
41
+
42
+ # rubocop:disable Security/Eval
43
+ def req_options
44
+ return {} if redirect? || basic_auth?
45
+
46
+ begin
47
+ eval(reqs.split("#{action} ").last)
48
+ rescue StandardError
49
+ {}
50
+ end
51
+ end
52
+ # rubocop:enable Security/Eval
53
+
54
+ def to_h
55
+ {
56
+ name: name,
57
+ verb: verb,
58
+ path: path,
59
+ controller: controller,
60
+ action: action,
61
+ req_options: req_options
62
+ }
63
+ end
64
+
65
+ def to_json(*_args)
66
+ to_h.to_json
67
+ end
68
+
69
+ # rubocop:disable Layout/LineLength
70
+ def to_s
71
+ "<Route name: '#{name}', verb: '#{verb}', path: '#{path}', controller: '#{controller}', action: '#{action}' req_options: #{req_options}>"
72
+ end
73
+ # rubocop:enable Layout/LineLength
74
+
75
+ def inspect
76
+ "##{self}"
77
+ end
78
+
79
+ def redirect?
80
+ reqs.match?(/redirect(.*,|.*$)/)
81
+ end
82
+
83
+ def basic_auth?
84
+ reqs.start_with?('#<Rack::Auth::Basic')
85
+ end
86
+
87
+ def custom_router?
88
+ reqs.start_with?('#<')
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -3,7 +3,7 @@
3
3
  module Rails
4
4
  module Routes
5
5
  module Prettier
6
- VERSION = '0.1.0'
6
+ VERSION = '1.0.0'
7
7
  end
8
8
  end
9
9
  end
@@ -1,12 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'prettier/version'
4
+ require_relative 'prettier/objects/route'
5
+ require_relative 'prettier/formatter'
6
+ require 'rails'
4
7
 
5
8
  module Rails
6
9
  module Routes
7
10
  module Prettier
8
- class Error < StandardError; end
9
- # Your code goes here...
11
+ class << self
12
+ SEARCHABLE_FIELDS = %i[name verb path req_options controller action].freeze
13
+
14
+ def all
15
+ ActionDispatch::Routing::RoutesInspector.new(::Rails.application.routes.routes).format(
16
+ Rails::Routes::Prettier::Formatter.new, {}
17
+ )
18
+ end
19
+
20
+ SEARCHABLE_FIELDS.each do |field|
21
+ define_method :"find_by_#{field}" do |value|
22
+ all.select { |route| route.send(field) == value }
23
+ end
24
+ end
25
+ end
10
26
  end
11
27
  end
12
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-routes-prettier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nejdetkadir
@@ -44,6 +44,8 @@ files:
44
44
  - README.md
45
45
  - Rakefile
46
46
  - lib/rails/routes/prettier.rb
47
+ - lib/rails/routes/prettier/formatter.rb
48
+ - lib/rails/routes/prettier/objects/route.rb
47
49
  - lib/rails/routes/prettier/version.rb
48
50
  - rails-routes-prettier.gemspec
49
51
  - sig/rails/routes/prettier.rbs