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.



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

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

Instance Method Details

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



59
60
61
62
63
64
65
66
67
# File 'lib/oat/adapters/json_api.rb', line 59

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 if ent
  end
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oat/adapters/json_api.rb', line 44

def entities(name, collection, serializer_class = nil, context_options = {}, &block)
  return if collection.nil? || collection.empty?
  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
      data[:links][link_name] << ent[:id]
      entity_hash[link_name] << ent
    end
  end
end

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



35
36
37
38
39
40
41
42
# File 'lib/oat/adapters/json_api.rb', line 35

def entity(name, obj, serializer_class = nil, context_options = {}, &block)
  ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
  if ent
    entity_hash[name.to_s.pluralize.to_sym] ||= []
    link name, :href => ent[:id]
    entity_hash[name.to_s.pluralize.to_sym] << ent
  end
end


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

def link(rel, opts = {})
  data[:links][rel] = opts[:href]
end

#properties(&block) ⇒ Object



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

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

#property(key, value) ⇒ Object



31
32
33
# File 'lib/oat/adapters/json_api.rb', line 31

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

#to_hashObject



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

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?
    return h
  end
end

#type(*types) ⇒ Object



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

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