Class: Api::Presenter::Hypermedia

Inherits:
Object
  • Object
show all
Defined in:
lib/api/presenter/hypermedia.rb

Class Method Summary collapse

Class Method Details



56
57
58
# File 'lib/api/presenter/hypermedia.rb', line 56

def build_links(resource, options = {})
  { "links" => resource.build_links(options) }
end

.present(resource) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/api/presenter/hypermedia.rb', line 5

def present(resource)
  # Initialize representation with links
  representation = build_links resource

  present_properties resource, representation

  representation
end

.present_entries_property(resource, representation, resource_properties) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/api/presenter/hypermedia.rb', line 37

def present_entries_property(resource, representation, resource_properties)
  entries_property = resource_properties.delete(:entries)

  representation[entries_property.to_s] = []

  resource.send(entries_property).each do |nested_resource|
    representation[entries_property.to_s] << build_links(nested_resource.to_resource, embed: true)
  end
end

.present_properties(resource, representation) ⇒ Object

Process basic information from the resource such as simlple fields(Dates, numbers, etc.) and also more complex ones that may be subject of expansion.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/api/presenter/hypermedia.rb', line 17

def present_properties(resource, representation)
  resource_properties = resource.class.properties.dup
  
  # entries property receives special treatment
  present_entries_property resource, representation, resource_properties if resource_properties.include? :entries
            
  # Now the other muggles
  resource_properties.each do |property_name|
    property_value = resource.send(property_name)
    
    if property_value.kind_of?(Resource) || property_value.respond_to?(:to_resource)
      # Resource like properties
      present_resoure_property property_name, property_value, resource, representation
    else
      # Non-Resource like properties
      representation[property_name.to_s] = property_value
    end
  end
end

.present_resoure_property(property_name, property_value, resource, representation) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/api/presenter/hypermedia.rb', line 47

def present_resoure_property(property_name, property_value, resource, representation)
  property_value = property_value.to_resource if property_value.respond_to? :to_resource
  
  representation["links"][property_name.to_s] = property_value.build_links(embed: resource.kind_of?(CollectionResource))

  # we only need the "self" contents
  representation["links"][property_name.to_s] = representation["links"][property_name.to_s]["self"] if representation["links"][property_name.to_s]["self"]                        
end