Class: Grape::JSONAPI::Streamer

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/json_api/version.rb,
lib/grape/json_api/streamer.rb

Constant Summary collapse

VERSION =
'0.0.3'

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Streamer

Returns a new instance of Streamer.



6
7
8
# File 'lib/grape/json_api/streamer.rb', line 6

def initialize(collection)
  @collection = collection
end

Instance Method Details

#each {|'{"data":['| ... } ⇒ Object

Yields:

  • ('{"data":[')


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/grape/json_api/streamer.rb', line 10

def each
  yield '{"data":['
  first = true
  @collection.lazy.each do |object|
    buffer = ''
    buffer << ',' unless first
    first = false
    data = serialize(object)
    buffer << JSON.unparse(data)[8..-2].strip
    yield buffer
  end
  yield ']}'
end

#serialize(model) ⇒ Object



24
25
26
# File 'lib/grape/json_api/streamer.rb', line 24

def serialize(model)
  ::JSONAPI::Serializer.serialize(model, is_collection: false)
end