Class: Apipie::RoutesFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/routes_formatter.rb

Constant Summary collapse

API_METHODS =
%w{GET POST PUT PATCH OPTIONS DELETE}

Instance Method Summary collapse

Instance Method Details

#format_path(rails_route) ⇒ Object



18
19
20
# File 'lib/apipie/routes_formatter.rb', line 18

def format_path(rails_route)
  rails_route.path.spec.to_s.gsub('(.:format)', '')
end

#format_route(rails_route, args) ⇒ Object



11
12
13
14
15
16
# File 'lib/apipie/routes_formatter.rb', line 11

def format_route(rails_route, args)
  { :path => format_path(rails_route),
    :verb => format_verb(rails_route),
    :desc => args[:desc],
    :options => args[:options] }
end

#format_routes(rails_routes, args) ⇒ Object

The entry method called by Apipie to extract the array representing the api dsl from the routes definition.



7
8
9
# File 'lib/apipie/routes_formatter.rb', line 7

def format_routes(rails_routes, args)
  rails_routes.map { |rails_route| format_route(rails_route, args) }
end

#format_verb(rails_route) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/apipie/routes_formatter.rb', line 22

def format_verb(rails_route)
  verb = API_METHODS.select{|defined_verb| defined_verb =~ /\A#{rails_route.verb}\z/}
  if verb.count != 1
    verb = API_METHODS.select{|defined_verb| defined_verb == rails_route.constraints[:method]}
    if verb.blank?
      raise "Unknow verb #{rails_route.path.spec.to_s}"
    end
  end
  verb.first
end