Class: Restapi::MethodDescription

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/method_description.rb

Defined Under Namespace

Classes: Api

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, resource, app) ⇒ MethodDescription

Returns a new instance of MethodDescription.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/restapi/method_description.rb', line 26

def initialize(method, resource, app)
  @method = method
  @resource = resource
  
  @apis = app.get_api_args
 
  desc = app.get_description || ''
  @full_description = Restapi.markup_to_html(desc)
  @errors = app.get_errors
  @params_ordered = app.get_params
  @examples = app.get_examples

  parent = @resource.controller.superclass
  if parent != ActionController::Base
    @parent_resource = parent.controller_name
  end
  @resource.add_method("#{resource._id}##{method}")
end

Instance Attribute Details

#apisObject (readonly)

Returns the value of attribute apis.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def apis
  @apis
end

#errorsObject (readonly)

Returns the value of attribute errors.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def errors
  @errors
end

#examplesObject (readonly)

Returns the value of attribute examples.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def examples
  @examples
end

#full_descriptionObject (readonly)

Returns the value of attribute full_description.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def full_description
  @full_description
end

#methodObject (readonly)

Returns the value of attribute method.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def method
  @method
end

#resourceObject (readonly)

Returns the value of attribute resource.



24
25
26
# File 'lib/restapi/method_description.rb', line 24

def resource
  @resource
end

Instance Method Details

#doc_urlObject



66
67
68
69
70
71
72
# File 'lib/restapi/method_description.rb', line 66

def doc_url
  [
    ENV["RAILS_RELATIVE_URL_ROOT"],
    Restapi.configuration.doc_base_url,
    "/#{@resource._id}/#{@method}"
  ].join
end

#method_apis_to_jsonObject



74
75
76
77
78
79
80
81
82
# File 'lib/restapi/method_description.rb', line 74

def method_apis_to_json
  @apis.each.collect do |api|
    {
      :api_url => api.api_url,
      :http_method => api.http_method.to_s,
      :short_description => api.short_description
    }
  end
end

#paramsObject



45
46
47
# File 'lib/restapi/method_description.rb', line 45

def params
  params_ordered.reduce({}) { |h,p| h[p.name] = p; h }
end

#params_orderedObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/restapi/method_description.rb', line 49

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

#to_jsonObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/restapi/method_description.rb', line 84

def to_json
  {
    :doc_url => doc_url,
    :name => @method,
    :apis => method_apis_to_json,
    :full_description => @full_description,
    :errors => @errors,
    :params => params_ordered.map(&:to_json).flatten,
    :examples => @examples
  }
end