Sha256: e1531b9165ffc6f545b64bf1f561c0297281d2eb0d494ccf2fe5a45dcdca2a0f

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module GrapeApiary
  class Resource
    attr_reader :key, :name, :routes, :sample_generator

    def initialize(key, routes)
      @key              = key
      @name             = key.humanize
      @routes           = routes
      @sample_generator = SampleGenerator.new(self)
    end

    def title
      @title ||= name.titleize
    end

    def namespaced
      @namespaced ||= routes.group_by(&:route_namespace).map do |_, routes|
        Resource.new(name, routes)
      end
    end

    def paths
      @paths ||= routes.group_by(&:route_path_without_format).map do |n, routes|
        Resource.new(name, routes)
      end
    end

    def header
      # TODO: ???
      route = routes.first

      "#{title} #{route.route_type} [#{route.route_path_without_format}]"
    end

    def sample_request
      sample_generator.request
    end

    def sample_response
      sample_generator.response
    end

    def unique_params
      # TODO: this is a hack, assuming that the resource has a POST or PUT
      # route that defines all of the parameters that would define the resource
      methods = %w(POST PUT)

      potential = routes.select do |route|
        methods.include?(route.route_method) && route.route_params.present?
      end

      if potential.present?
        potential.first.route_params
      else
        []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-apiary-0.0.4 lib/grape-apiary/resource.rb