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.5'

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Streamer

Returns a new instance of Streamer.



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

def initialize(collection, options = {})
  @collection = collection
  @options    = ActiveSupport::HashWithIndifferentAccess.new(options)
end

Instance Method Details

#each {|'{'| ... } ⇒ Object

Yields:

  • ('{')


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

def each
  yield '{'
  yield "\"meta\": #{JSON.unparse(@options[:meta])}," if @options[:meta]
  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 ']'
  yield ",\"links\": #{JSON.unparse(@options[:links])}" if @options[:links]
  yield '}'
end

#serialize(model) ⇒ Object



29
30
31
# File 'lib/grape/json_api/streamer.rb', line 29

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