Class: Routes2spec::RequestSpecFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/routes2spec/request_spec_formatter.rb

Overview

Routes2spec::RequestSpecFormatter class

Constant Summary collapse

SUPPORTED_VERBS =
%w[get post patch put delete].freeze
STATUS =
{
  get: 200,
  post: 201,
  patch: 200,
  put: 200,
  delete: 204,
}.tap{ _1.default = 200 }.freeze
SYMBOL_STATUS =
{
  get: ":ok",
  post: ":created",
  patch: ":ok",
  put: ":ok",
  delete: ":no_content",
}.tap{ _1.default = ":ok" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RequestSpecFormatter

Returns a new instance of RequestSpecFormatter.



24
25
26
27
# File 'lib/routes2spec/request_spec_formatter.rb', line 24

def initialize(opts = {})
  @results = []
  @opts = opts
end

Instance Method Details

#header(routes) ⇒ Object



112
113
# File 'lib/routes2spec/request_spec_formatter.rb', line 112

def header(routes)
end

#no_routesObject



115
116
# File 'lib/routes2spec/request_spec_formatter.rb', line 115

def no_routes(*)
end

#resultObject



29
30
31
# File 'lib/routes2spec/request_spec_formatter.rb', line 29

def result
  @results.flatten
end

#section(routes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/routes2spec/request_spec_formatter.rb', line 36

def section(routes)
  routes = routes.select do |r|
    is_valid = r[:reqs].include?("#") && !r[:reqs].start_with?("#<")
    Routes2spec.log "Skip. Unsupported reqs! `#{r[:reqs]}`" unless is_valid
    is_valid
  end
  grouped = routes.group_by { |r| r[:reqs].split("#").first }
  Routes2spec.log_debug "grouped: #{grouped}"
  @results << grouped.map do |controller, grouped_routes|
    # TODO: support redirect. ex: {"redirect(301, https://example.com)"=>[{:name=>"example", :verb=>"GET", :path=>"/example(.:format)", :reqs=>"redirect(301, https://example.com)"}]}

    namespaces = controller.split("/")
    controller_name = namespaces.map(&:camelize).join("::")
    group_name = namespaces.pop
    Routes2spec.log_debug "namespaces: #{namespaces}, group_name: #{group_name}, controller_name: #{controller_name}"
    unless group_name
      Routes2spec.log "Skip. Invalid controller format! `#{controller}`"
      next
    end
    routes = grouped_routes.map do |r|
      verb = r[:verb]&.downcase # GET|POST
      path = r[:path].gsub("(.:format)", "")
      param_names = path.scan %r{(?<=/:).+?(?=/|\z)}
      params_str = param_names.map{|name| "#{name}: \"#{name}\"" }.join(", ")
      path_name = r[:name] || ""
      path_name = grouped_routes.find{ _1[:path] == r[:path] && !_1[:name].empty? }&.fetch(:name) || "" if path_name.empty?
      Routes2spec.log_debug "verb: #{verb}, path: #{path}, path_name: #{path_name}, @opts: #{@opts}"
      if path_name.empty?
        Routes2spec.log "Skip. No path name! `#{verb&.upcase} #{path}`"
        next
      end
      unless SUPPORTED_VERBS.include?(verb)
        Routes2spec.log "Skip. Unsupported verb! `#{verb&.upcase} #{path}`"
        next
      end
      if !@opts[:verb].nil? && @opts[:verb].downcase != verb
        Routes2spec.log "Skip. Not matched specified verb(#{@opts[:verb].upcase})! `#{verb&.upcase} #{path}`"
        next
      end
      endpoint, constraints = r[:reqs].split(" ")
      Routes2spec.log_debug "endpoint: #{endpoint}, constraints: #{constraints}"
      # TODO: insert constraints to routing spec
      status = @opts[:symbol_status] ? SYMBOL_STATUS[verb.to_sym] : STATUS[verb.to_sym]
      use_literal_path = @opts[:literal_path] || false
      path_helper_str = "#{path_name}_path#{params_str.empty? ? "" : "(#{params_str})"}"
      literal_path_str = "\"#{path}\""
      path_str = use_literal_path ? literal_path_str : path_helper_str
      r.merge(
        path: path,
        path_str: path_str,
        path_name: path_name,
        params_str: params_str,
        reqs: r[:reqs],
        endpoint: endpoint,
        constraints: constraints || "",
        status: status,
      )
    end.compact
    if routes.empty?
      Routes2spec.log "Skip. Empty routes! `#{controller}`"
      next
    end
    pending = @opts[:pending] || false
    template_path = File.expand_path(File.join(File.dirname(__FILE__), "templates/request_spec.rb.erb"))
    content = ERB.new(File.read(template_path)).result(binding)
    routing_template_path = File.expand_path(File.join(File.dirname(__FILE__), "templates/routing_spec.rb.erb"))
    routing_content = ERB.new(File.read(routing_template_path)).result(binding)
    {
      name: group_name,
      namespaces: namespaces,
      content: content,
      routing_content: routing_content,
    }
  end.compact
end

#section_title(title) ⇒ Object



33
34
# File 'lib/routes2spec/request_spec_formatter.rb', line 33

def section_title(title)
end