Class: Oat::Adapters::JsonAPI

Inherits:
Oat::Adapter show all
Defined in:
lib/oat/adapters/json_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ JsonAPI

Returns a new instance of JsonAPI.



7
8
9
10
11
12
# File 'lib/oat/adapters/json_api.rb', line 7

def initialize(*args)
  super
  @entities = {}
  @link_templates = {}
  @meta = {}
end

Instance Method Details

#collection(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/oat/adapters/json_api.rb', line 105

def collection(name, collection, serializer_class = nil, context_options = {}, &block)
  @treat_as_resource_collection = true
  data[:resource_collection] = [] unless data[:resource_collection].is_a?(Array)

  collection.each do |obj|
    ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
    data[:resource_collection] << ent.to_hash if ent
  end
end

#entities(name, collection, serializer_class = nil, context_options = {}, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/oat/adapters/json_api.rb', line 79

def entities(name, collection, serializer_class = nil, context_options = {}, &block)
  return if collection.nil? || collection.empty?
  _name = entity_name(name)
  link_name = pluralize(_name.to_s).to_sym
  data[:links][link_name] = []

  collection.each do |obj|
    entity_hash[link_name] ||= []
    ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
    if ent
      ent_hash = ent.to_hash
      data[:links][link_name] << ent_hash[:id]
      unless entity_hash[link_name].include? ent_hash
        entity_hash[link_name] << ent_hash
      end
    end
  end
end

#entity(name, obj, serializer_class = nil, context_options = {}, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oat/adapters/json_api.rb', line 64

def entity(name, obj, serializer_class = nil, context_options = {}, &block)
  ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
  if ent
    ent_hash = ent.to_hash
    _name = entity_name(name)
    link_name = pluralize(_name.to_s).to_sym
    data[:links][_name] = ent_hash[:id]

    entity_hash[link_name] ||= []
    unless entity_hash[link_name].include? ent_hash
      entity_hash[link_name] << ent_hash
    end
  end
end


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oat/adapters/json_api.rb', line 22

def link(rel, opts = {})
  templated = false
  if opts.is_a?(Hash)
    templated = opts.delete(:templated)
    if templated
      link_template(rel, opts[:href])
    else
      check_link_keys(opts)
    end
  end
  data[:links][rel] = opts unless templated
end

#meta(key, value) ⇒ Object



60
61
62
# File 'lib/oat/adapters/json_api.rb', line 60

def meta(key, value)
  @meta[key] = value
end

#properties(&block) ⇒ Object



52
53
54
# File 'lib/oat/adapters/json_api.rb', line 52

def properties(&block)
  data.merge! yield_props(&block)
end

#property(key, value) ⇒ Object



56
57
58
# File 'lib/oat/adapters/json_api.rb', line 56

def property(key, value)
  data[key] = value
end

#rel(rels) ⇒ Object



14
15
16
# File 'lib/oat/adapters/json_api.rb', line 14

def rel(rels)
  # no-op to maintain interface compatibility with the Siren adapter
end

#to_hashObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/oat/adapters/json_api.rb', line 115

def to_hash
  raise "JSON API entities MUST define a type. Use type 'user' in your serializers" unless root_name
  if serializer.top != serializer
    return data
  else
    h = {}
    if @treat_as_resource_collection
      h[root_name] = data[:resource_collection]
    else
      h[root_name] = [data]
    end
    h[:linked] = @entities if @entities.keys.any?
    h[:links] = @link_templates if @link_templates.keys.any?
    h[:meta] = @meta if @meta.keys.any?
    return h
  end
end

#type(*types) ⇒ Object



18
19
20
# File 'lib/oat/adapters/json_api.rb', line 18

def type(*types)
  @root_name = pluralize(types.first.to_s).to_sym
end