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.


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

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

Instance Method Details

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


113
114
115
116
117
118
119
120
121
# File 'lib/oat/adapters/json_api.rb', line 113

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


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/oat/adapters/json_api.rb', line 87

def entities(name, collection, serializer_class = nil, context_options = {}, &block)
  return if collection.nil? || collection.empty?
  _name = entity_name(name)
  link_name = _name.to_s.pluralize.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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/oat/adapters/json_api.rb', line 72

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 = _name.to_s.pluralize.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

30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oat/adapters/json_api.rb', line 30

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


68
69
70
# File 'lib/oat/adapters/json_api.rb', line 68

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

#properties(&block) ⇒ Object


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

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

#property(key, value) ⇒ Object


64
65
66
# File 'lib/oat/adapters/json_api.rb', line 64

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

#rel(rels) ⇒ Object


22
23
24
# File 'lib/oat/adapters/json_api.rb', line 22

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

#to_hashObject


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/oat/adapters/json_api.rb', line 123

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


26
27
28
# File 'lib/oat/adapters/json_api.rb', line 26

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