grape_json_api_streamer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3947b19e1ad570c1531abc9c18f65265ff142a46
4
- data.tar.gz: f68ff8b85660052472a4b10f7fad6f4812880fd3
3
+ metadata.gz: 395983ae9909735b82f732588bfeba09e1033343
4
+ data.tar.gz: 18096e8bd416a14eae54994259f27869d100106a
5
5
  SHA512:
6
- metadata.gz: 8d68e245263cc345eb74dcf5215d30d85cbc275b88b2dcf7ce15e5a82fb4e8c02035ff301c9dca64b238fe3c8d872a25e72236f9b97b7a468389cf160ed2638d
7
- data.tar.gz: 1fe50ea846c4e06fd7fdcea3def08759cc17aa18a212acbcfb275e822fc07c9d766e53e06aa78a8de857b702fe55f08a221146e1a285902b618226e249a9bccb
6
+ metadata.gz: 8bf85ef0d45f9154cc7b7c980db3fbcc8ba188b74bfe253e2fd46587e94527f7a388c5a1fecb206d272a5338caac9b3e2d8d1aca3033a2f33f2f24331d420163
7
+ data.tar.gz: 817b936c8d05be5fb5f0563d320ad12ce81f6bd02651d41b8185fdca2ee4910fbfee2eb916384f4c12a98ce552205404b52a2f8362246fbd81a100e0ef088ee5
data/README.md CHANGED
@@ -0,0 +1,42 @@
1
+ # grape_json_api_streamer
2
+
3
+ Based heavily on the suggestion by raulpopadineti at https://github.com/ruby-grape/grape/issues/1392 which is used for streaming Grape::Entity, this class provides a way to stream JSON::API using the jsonapi-serializers gem in grape.
4
+
5
+ ## Example
6
+ ```
7
+ require 'grape'
8
+ require 'grape/json_api/streamer'
9
+
10
+ class Model
11
+ def initialize(id)
12
+ @id = id
13
+ end
14
+
15
+ def id
16
+ @id
17
+ end
18
+
19
+ def foo
20
+ 'bar'
21
+ end
22
+ end
23
+
24
+ class ModelSerializer
25
+ include JSONAPI::Serializer
26
+
27
+ attribute :foo
28
+ end
29
+
30
+ class MockAPI < Grape::API
31
+ format :json
32
+ content_type :json, 'application/json;charset=UTF-8'
33
+
34
+ resource :foo do
35
+ get do
36
+ model = Model.new(1)
37
+ model2 = Model.new(2)
38
+ stream Grape::JSONAPI::Streamer.new([model, model2])
39
+ end
40
+ end
41
+ end
42
+ ```
@@ -3,12 +3,15 @@ require 'jsonapi-serializers'
3
3
  module Grape
4
4
  module JSONAPI
5
5
  class Streamer
6
- def initialize(collection)
6
+ def initialize(collection, options = {})
7
7
  @collection = collection
8
+ @options = ActiveSupport::HashWithIndifferentAccess.new(options)
8
9
  end
9
10
 
10
11
  def each
11
- yield '{"data":['
12
+ yield '{'
13
+ yield "\"meta\": #{@options[:meta].to_json}," if @options[:meta]
14
+ yield '"data":['
12
15
  first = true
13
16
  @collection.lazy.each do |object|
14
17
  buffer = ''
@@ -18,7 +21,9 @@ module Grape
18
21
  buffer << JSON.unparse(data)[8..-2].strip
19
22
  yield buffer
20
23
  end
21
- yield ']}'
24
+ yield ']'
25
+ yield ",\"links\": #{@options[:links].to_json}" if @options[:links]
26
+ yield '}'
22
27
  end
23
28
 
24
29
  def serialize(model)
@@ -1,7 +1,7 @@
1
1
  module Grape
2
2
  module JSONAPI
3
3
  class Streamer
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
6
6
  end
7
7
  end
@@ -28,10 +28,17 @@ class MockAPI < Grape::API
28
28
  content_type :json, 'application/json;charset=UTF-8'
29
29
 
30
30
  resource :foo do
31
+ params do
32
+ optional :limit, type: Integer
33
+ end
31
34
  get do
32
- model = Model.new(1)
33
- model2 = Model.new(2)
34
- stream Grape::JSONAPI::Streamer.new([model, model2])
35
+ models = [Model.new(1), Model.new(2)]
36
+ if params[:limit]
37
+ options = { 'meta' => { 'total-records' => 100 }, 'links' => { 'self' => '/foo?limit=2' } }
38
+ stream Grape::JSONAPI::Streamer.new(models, options)
39
+ else
40
+ stream Grape::JSONAPI::Streamer.new(models)
41
+ end
35
42
  end
36
43
  end
37
44
  end
@@ -43,28 +50,63 @@ class StreamerSpec < Minitest::Spec
43
50
  MockAPI
44
51
  end
45
52
 
46
- let(:body) do
47
- {
48
- 'data' => [
49
- {
50
- 'type' => 'models',
51
- 'id' => '1',
52
- 'attributes' => { 'foo' => 'bar' },
53
- 'links' => { 'self' => '/models/1' }
54
- }, {
55
- 'type' => 'models',
56
- 'id' => '2',
57
- 'attributes' => { 'foo' => 'bar' },
58
- 'links' => { 'self' => '/models/2' }
59
- }
60
- ]
61
- }
53
+ context 'when no options' do
54
+ let(:body) do
55
+ {
56
+ 'data' => [
57
+ {
58
+ 'type' => 'models',
59
+ 'id' => '1',
60
+ 'attributes' => { 'foo' => 'bar' },
61
+ 'links' => { 'self' => '/models/1' }
62
+ }, {
63
+ 'type' => 'models',
64
+ 'id' => '2',
65
+ 'attributes' => { 'foo' => 'bar' },
66
+ 'links' => { 'self' => '/models/2' }
67
+ }
68
+ ]
69
+ }
70
+ end
71
+
72
+ it 'should output json response' do
73
+ get '/foo'
74
+
75
+ assert_equal body, JSON.parse(last_response.body)
76
+ assert_equal 200, last_response.status
77
+ end
62
78
  end
63
79
 
64
- it 'should output json response' do
65
- get '/foo'
80
+ context 'when meta and links options provided' do
81
+ let(:body) do
82
+ {
83
+ 'meta' => {
84
+ 'total-records' => 100
85
+ },
86
+ 'data' => [
87
+ {
88
+ 'type' => 'models',
89
+ 'id' => '1',
90
+ 'attributes' => { 'foo' => 'bar' },
91
+ 'links' => { 'self' => '/models/1' }
92
+ }, {
93
+ 'type' => 'models',
94
+ 'id' => '2',
95
+ 'attributes' => { 'foo' => 'bar' },
96
+ 'links' => { 'self' => '/models/2' }
97
+ }
98
+ ],
99
+ 'links' => {
100
+ 'self' => '/foo?limit=2'
101
+ }
102
+ }
103
+ end
66
104
 
67
- assert_equal body, JSON.parse(last_response.body)
68
- assert_equal 200, last_response.status
105
+ it 'should output json response' do
106
+ get '/foo?limit=2'
107
+
108
+ assert_equal body, JSON.parse(last_response.body)
109
+ assert_equal 200, last_response.status
110
+ end
69
111
  end
70
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_json_api_streamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Roy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-03 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape