Class: Yaks::Format::CollectionJson

Inherits:
Yaks::Format show all
Includes:
Yaks::FP
Defined in:
lib/yaks/format/collection_json.rb

Instance Attribute Summary

Attributes inherited from Yaks::Format

#env

Instance Method Summary collapse

Methods inherited from Yaks::Format

all, by_accept_header, by_media_type, by_name, #call, #initialize, media_types, names, register

Methods included from Util::Deprecated

#deprecated_alias

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Methods included from Yaks::FP::Callable

#to_proc

Constructor Details

This class inherits a constructor from Yaks::Format

Instance Method Details

#links?(resource) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/yaks/format/collection_json.rb', line 69

def links?(resource)
  resource.collection? && resource.links.any?
end

#queries?(resource) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/yaks/format/collection_json.rb', line 65

def queries?(resource)
  resource.forms.any? { |f| form_is_query? f }
end

#serialize_items(resource) ⇒ Array

Parameters:

Returns:

  • (Array)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yaks/format/collection_json.rb', line 24

def serialize_items(resource)
  resource.seq.map do |item|
    attrs = item.attributes.map do |name, value|
      {
        name: name,
        value: value
      }
    end
    result = {data: attrs}
    result[:href] = item.self_link.uri if item.self_link
    item.links.each do |link|
      next if link.rel.equal? :self
      result[:links] = [] unless result.key?(:links)
      result[:links] << {rel: link.rel, href: link.uri}
      result[:links].last[:name] = link.title if link.title
    end
    result
  end
end


44
45
46
47
48
# File 'lib/yaks/format/collection_json.rb', line 44

def serialize_links(resource)
  resource.links.each_with_object([]) do |link, result|
    result << {href: link.uri, rel: link.rel}
  end
end

#serialize_queries(resource) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yaks/format/collection_json.rb', line 50

def serialize_queries(resource)
  resource.forms.each_with_object([]) do |form, result|
    next unless form_is_query? form

    result << {rel: form.name, href: form.action}
    result.last[:prompt] = form.title if form.title

    form.fields_flat.each do |field|
      result.last[:data] = [] unless result.last.key? :data
      result.last[:data] << {name: field.name, value: nil.to_s}
      result.last[:data].last[:prompt] = field.label if field.label
    end
  end
end

#serialize_resource(resource) ⇒ Hash

Parameters:

Returns:

  • (Hash)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yaks/format/collection_json.rb', line 10

def serialize_resource(resource)
  result = {
    version: "1.0",
    items: serialize_items(resource)
  }
  result[:href] = resource.self_link.uri if resource.self_link
  result[:links] = serialize_links(resource) if links?(resource)
  result[:queries] = serialize_queries(resource) if queries?(resource)
  result[:template] = serialize_template(resource) if template?(resource)
  {collection: result}
end

#template?(resource) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/yaks/format/collection_json.rb', line 73

def template?(resource)
  options.key?(:template) && template_form_exists?(resource)
end