Sha256: 491bdafec06a3f708c637dedfcc5c945f929eda9f5ee9982b9ac09efb1b4eb8a

Contents?: true

Size: 1.03 KB

Versions: 23

Compression:

Stored size: 1.03 KB

Contents

module Apipie
  class RoutesFormatter
    API_METHODS = %w{GET POST PUT PATCH OPTIONS DELETE}

    # The entry method called by Apipie to extract the array
    # representing the api dsl from the routes definition.
    def format_routes(rails_routes, args)
      rails_routes.map { |rails_route| format_route(rails_route, args) }
    end

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

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

    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
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
apipie-rails-1.3.0 lib/apipie/routes_formatter.rb
apipie-rails-1.2.3 lib/apipie/routes_formatter.rb
apipie-rails-1.2.2 lib/apipie/routes_formatter.rb
apipie-rails-1.2.1 lib/apipie/routes_formatter.rb
apipie-rails-1.2.0 lib/apipie/routes_formatter.rb
apipie-rails-1.1.0 lib/apipie/routes_formatter.rb
apipie-rails-1.0.0 lib/apipie/routes_formatter.rb
apipie-rails-0.9.4 lib/apipie/routes_formatter.rb
apipie-rails-0.9.3 lib/apipie/routes_formatter.rb
apipie-rails-0.9.2 lib/apipie/routes_formatter.rb
apipie-rails-0.9.1 lib/apipie/routes_formatter.rb
apipie-rails-0.9.0 lib/apipie/routes_formatter.rb
apipie-rails-0.8.2 lib/apipie/routes_formatter.rb
apipie-rails-0.8.1 lib/apipie/routes_formatter.rb
apipie-rails-0.8.0 lib/apipie/routes_formatter.rb
apipie-rails-0.7.2 lib/apipie/routes_formatter.rb
apipie-rails-0.7.1 lib/apipie/routes_formatter.rb
apipie-rails-0.7.0 lib/apipie/routes_formatter.rb
apipie-rails-0.6.0 lib/apipie/routes_formatter.rb
apipie-rails-0.5.20 lib/apipie/routes_formatter.rb