feedx 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93c6d8da8f0485d4cde2b935d55d176bd85265ed2983d5fda8b86e229635c666
4
- data.tar.gz: 0d32c1595c97c42937bbdb8765da4347d8041fc6e4db8c335a88a26c715eed02
3
+ metadata.gz: 88ccb5e67e8ad202fab870bea54b8ff04c26b644fa3f8e07f8aa3b0c09a75351
4
+ data.tar.gz: b832b9867141506986402d5a091f0c35528ee739ea624e7616e7e018748c0cd1
5
5
  SHA512:
6
- metadata.gz: 689dd5925778d4895cd1dbfd35bf717070dbfdbe4fe3c831d01108ccef637fb1eaeb1166945b152393cff653f71426dbdd326de78633b6ed4aab36640e11693e
7
- data.tar.gz: ea603ed38643a9484875d626ff9be7b42ae0a56f20e7a85ccdcbb0ca8101846084bfc4cdb4373793df3d4be36ca31eb92e0351186c85712937d88f1b46677e07
6
+ metadata.gz: 4e9df1a95a2821b882c6eed8e0d7b36532bb734b43405eb5e90c24984b8c7a0ef5fa4f7f8b35fd371a7ce724ce00e6d9a6174753ae552340773e9367024b8fce
7
+ data.tar.gz: 9e2d6714753f66eb2976a591eaf95a1b7caa4476e404d5622820de824bc7212e182b8d3b6cac0ae50cc5b2a6bcfd1cc4c2ab94e1a36b20b99563bae957969a45
@@ -1,5 +1,2 @@
1
1
  inherit_from:
2
- - https://storage.googleapis.com/bsm-misc/rubocop.yml?1541767352
3
-
4
- AllCops:
5
- TargetRubyVersion: "2.2"
2
+ - https://bitbucket.org/bsm/misc/raw/master/rubocop/default.yml
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- feedx (0.1.0)
4
+ feedx (0.2.0)
5
5
  bfs
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.0)
11
- bfs (0.2.2)
11
+ bfs (0.3.2)
12
12
  diff-lcs (1.3)
13
13
  google-protobuf (3.6.1)
14
14
  jaro_winkler (1.5.1)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'feedx'
3
- s.version = '0.1.0'
3
+ s.version = '0.2.0'
4
4
  s.authors = ['Black Square Media Ltd']
5
5
  s.email = ['info@blacksquaremedia.com']
6
6
  s.summary = %(Exchange data between components via feeds)
@@ -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
- # @param [Enumerable,ActiveRecord::Relation] relation to stream.
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
- def initialize(relation, url, opts={})
13
- @relation = relation
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 = @format.new(io)
58
- enum = @relation.respond_to?(:find_each) ? :find_each : :each
59
- @relation.send(enum) {|rec| stream.write(rec) }
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
@@ -13,8 +13,8 @@ RSpec.describe Feedx::Pusher do
13
13
  end
14
14
  end
15
15
 
16
- let :relation do
17
- %w[x y z].map {|_t| model.new('t') } * 100
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.new relation, "file://#{tempdir}/file.txt"
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
- pusher = described_class.new relation, "file://#{tempdir}/file.jsonz"
31
- size = pusher.perform
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
- pusher = described_class.new relation, "file://#{tempdir}/file.json"
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
- pusher = described_class.new relation, "file://#{tempdir}/file.pbz"
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
- pusher = described_class.new relation, "file://#{tempdir}/file.pb"
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.1.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-09 00:00:00.000000000 Z
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bfs