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

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.



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

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.



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

def _errors_args
  @_errors_args
end

#_formatsObject (readonly)

Returns the value of attribute _formats.



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

def _formats
  @_formats
end

#_full_descriptionObject (readonly)

Returns the value of attribute _full_description.



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

def _full_description
  @_full_description
end

#_headersObject (readonly)

Returns the value of attribute _headers.



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

def _headers
  @_headers
end

#_idObject (readonly)

Returns the value of attribute _id.



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

def _id
  @_id
end

#_metadataObject (readonly)

Returns the value of attribute _metadata.



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

def 
  @_metadata
end

#_methodsObject (readonly)

Returns the value of attribute _methods.



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

def _methods
  @_methods
end

#_nameObject (readonly)

Returns the value of attribute _name.



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

def _name
  @_name
end

#_params_argsObject (readonly)

Returns the value of attribute _params_args.



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

def _params_args
  @_params_args
end

#_parentObject (readonly)

Returns the value of attribute _parent.



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

def _parent
  @_parent
end

#_pathObject (readonly)

Returns the value of attribute _path.



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

def _path
  @_path
end

#_short_descriptionObject (readonly)

Returns the value of attribute _short_description.



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

def _short_description
  @_short_description
end

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

Instance Method Details

#_api_base_urlObject



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

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

#_versionObject



51
52
53
# File 'lib/apipie/resource_description.rb', line 51

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

#add_method_description(method_description) ⇒ Object



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

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



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

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

#doc_urlObject



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

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



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

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

#method_descriptionsObject



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

def method_descriptions
  @_methods.values
end

#remove_method_description(method_name) ⇒ Object



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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/apipie/resource_description.rb', line 91

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
  }
end

#update_from_dsl_data(dsl_data) ⇒ Object



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

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]
  @_metadata = dsl_data[:meta]
  @_api_base_url = dsl_data[:api_base_url]
  @_headers = dsl_data[:headers]

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

#valid_method_name?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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