Skip to content

Commit 68c4082

Browse files
author
David Roy
committed
first commit
0 parents  commit 68c4082

12 files changed

+127
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/
10+
*.bundle
11+
*.so
12+
*.o
13+
*.a
14+
mkmf.log
15+
*.gem
16+
coverage
17+
dump.rdb

.hound.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
LineLength:
2+
Description: 'Limit lines to 130 characters.'
3+
Max: 130
4+
5+
StringLiterals:
6+
EnforcedStyle: single_quotes
7+
8+
HashSyntax:
9+
Description: >
10+
Prefer Ruby 1.9 hash syntax { a: 1, b: 2 } over 1.8 syntax
11+
{ :a => 1, :b => 2 }.
12+
EnforcedStyle: ruby19
13+
Enabled: true
14+
15+
RedundantBegin:
16+
Enabled: false

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format documentation

.rubocop.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
inherit_from: .hound.yml
2+
3+
Documentation:
4+
Enabled: false
5+
NumericLiterals:
6+
MinDigits: 10

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0

Gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in starbuck_core.gemspec
4+
gemspec
5+
6+
gem 'rubocop', require: false
7+
gem 'simplecov', require: false, group: :test
8+
gem 'pry', require: false

README.md

Whitespace-only changes.

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'bundler/gem_tasks'
2+
3+
require 'rake/testtask'
4+
5+
Rake::TestTask.new do |t|
6+
t.libs << 'test'
7+
t.pattern = 'test/*_test.rb'
8+
end

grape_json_api_streamer.gemspec

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'grape/json_api/streamer'
5+
require 'grape/json_api/version'
6+
7+
Gem::Specification.new do |spec|
8+
spec.name = 'grape_json_api_streamer'
9+
spec.version = Grape::JSONAPI::Streamer::VERSION
10+
spec.authors = ['David Roy']
11+
spec.email = ['[email protected]']
12+
spec.summary = 'Class for streaming JSON API via Grape stream'
13+
spec.homepage = 'https://github.com/Cabbit/grape_json_api_streamer'
14+
15+
spec.files = `git ls-files -z`.split("\x0")
16+
spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) }
17+
spec.test_files = spec.files.grep(/^(test|spec|features)/)
18+
spec.require_paths = ['lib']
19+
20+
spec.add_dependency 'grape', '>= 0.16.2'
21+
spec.add_dependency 'jsonapi-serializers', '>= 0.13.0'
22+
spec.add_development_dependency 'bundler', '~> 1.7'
23+
spec.add_development_dependency 'rake', '~> 10.0'
24+
end

lib/grape/json_api/streamer.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Grape
2+
module JSONAPI
3+
class Streamer
4+
def initialize(collection)
5+
@collection = collection
6+
end
7+
8+
def collection
9+
@collection
10+
end
11+
12+
def first
13+
@first ||= true
14+
end
15+
16+
def first=(first)
17+
@first = first
18+
end
19+
20+
def each
21+
yield '{"data":['
22+
collection.lazy.each do |object|
23+
buffer = ''
24+
buffer << ',' unless first
25+
first = false
26+
data = serialize(object).as_json
27+
buffer << JSON.unparse(data)[8..-2].strip
28+
yield buffer
29+
end
30+
yield ']}'
31+
end
32+
33+
def serialize(model)
34+
JSONAPI::Serializer.serialize(model, is_collection: false)
35+
end
36+
end
37+
end
38+
end

lib/grape/json_api/version.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Grape
2+
module JSONAPI
3+
class Streamer
4+
VERSION = '0.0.1'
5+
end
6+
end
7+
end

test/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)