feedx 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -4
- data/Gemfile.lock +2 -2
- data/feedx.gemspec +1 -1
- data/lib/feedx/pusher.rb +16 -6
- data/spec/feedx/pusher_spec.rb +16 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88ccb5e67e8ad202fab870bea54b8ff04c26b644fa3f8e07f8aa3b0c09a75351
|
4
|
+
data.tar.gz: b832b9867141506986402d5a091f0c35528ee739ea624e7616e7e018748c0cd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e9df1a95a2821b882c6eed8e0d7b36532bb734b43405eb5e90c24984b8c7a0ef5fa4f7f8b35fd371a7ce724ce00e6d9a6174753ae552340773e9367024b8fce
|
7
|
+
data.tar.gz: 9e2d6714753f66eb2976a591eaf95a1b7caa4476e404d5622820de824bc7212e182b8d3b6cac0ae50cc5b2a6bcfd1cc4c2ab94e1a36b20b99563bae957969a45
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/feedx.gemspec
CHANGED
data/lib/feedx/pusher.rb
CHANGED
@@ -4,13 +4,22 @@ require 'bfs'
|
|
4
4
|
module Feedx
|
5
5
|
# Pushes a relation as a protobuf encoded stream to an S3 location.
|
6
6
|
class Pusher
|
7
|
-
#
|
7
|
+
# See constructor.
|
8
|
+
def self.perform(url, opts={}, &block)
|
9
|
+
new(url, opts, &block).perform
|
10
|
+
end
|
11
|
+
|
8
12
|
# @param [String] url the destination URL.
|
9
13
|
# @param [Hash] opts options
|
14
|
+
# @option opts [Enumerable,ActiveRecord::Relation] :enum relation or enumerator to stream.
|
10
15
|
# @option opts [Symbol,Class<Feedx::Format::Abstract>] :format custom formatter. Default: from file extension.
|
11
16
|
# @option opts [Symbol,Class<Feedx::Compression::Abstract>] :compress enable compression. Default: from file extension.
|
12
|
-
|
13
|
-
|
17
|
+
# @yield A block factory to generate the relation or enumerator.
|
18
|
+
# @yieldreturn [Enumerable,ActiveRecord::Relation] the relation or enumerator to stream.
|
19
|
+
def initialize(url, opts={}, &block)
|
20
|
+
@enum = opts[:enum] || block
|
21
|
+
raise ArgumentError, "#{self.class.name}.new expects an :enum option or a block factory" unless @enum
|
22
|
+
|
14
23
|
@blob = BFS::Blob.new(url)
|
15
24
|
@format = detect_format(opts[:format])
|
16
25
|
@compress = detect_compress(opts[:compress])
|
@@ -54,9 +63,10 @@ module Feedx
|
|
54
63
|
end
|
55
64
|
|
56
65
|
def write_to(io)
|
57
|
-
stream
|
58
|
-
|
59
|
-
|
66
|
+
stream = @format.new(io)
|
67
|
+
relation = @enum.is_a?(Proc) ? @enum.call : @enum
|
68
|
+
iterator = relation.respond_to?(:find_each) ? :find_each : :each
|
69
|
+
relation.send(iterator) {|rec| stream.write(rec) }
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
data/spec/feedx/pusher_spec.rb
CHANGED
@@ -13,8 +13,8 @@ RSpec.describe Feedx::Pusher do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
let :
|
17
|
-
%w[x y z].map {|
|
16
|
+
let :enumerable do
|
17
|
+
%w[x y z].map {|t| model.new(t) } * 100
|
18
18
|
end
|
19
19
|
|
20
20
|
let(:tempdir) { Dir.mktmpdir }
|
@@ -22,35 +22,39 @@ RSpec.describe Feedx::Pusher do
|
|
22
22
|
|
23
23
|
it 'should reject invalid inputs' do
|
24
24
|
expect do
|
25
|
-
described_class.
|
25
|
+
described_class.perform "file://#{tempdir}/file.txt", enum: enumerable
|
26
26
|
end.to raise_error(/unable to detect format/)
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should push compressed JSON' do
|
30
|
-
|
31
|
-
size
|
32
|
-
expect(size).to be_within(20).of(140)
|
30
|
+
size = described_class.perform "file://#{tempdir}/file.jsonz", enum: enumerable
|
31
|
+
expect(size).to be_within(20).of(166)
|
33
32
|
expect(File.size("#{tempdir}/file.jsonz")).to eq(size)
|
34
33
|
end
|
35
34
|
|
36
35
|
it 'should push plain JSON' do
|
37
|
-
|
38
|
-
size = pusher.perform
|
36
|
+
size = described_class.perform "file://#{tempdir}/file.json", enum: enumerable
|
39
37
|
expect(size).to be_within(0).of(15900)
|
40
38
|
expect(File.size("#{tempdir}/file.json")).to eq(size)
|
41
39
|
end
|
42
40
|
|
43
41
|
it 'should push compressed PB' do
|
44
|
-
|
45
|
-
size = pusher.perform
|
42
|
+
size = described_class.perform "file://#{tempdir}/file.pbz", enum: enumerable
|
46
43
|
expect(size).to be_within(20).of(41)
|
47
44
|
expect(File.size("#{tempdir}/file.pbz")).to eq(size)
|
48
45
|
end
|
49
46
|
|
50
47
|
it 'should push plain PB' do
|
51
|
-
|
52
|
-
size = pusher.perform
|
48
|
+
size = described_class.perform "file://#{tempdir}/file.pb", enum: enumerable
|
53
49
|
expect(size).to be_within(0).of(1200)
|
54
50
|
expect(File.size("#{tempdir}/file.pb")).to eq(size)
|
55
51
|
end
|
52
|
+
|
53
|
+
it 'should support factories' do
|
54
|
+
size = described_class.perform("file://#{tempdir}/file.json") do
|
55
|
+
[model.new('xy')] * 10
|
56
|
+
end
|
57
|
+
expect(size).to be_within(0).of(540)
|
58
|
+
expect(File.size("#{tempdir}/file.json")).to eq(size)
|
59
|
+
end
|
56
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: feedx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Black Square Media Ltd
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bfs
|