Class: Apipie::ResourceDescription

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

Overview

Resource description

version - api version (1) description path - relative path (/api/articles) methods - array of keys to Apipie.method_descriptions (array of Apipie::MethodDescription) name - human readable alias of resource (Articles) id - resouce name formats - acceptable request/response format types headers - array of headers deprecated - boolean indicating if resource is deprecated

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, resource_name, dsl_data = nil, version = nil, &block) ⇒ ResourceDescription

Returns a new instance of ResourceDescription.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/apipie/resource_description.rb', line 20

def initialize(controller, resource_name, dsl_data = nil, version = nil, &block)

  @_methods = ActiveSupport::OrderedHash.new
  @_params_args = []
  @_errors_args = []
  @_returns_args = []

  @controller = controller
  @_id = resource_name
  @_version = version || Apipie.configuration.default_version
  @_name = @_id.humanize
  @_parent = Apipie.get_resource_description(controller.superclass, version)

  update_from_dsl_data(dsl_data) if dsl_data
end

Instance Attribute Details

#_deprecatedObject (readonly)

Returns the value of attribute _deprecated.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _deprecated
  @_deprecated
end

#_errors_argsObject (readonly)

Returns the value of attribute _errors_args.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _errors_args
  @_errors_args
end

#_formatsObject (readonly)

Returns the value of attribute _formats.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _formats
  @_formats
end

#_full_descriptionObject (readonly)

Returns the value of attribute _full_description.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _full_description
  @_full_description
end

#_headersObject (readonly)

Returns the value of attribute _headers.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _headers
  @_headers
end

#_idObject (readonly)

Returns the value of attribute _id.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _id
  @_id
end

#_metadataObject (readonly)

Returns the value of attribute _metadata.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def 
  @_metadata
end

#_methodsObject (readonly)

Returns the value of attribute _methods.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _methods
  @_methods
end

#_nameObject (readonly)

Returns the value of attribute _name.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _name
  @_name
end

#_params_argsObject (readonly)

Returns the value of attribute _params_args.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _params_args
  @_params_args
end

#_parentObject (readonly)

Returns the value of attribute _parent.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _parent
  @_parent
end

#_pathObject (readonly)

Returns the value of attribute _path.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _path
  @_path
end

#_returns_argsObject (readonly)

Returns the value of attribute _returns_args.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _returns_args
  @_returns_args
end

#_short_descriptionObject (readonly)

Returns the value of attribute _short_description.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _short_description
  @_short_description
end

#_tag_list_argObject (readonly)

Returns the value of attribute _tag_list_arg.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def _tag_list_arg
  @_tag_list_arg
end

#controllerObject (readonly)

Returns the value of attribute controller.



16
17
18
# File 'lib/apipie/resource_description.rb', line 16

def controller
  @controller
end

Instance Method Details

#_api_base_urlObject



60
61
62
# File 'lib/apipie/resource_description.rb', line 60

def _api_base_url
  @_api_base_url || @_parent.try(:_api_base_url) || Apipie.api_base_url(_version)
end

#_versionObject



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

def _version
  @_version || @_parent.try(:_version) || Apipie.configuration.default_version
end

#add_method_description(method_description) ⇒ Object



64
65
66
67
# File 'lib/apipie/resource_description.rb', line 64

def add_method_description(method_description)
  Apipie.debug "@resource_descriptions[#{self._version}][#{self._name}]._methods[#{method_description.method}] = #{method_description}"
  @_methods[method_description.method.to_sym] = method_description
end

#api_urlObject



90
# File 'lib/apipie/resource_description.rb', line 90

def api_url; "#{Apipie.api_base_url(_version)}#{@_path}"; end

#doc_urlObject



83
84
85
86
87
88
# File 'lib/apipie/resource_description.rb', line 83

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

#method_description(method_name) ⇒ Object



69
70
71
# File 'lib/apipie/resource_description.rb', line 69

def method_description(method_name)
  @_methods[method_name.to_sym]
end

#method_descriptionsObject



79
80
81
# File 'lib/apipie/resource_description.rb', line 79

def method_descriptions
  @_methods.values
end

#remove_method_description(method_name) ⇒ Object



73
74
75
76
77
# File 'lib/apipie/resource_description.rb', line 73

def remove_method_description(method_name)
  if @_methods.has_key?(method_name)
    @_methods.delete(method_name)
  end
end

#to_json(method_name = nil, lang = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/apipie/resource_description.rb', line 96

def to_json(method_name = nil, lang = nil)
  if method_name && !valid_method_name?(method_name)
    raise "Method #{method_name} not found for resource #{_name}"
  end

  methods = if method_name.blank?
    @_methods.collect { |key, method_description| method_description.to_json(lang) }
  else
    [@_methods[method_name.to_sym].to_json(lang)]
  end

  {
    :doc_url => doc_url,
    :api_url => api_url,
    :name => @_name,
    :short_description => Apipie.app.translate(@_short_description, lang),
    :full_description => Apipie.app.translate(@_full_description, lang),
    :version => _version,
    :formats => @_formats,
    :metadata => @_metadata,
    :methods => methods,
    :headers => _headers,
    :deprecated => @_deprecated
  }
end

#update_from_dsl_data(dsl_data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/apipie/resource_description.rb', line 36

def update_from_dsl_data(dsl_data)
  @_name = dsl_data[:resource_name] if dsl_data[:resource_name]
  @_full_description = Apipie.markup_to_html(dsl_data[:description])
  @_short_description = dsl_data[:short_description]
  @_path = dsl_data[:path] || ""
  @_formats = dsl_data[:formats]
  @_errors_args = dsl_data[:errors]
  @_params_args = dsl_data[:params]
  @_returns_args = dsl_data[:returns]
  @_tag_list_arg = dsl_data[:tag_list]
  @_metadata = dsl_data[:meta]
  @_api_base_url = dsl_data[:api_base_url]
  @_headers = dsl_data[:headers]
  @_deprecated = dsl_data[:deprecated] || false

  if dsl_data[:app_info]
    Apipie.configuration.app_info[_version] = dsl_data[:app_info]
  end
end

#valid_method_name?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/apipie/resource_description.rb', line 92

def valid_method_name?(method_name)
  @_methods.keys.map(&:to_s).include?(method_name.to_s)
end