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
44
45
46
# File 'lib/restapi/method_description.rb', line 26

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

  parent = @resource.controller.superclass
  if parent != ActionController::Base
    @parent_resource = parent.controller_name
  end
  @resource.add_method(id)
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

#seeObject (readonly)

Returns the value of attribute see.



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

def see
  @see
end

Instance Method Details

#doc_urlObject



73
74
75
# File 'lib/restapi/method_description.rb', line 73

def doc_url
  Restapi.full_url("#{@resource._id}/#{@method}")
end

#idObject



48
49
50
# File 'lib/restapi/method_description.rb', line 48

def id
  "#{resource._id}##{method}"
end

#method_apis_to_jsonObject



77
78
79
80
81
82
83
84
85
# File 'lib/restapi/method_description.rb', line 77

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



52
53
54
# File 'lib/restapi/method_description.rb', line 52

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

#params_orderedObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/restapi/method_description.rb', line 56

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

#see_urlObject



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

def see_url
  if @see
    method_description = Restapi[@see]
    if method_description.nil?
      raise ArgumentError.new("Method #{@see} referenced in 'see' does not exist.")
    end 
    method_description.doc_url
  end
end

#to_jsonObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/restapi/method_description.rb', line 101

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,
    :see => @see,
    :see_url => see_url
  }
end