Sha256: 3f0e65df762541643c8c8e5b7f641efc4aab007cfca4f0e2559d8f9d9a4f2e66

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module ApiTaster
  class Route
    cattr_accessor :route_set
    cattr_accessor :inputs

    class << self
      def routes
        _routes = []

        route_set.routes.each_with_index do |route, index|
          next if route.verb.source.empty?

          _routes << {
            :id   => index,
            :name => route.name,
            :verb => route.verb.source.gsub(/[$^]/, ''),
            :path => route.path.spec.to_s.sub('(.:format)', ''),
            :reqs => route.requirements
          }
        end

        _routes
      end

      def grouped_routes
        routes.group_by { |r| r[:reqs][:controller] }
      end

      def find(id)
        routes.select { |r| r[:id] == id.to_i }[0]
      end

      def find_by_verb_and_path(verb, path)
        routes.select do |r|
          r[:path].to_s == path &&
          r[:verb].to_s.downcase == verb.to_s.downcase
        end[0]
      end

      def inputs_for(route)
        unless inputs.has_key?(route[:id])
          return { :undefined => route }
        end

        inputs[route[:id]].collect { |input| split_input(input, route) }
      end

      private

      def split_input(input, route)
        url_param_keys = route[:path].scan /:\w+/

        url_params  = input.select { |k| ":#{k}".in?(url_param_keys) }
        post_params = input.diff(url_params)

        {
          :url_params  => url_params,
          :post_params => post_params
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_taster-0.1.0 lib/api_taster/route.rb