Class: Apipie::MethodDescription

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

Defined Under Namespace

Classes: Api, ApisService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, resource, dsl_data) ⇒ MethodDescription

Returns a new instance of MethodDescription.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/apipie/method_description.rb', line 6

def initialize(method, resource, dsl_data)
  @method = method.to_s
  @resource = resource
  @from_concern = dsl_data[:from_concern]
  @apis = ApisService.new(resource, method, dsl_data).call

  @full_description = dsl_data[:description] || ''

  @errors = dsl_data[:errors].map do |args|
    Apipie::ErrorDescription.from_dsl_data(args)
  end

  @tag_list = dsl_data[:tag_list]

  @returns = dsl_data[:returns].map do |code,args|
    Apipie::ResponseDescription.from_dsl_data(self, code, args)
  end

  @see = dsl_data[:see].map do |args|
    Apipie::SeeDescription.new(args)
  end

  @formats = dsl_data[:formats]
  @examples = dsl_data[:examples]
  @examples += load_recorded_examples

  @metadata = dsl_data[:meta]

  @params_ordered = dsl_data[:params].map do |args|
    Apipie::ParamDescription.from_dsl_data(self, args)
  end.reject(&:response_only?)

  @params_ordered = ParamDescription.unify(@params_ordered)
  @headers = dsl_data[:headers]

  @show = if dsl_data.key? :show
    dsl_data[:show]
  else
    true
  end
end

Instance Attribute Details

#apisObject (readonly)

Returns the value of attribute apis.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def apis
  @apis
end

#examplesObject (readonly)

Returns the value of attribute examples.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def examples
  @examples
end

#formatsObject (readonly)

Returns the value of attribute formats.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def formats
  @formats
end

#full_descriptionObject (readonly)

Returns the value of attribute full_description.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def full_description
  @full_description
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def headers
  @headers
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'lib/apipie/method_description.rb', line 4

def 
  @metadata
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def method
  @method
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def resource
  @resource
end

#seeObject (readonly)

Returns the value of attribute see.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def see
  @see
end

#showObject (readonly)

Returns the value of attribute show.



3
4
5
# File 'lib/apipie/method_description.rb', line 3

def show
  @show
end

Instance Method Details

#create_api_url(api) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/apipie/method_description.rb', line 133

def create_api_url(api)
  path = api.path
  unless api.from_routes
    path = "#{@resource._api_base_url}#{path}"
  end
  path = path[0..-2] if path[-1..-1] == '/'
  return path
end

#doc_urlObject



125
126
127
128
129
130
131
# File 'lib/apipie/method_description.rb', line 125

def doc_url
  crumbs = []
  crumbs << @resource._version if Apipie.configuration.version_in_url
  crumbs << @resource._id
  crumbs << @method
  Apipie.full_url crumbs.join('/')
end

#errorsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/apipie/method_description.rb', line 104

def errors
  return @merged_errors if @merged_errors
  @merged_errors = []
  if @resource
    resource_errors = @resource._errors_args.map do |args|
      Apipie::ErrorDescription.from_dsl_data(args)
    end

    # exclude overwritten parent errors
    @merged_errors = resource_errors.find_all do |err|
      !@errors.any? { |e| e.code == err.code }
    end
  end
  @merged_errors.concat(@errors)
  return @merged_errors
end

#from_concern?Boolean

was the description defines in a module instead of directly in controller?

Returns:

  • (Boolean)


180
181
182
# File 'lib/apipie/method_description.rb', line 180

def from_concern?
  @from_concern
end

#idObject



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

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

#method_apis_to_json(lang = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/apipie/method_description.rb', line 142

def method_apis_to_json(lang = nil)
  @apis.each.collect do |api|
    {
      :api_url => create_api_url(api),
      :http_method => api.http_method.to_s,
      :short_description => Apipie.app.translate(api.short_description, lang),
      :deprecated => resource._deprecated || api.options[:deprecated]
    }
  end
end

#method_nameObject



184
185
186
# File 'lib/apipie/method_description.rb', line 184

def method_name
  @method
end

#paramsObject



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

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

#params_orderedObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/apipie/method_description.rb', line 60

def params_ordered
  all_params = []
  parent = Apipie.get_resource_description(@resource.controller.superclass)

  # get params from parent resource description
  [parent, @resource].compact.each do |resource|
    resource_params = resource._params_args.map do |args|
      Apipie::ParamDescription.from_dsl_data(self, args)
    end
    merge_params(all_params, resource_params)
  end

  merge_params(all_params, @params_ordered)
  all_params.find_all(&:validator)
end

#params_ordered_selfObject



56
57
58
# File 'lib/apipie/method_description.rb', line 56

def params_ordered_self
  @params_ordered
end

#returnsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/apipie/method_description.rb', line 89

def returns
  all_returns = []
  parent = Apipie.get_resource_description(@resource.controller.superclass)

  # get response descriptions from parent resource description
  [parent, @resource].compact.each do |resource|
    resource_returns = resource._returns_args.map do |code, args|
      Apipie::ResponseDescription.from_dsl_data(self, code, args)
    end
    merge_returns(all_returns, resource_returns)
  end

  merge_returns(all_returns, @returns)
end

#returns_selfObject



76
77
78
# File 'lib/apipie/method_description.rb', line 76

def returns_self
  @returns
end

#tag_listObject



80
81
82
83
84
85
86
87
# File 'lib/apipie/method_description.rb', line 80

def tag_list
  all_tag_list = []
  parent = Apipie.get_resource_description(@resource.controller.superclass)

  # get tags from parent resource description
  parent_tags = [parent, @resource].compact.flat_map(&:_tag_list_arg)
  Apipie::TagListDescription.new((parent_tags + @tag_list).uniq.compact)
end

#to_json(lang = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/apipie/method_description.rb', line 161

def to_json(lang = nil)
  {
    :doc_url => doc_url,
    :name => @method,
    :apis => method_apis_to_json(lang),
    :formats => formats,
    :full_description => Apipie.markup_to_html(Apipie.app.translate(@full_description, lang)),
    :errors => errors.map{ |error| error.to_json(lang) }.flatten,
    :params => params_ordered.map{ |param| param.to_json(lang) }.flatten,
    :returns => @returns.map{ |return_item| return_item.to_json(lang) }.flatten,
    :examples => @examples,
    :metadata => @metadata,
    :see => see.map(&:to_json),
    :headers => headers,
    :show => @show
  }
end

#versionObject



121
122
123
# File 'lib/apipie/method_description.rb', line 121

def version
  resource._version
end