Class: Grape::JSONAPI::Streamer
- Inherits:
-
Object
- Object
- Grape::JSONAPI::Streamer
- Defined in:
- lib/grape/json_api/version.rb,
lib/grape/json_api/streamer.rb
Constant Summary collapse
- VERSION =
'0.0.6'
Instance Method Summary collapse
- #each {|'{'| ... } ⇒ Object
-
#initialize(collection, options = {}) ⇒ Streamer
constructor
A new instance of Streamer.
- #serialize(model) ⇒ Object
Constructor Details
#initialize(collection, options = {}) ⇒ Streamer
Returns a new instance of Streamer.
6 7 8 9 10 11 |
# File 'lib/grape/json_api/streamer.rb', line 6 def initialize(collection, = {}) @collection = collection @options = ActiveSupport::HashWithIndifferentAccess.new() @meta = @options[:meta] @links = @options[:links] end |
Instance Method Details
#each {|'{'| ... } ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/grape/json_api/streamer.rb', line 13 def each yield '{' yield "\"meta\": #{JSON.unparse(@meta)}," if @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(@links)}" if @links yield '}' end |
#serialize(model) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/grape/json_api/streamer.rb', line 31 def serialize(model) @options.delete(:meta) @options.delete(:links) @options[:is_collection] = false ::JSONAPI::Serializer.serialize(model, @options) end |