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 - resource 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, id, dsl_data = nil, version = nil) ⇒ 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, id, dsl_data = nil, version = nil)
  @_methods = ActiveSupport::OrderedHash.new
  @_params_args = []
  @_errors_args = []
  @_returns_args = []

  @controller = controller
  @_id = id
  @_version = version || Apipie.configuration.default_version
  @_parent = Apipie.value_from_parents(controller.superclass, version) do |parent, ver|
    Apipie.get_resource_description(parent, ver)
  end

  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

#_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



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

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

#api_urlObject



104
105
106
# File 'lib/apipie/resource_description.rb', line 104

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

#doc_urlObject



97
98
99
100
101
102
# File 'lib/apipie/resource_description.rb', line 97

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



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

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

#method_descriptionsObject



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

def method_descriptions
  @_methods.values
end

#nameObject Also known as: _name



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/apipie/resource_description.rb', line 64

def name
  @name ||= case resource_name
            when Proc
      resource_name.call(controller)
            when Symbol
      controller.public_send(resource_name)
            when String
      resource_name
    else
      default_name
    end
end

#remove_method_description(method_name) ⇒ Object



87
88
89
90
91
# File 'lib/apipie/resource_description.rb', line 87

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

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/apipie/resource_description.rb', line 112

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,
    :id => _id,
    :api_url => api_url,
    :name => name,
    :short_description => Apipie.app.translate(@_short_description, lang),
    :full_description => Apipie.markup_to_html(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)
  @_resource_name = dsl_data[:resource_name] if dsl_data[:resource_name]
  @_full_description = 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)


108
109
110
# File 'lib/apipie/resource_description.rb', line 108

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