lib/restapi/method_description.rb in restapi-0.0.2 vs lib/restapi/method_description.rb in restapi-0.0.3
- old
+ new
@@ -1,38 +1,104 @@
+require 'set'
module Restapi
-
+
class MethodDescription
+
+ class Api
+
+ attr_accessor :short_description, :api_url, :http_method
+
+ def initialize(args)
+ @http_method = args[:method] || args[:http_method] || args[:http]
+ @short_description = args[:desc] || args[:short] || args[:description]
+ @api_url = create_api_url(args[:path] || args[:url])
+ end
+
+ private
+
+ def create_api_url(path)
+ "#{Restapi.configuration.api_base_url}#{path}"
+ end
+
+ end
+
+ attr_reader :errors, :full_description, :method, :resource, :apis
- attr_reader :errors, :params, :full_description, :method, :resource, :short_description, :path, :http
-
def initialize(method, resource, app)
@method = method
@resource = resource
- args = app.get_api_args
- @short_description = args[:desc] || args[:short] || args[:description]
- @path = args[:path]
- @http = args[:method]
+
+ @apis = app.get_api_args
+
desc = app.get_description || ''
- @full_description = Restapi.rdoc.convert(desc.strip_heredoc)
+ @full_description = Restapi.markup_to_html(desc)
@errors = app.get_errors
- @params = app.get_params
+ @params_ordered = app.get_params
+
+ parent = @resource.controller.superclass
+ if parent != ActionController::Base
+ @parent_resource = parent.controller_name
+ end
+ @resource.add_method("#{resource._id}##{method}")
end
+
+ def params
+ params_ordered.reduce({}) { |h,p| h[p.name] = p; h }
+ end
+
+ def params_ordered
+ all_params = []
+ # get params from parent resource description
+ if @parent_resource
+ parent = Restapi.get_resource_description(@parent_resource)
+ merge_params(all_params, parent._params_ordered) if parent
+ end
+
+ # get params from actual resource description
+ if @resource
+ merge_params(all_params, resource._params_ordered)
+ end
+
+ merge_params(all_params, @params_ordered)
+ all_params.find_all(&:validator)
+ end
- def doc_url; "#{Restapi.configuration.doc_base_url}/#{@resource}/#{@method}"; end
- def api_url; "#{Restapi.configuration.api_base_url}#{@path}"; end
+ def doc_url
+ [
+ ENV["RAILS_RELATIVE_URL_ROOT"],
+ Restapi.configuration.doc_base_url,
+ "##{@resource._id}/#{@method}"
+ ].join
+ end
+ def method_apis_to_json
+ @apis.each.collect do |api|
+ {
+ :api_url => api.api_url,
+ :http_method => api.http_method,
+ :short_description => api.short_description
+ }
+ end
+ end
+
def to_json
{
:doc_url => doc_url,
- :api_url => api_url,
:name => @method,
- :http_method => @http,
- :short_description => @short_description,
+ :apis => method_apis_to_json,
:full_description => @full_description,
:errors => @errors,
- :params => @params.collect { |_,v| v.to_json }
+ :params => params_ordered.map(&:to_json).flatten
}
end
+ private
+
+ def merge_params(params, new_params)
+ new_param_names = Set.new(new_params.map(&:name))
+ params.delete_if { |p| new_param_names.include?(p.name) }
+ params.concat(new_params)
+ end
+
end
-end
\ No newline at end of file
+end