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.



46
47
48
# File 'lib/locomotive/misc/api_documentation.rb', line 46

def initialize
  @resources = []
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



44
45
46
# File 'lib/locomotive/misc/api_documentation.rb', line 44

def resources
  @resources
end

Instance Method Details

#parseObject

Take all the routes and build the set of RESTFUL resources



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/locomotive/misc/api_documentation.rb', line 52

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_attributes(resource[:actions][action])
  end

  self.resources
end