Class: Locomotive::Misc::ApiDocumentation::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/misc/api_documentation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



31
32
33
# File 'lib/locomotive/misc/api_documentation.rb', line 31

def initialize
  @resources = []
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



29
30
31
# File 'lib/locomotive/misc/api_documentation.rb', line 29

def resources
  @resources
end

Instance Method Details

#parseObject

Take all the routes and build the set of RESTFUL resources



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/locomotive/misc/api_documentation.rb', line 37

def parse
  routes = Rails.application.routes.routes.each do |route|
    next unless route.path.spec.to_s =~ %r{^/locomotive/api/}

    name        = route.defaults[:controller].split('/').last
    resource    = resources.find { |r| r[:name] == name }
    controller  = "#{route.defaults[:controller]}_controller".camelize.constantize
    action      = route.defaults[:action].to_sym
    verb        = route.verb.inspect.gsub("\/^", '').gsub("$\/", '')

    if resource.nil?
      resource = controller.description
      resource[:name] = name
      self.resources << resource
    end

    resource[:actions][action] ||= {}
    resource[:actions][action].merge!(path: route.path.spec.to_s, verb: verb)

    self.sanitize_params(resource[:actions][action])
  end

  self.resources
end