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

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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/apipie/resource_description.rb', line 17

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

  @_methods = ActiveSupport::OrderedHash.new
  @_params_args = []
  @_errors_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

#_errors_argsObject (readonly)

Returns the value of attribute _errors_args.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _errors_args
  @_errors_args
end

#_formatsObject (readonly)

Returns the value of attribute _formats.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _formats
  @_formats
end

#_full_descriptionObject (readonly)

Returns the value of attribute _full_description.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _full_description
  @_full_description
end

#_idObject (readonly)

Returns the value of attribute _id.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _id
  @_id
end

#_methodsObject (readonly)

Returns the value of attribute _methods.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _methods
  @_methods
end

#_nameObject (readonly)

Returns the value of attribute _name.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _name
  @_name
end

#_params_argsObject (readonly)

Returns the value of attribute _params_args.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _params_args
  @_params_args
end

#_parentObject (readonly)

Returns the value of attribute _parent.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _parent
  @_parent
end

#_pathObject (readonly)

Returns the value of attribute _path.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _path
  @_path
end

#_short_descriptionObject (readonly)

Returns the value of attribute _short_description.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def _short_description
  @_short_description
end

#controllerObject (readonly)

Returns the value of attribute controller.



14
15
16
# File 'lib/apipie/resource_description.rb', line 14

def controller
  @controller
end

Instance Method Details

#_versionObject



49
50
51
# File 'lib/apipie/resource_description.rb', line 49

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

#add_method_description(method_description) ⇒ Object



53
54
55
56
# File 'lib/apipie/resource_description.rb', line 53

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



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

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

#doc_urlObject



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

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



58
59
60
# File 'lib/apipie/resource_description.rb', line 58

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

#method_descriptionsObject



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

def method_descriptions
  @_methods.values
end

#remove_method_description(method_name) ⇒ Object



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

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

#to_json(method_name = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/apipie/resource_description.rb', line 81

def to_json(method_name = nil)

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

  {
    :doc_url => doc_url,
    :api_url => api_url,
    :name => @_name,
    :short_description => @_short_description,
    :full_description => @_full_description,
    :version => _version,
    :formats => @_formats,
    :methods => methods
  }
end

#update_from_dsl_data(dsl_data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/apipie/resource_description.rb', line 32

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]

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