class MakeRestful::Specification::Builder attr_reader :_controller_, :_methods_, :_description_, :_formats_, :_attributes_, :_resource_, :_default_methods_, :_default_attributes_, :_default_formats_ private def initialize(controller, &block) @_controller_ = controller @_methods_ = [] @_default_methods_ = default_supported_methods @_methods_ = [] @_attributes_ = [] @_default_attributes_ = controller.resource_class.new.attributes.keys @_default_formats_ = controller.allowed_formats @_formats_ = [] @_description_ = "Api for #{controller.resource_name.titleize}" @_resource_ = controller.instance_name instance_eval(&block) if block_given? end def method(method, options={}) method = { method: method, path: options[:path], returns: options[:returns] } @_methods_ << method method end def description(string) @_description_ = string end def resource(string) @_resource_ = string end def attribute(attr) @_attributes_ << attr attr end def attributes(*args) @_attributes_ += args.flatten end def format(attr) @_formats_ << attr attr end def formats(*args) @_formats_ += args end def default_supported_methods method :get, path: "/", returns: "collection" if _controller_.allow_method?(:list) || _controller_.allow_method?(:index) method :post, path: "/", returns: "resource" if _controller_.allow_method?(:create) || _controller_.allow_method?(:post) method :get, path: "/:id", returns: "resource" if _controller_.allow_method?(:show) || _controller_.allow_method?(:get) method :put, path: "/:id", returns: "resource" if _controller_.allow_method?(:update) || _controller_.allow_method?(:put) method :delete, path: "/:id", returns: "success" if _controller_.allow_method?(:destroy) || _controller_.allow_method?(:delete) end def render { resource: _resource_, formats: _formats_.present? ? _formats_ : _default_formats_, description: _description_, attributes: _attributes_.present? ? _attributes_ : _default_attributes_, methods: _methods_.present? ? _methods_ : _default_methods_ } end end