Class: Calamum::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/calamum/resource.rb

Overview

This class represents a single resource. It contains attributes from parsed definition. So anywhere in view template we can use this object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Resource

Initialize object from attributes.

Parameters:

  • attrs (Hash)

    attributes to set



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/calamum/resource.rb', line 11

def initialize(attrs)
  @uri = attrs['uri']
  @action = attrs['action'].upcase
  @headers = attrs['headers'] || {}
  @auth = !attrs['authentication']
  @params = attrs['params'] || {}
  @errors = attrs['errors'] || {}
  @description = attrs['description']
  @response = attrs['response']
  @tryit = attrs['tryit']
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def action
  @action
end

#authObject

Returns the value of attribute auth.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def auth
  @auth
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def description
  @description
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def params
  @params
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def response
  @response
end

#tryitObject

Returns the value of attribute tryit.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def tryit
  @tryit
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/calamum/resource.rb', line 5

def uri
  @uri
end

Instance Method Details

#action_labelString

Returns a string representing a label css class.

Returns:

  • (String)

    css class



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/calamum/resource.rb', line 35

def action_label
  case @action
  when 'GET'
    'label-info'
  when 'POST'
    'label-success'
  when 'PUT'
    'label-warning'
  when 'DELETE'
    'label-important'
  end
end

#slugString

Returns a unique, but readable name for this resource suitable for use as a filename

Returns:

  • (String)

    resource filename



26
27
28
29
# File 'lib/calamum/resource.rb', line 26

def slug
  sanitized_uri = uri.gsub(/[^\w]/, '_').gsub('__', '_')
  "#{sanitized_uri}_#{action.downcase}_#{self.object_id}"
end

#to_sString

Returns a string representing a resource.

Returns:

  • (String)

    resource in a form (action: uri)



52
53
54
# File 'lib/calamum/resource.rb', line 52

def to_s
  "#{action}: #{uri}"
end