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.5'
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 |
# File 'lib/grape/json_api/streamer.rb', line 6 def initialize(collection, = {}) @collection = collection = ActiveSupport::HashWithIndifferentAccess.new() end |
Instance Method Details
#each {|'{'| ... } ⇒ Object
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 [: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 [: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 |