superbolt 0.4.1 → 0.5.0

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: 130ad90f4d01f0db9885e281506098ce7d94fbbd
4
- data.tar.gz: 25b93e5f97684570de5cb06bfdd1d4ca4ba04ca3
3
+ metadata.gz: 28f631baf4151d4a8a1cc90a04c4605720fe7add
4
+ data.tar.gz: 512314e5212d36fcdba03dddd0cf8238460752dc
5
5
  SHA512:
6
- metadata.gz: 42fae214ed7742f7e1645ad311e9447f9ee3d7090d3f46df9a3428463acba5f77f19c1a55992587cb2ceb86088db922f74546a978cee8e387d580eeaf19a2553
7
- data.tar.gz: 076555ca2550d6c2c048814ac761a1dc3d7c74646b7dbd11627e3a004ea07f9bf83ae00a963eac6440e6c5a4516d14ca3f29233b1b72a846a32059ac26ba9e9a
6
+ metadata.gz: b52d7077b9d04ab8d8c0e262bd8f2bb9c88a189a8e7672c5e5d73a58137c769b3a4549bcfb7febce1433910f5314d3fe0ce294563278c1b4a3d36ee9f30c3fb6
7
+ data.tar.gz: 0c8e4857cc0874d2acb2e6f4b1eefb0d39b4fa3e41e4025288d3bfbbe84a0c988a13ddceb1a1ed9e1eb7d3711c3c3d58e0d37b39d80349a7845ba30a506a5f01
data/lib/superbolt.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
 
3
+ require 'file_marshal'
3
4
  require 'bunny'
4
5
  require 'amqp'
5
6
  require 'eventmachine'
@@ -31,6 +32,10 @@ require "superbolt/app"
31
32
  require "superbolt/processor"
32
33
  require "superbolt/facade"
33
34
  require "superbolt/messenger"
35
+ require "superbolt/file_manager"
36
+ require "superbolt/file_unpacker"
37
+ require "superbolt/file_packer"
38
+
34
39
  require "superbolt/spec_helpers"
35
40
 
36
41
  require "superbolt/router"
@@ -1,9 +1,10 @@
1
1
  module Superbolt
2
-
2
+
3
3
  def self.config=(options)
4
4
  @config = Config.new({
5
5
  env: env,
6
- app_name: app_name
6
+ app_name: app_name,
7
+ file_matcher: file_matcher
7
8
  }.merge(options))
8
9
  end
9
10
 
@@ -16,7 +17,7 @@ module Superbolt
16
17
  end
17
18
 
18
19
  class << self
19
- attr_writer :env
20
+ attr_writer :env, :file_matcher
20
21
  attr_accessor :app_name
21
22
  end
22
23
 
@@ -24,6 +25,10 @@ module Superbolt
24
25
  @env || 'development'
25
26
  end
26
27
 
28
+ def self.file_matcher
29
+ @file_matcher || /_file$/
30
+ end
31
+
27
32
  def self.message(args={})
28
33
  Superbolt::Messenger.new(args)
29
34
  end
@@ -0,0 +1,24 @@
1
+ module Superbolt
2
+ class FileManager
3
+ attr_reader :hash
4
+
5
+ def initialize(hash)
6
+ @hash = hash
7
+ end
8
+
9
+ def perform
10
+ file_keys.each do |key|
11
+ hash[key] = process_file(hash[key])
12
+ end
13
+ hash
14
+ end
15
+
16
+ def file_keys
17
+ @file_keys ||= hash.keys.find_all { |key| key.to_s.match(Superbolt.file_matcher) }
18
+ end
19
+
20
+ def process_file(arg)
21
+ raise NotImplementedError
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Superbolt
2
+ class FilePacker < FileManager
3
+ def process_file(file)
4
+ FileMarshal::Dumper.new(file).to_hash
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Superbolt
2
+ class FileUnpacker < FileManager
3
+ def process_file(file_hash)
4
+ FileMarshal::Loader.new(file_hash).tempfile
5
+ end
6
+ end
7
+ end
@@ -9,7 +9,8 @@ module Superbolt
9
9
  end
10
10
 
11
11
  def parse
12
- JSON.parse(payload)
12
+ hash = JSON.parse(payload)
13
+ unpack_files(hash)
13
14
  rescue JSON::ParserError
14
15
  payload
15
16
  end
@@ -21,6 +22,11 @@ module Superbolt
21
22
  def ack
22
23
  channel.acknowledge(tag)
23
24
  end
24
- end
25
- end
26
25
 
26
+ def unpack_files(hash)
27
+ return hash unless hash.is_a?(Hash) && hash["arguments"].is_a?(Hash)
28
+ hash['arguments'] = FileUnpacker.new(hash["arguments"]).perform
29
+ hash
30
+ end
31
+ end
32
+ end
@@ -47,9 +47,18 @@ module Superbolt
47
47
 
48
48
  def send!(args=nil)
49
49
  self.arguments = args if args
50
+ pack_files
50
51
  queue.push(message)
51
52
  end
52
53
 
54
+ def pack_files
55
+ return unless arguments.is_a?(Hash)
56
+ file_keys = arguments.keys.find_all { |key| key.to_s.match(Superbolt.file_matcher) }
57
+ file_keys.each do |key|
58
+ arguments[key] = FileMarshal::Dumper.new(arguments[key]).to_hash
59
+ end
60
+ end
61
+
53
62
  def queue
54
63
  unless name
55
64
  raise "no destination app name defined, please pass one in"
@@ -1,3 +1,3 @@
1
1
  module Superbolt
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Superbolt::FilePacker do
4
+ let(:packer) { Superbolt::FilePacker.new(hash) }
5
+
6
+ let(:hash) {
7
+ {
8
+ my_file: file,
9
+ foo: 'bar'
10
+ }
11
+ }
12
+
13
+ let(:dumper) { double('dumper', to_hash: {}) }
14
+
15
+ let(:file) { double('file') }
16
+
17
+ describe '#perform' do
18
+ it "makes matching keys into a file hash via FileMarshal" do
19
+ FileMarshal::Dumper.should_receive(:new).with(file).and_return(dumper)
20
+ dumper.should_receive(:to_hash)
21
+ packer.perform.should == {
22
+ foo: 'bar',
23
+ my_file: {}
24
+ }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Superbolt::FileUnpacker do
4
+ let(:unpacker){ Superbolt::FileUnpacker.new(hash) }
5
+ let(:file) { File.open(File.dirname(__FILE__) + '/support/commodore.jpg')}
6
+ let(:file_hash) {
7
+ # back and forth in JSON required due to Time.parse issues
8
+ JSON.parse(FileMarshal::Dumper.new(file).to_hash.to_json)
9
+ }
10
+ let(:hash) {
11
+ {
12
+ my_file: file_hash,
13
+ foo: 'bar'
14
+ }
15
+ }
16
+
17
+ describe "#perform" do
18
+ it "makes the keys matching the Superbolt.file_matcher" do
19
+ unpacker.perform[:my_file].should be_a Tempfile
20
+ end
21
+
22
+ it "leaves everything else alone" do
23
+ unpacker.perform[:foo].should == "bar"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Superbolt::IncomingMessage do
4
+ let(:message){ Superbolt::IncomingMessage.new(delivery_info, payload, channel) }
5
+ let(:payload){ { some: "message" }.to_json }
6
+ let(:delivery_info) { double("info", delivery_tag: "tag") }
7
+ let(:channel) { double("channel") }
8
+
9
+ describe '#parse' do
10
+ context 'payload is not json' do
11
+ let(:payload) { 'foo' }
12
+
13
+ it 'just returns the payload' do
14
+ message.parse.should == payload
15
+ end
16
+ end
17
+
18
+ context 'payload is json' do
19
+ it "parses it to a hash" do
20
+ message.parse.should == {'some' => 'message'}
21
+ end
22
+
23
+ context 'payload has a file' do
24
+ let(:payload) { { 'arguments' => {'some_file' => {'file_hash' => 'yup'}} }.to_json }
25
+ let(:packer) { double('packer', perform: {'some_file' => 'some_file'} ) }
26
+
27
+ it "uses FilePacker to rewrite files into a hash" do
28
+ Superbolt::FileUnpacker.should_receive(:new)
29
+ .with({'some_file' => {'file_hash' => 'yup'}})
30
+ .and_return(packer)
31
+ message.parse.should == {'arguments' => packer.perform}
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ describe '#reject' do
38
+ it "calls reject on the channel with the appropritate data and options" do
39
+ channel.should_receive(:reject).with('tag', true)
40
+
41
+ message.reject
42
+ end
43
+
44
+ it "can reject without requeuing" do
45
+ channel.should_receive(:reject).with('tag', false)
46
+
47
+ message.reject(false)
48
+ end
49
+ end
50
+
51
+ describe "#ack" do
52
+ it "calls acknowledge on the channel" do
53
+ channel.should_receive(:acknowledge).with('tag')
54
+
55
+ message.ack
56
+ end
57
+ end
58
+ end
@@ -82,6 +82,8 @@ describe Superbolt::Messenger do
82
82
  end
83
83
 
84
84
  describe 'send!' do
85
+ let(:image_file) { File.open(File.dirname(__FILE__) + '/support/commodore.jpg')}
86
+
85
87
  before do
86
88
  messenger
87
89
  .to(name)
@@ -97,5 +99,14 @@ describe Superbolt::Messenger do
97
99
  'arguments' => 'none'
98
100
  }
99
101
  end
102
+
103
+ it "converts any values that have key ending in '_file' with FileMarshal" do
104
+ messenger.send!({
105
+ image_file: image_file
106
+ })
107
+
108
+ message = queue.pop
109
+ message['arguments']['image_file'].should be_a(Tempfile)
110
+ end
100
111
  end
101
112
  end
Binary file
data/superbolt.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "amqp"
23
23
  spec.add_dependency "bunny"
24
24
  spec.add_dependency 'eventmachine'
25
+ spec.add_dependency 'file_marshal'
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.3"
27
28
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superbolt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - socialchorus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-30 00:00:00.000000000 Z
11
+ date: 2014-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: file_marshal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +145,9 @@ files:
131
145
  - lib/superbolt/connection/base.rb
132
146
  - lib/superbolt/connection/queue.rb
133
147
  - lib/superbolt/facade.rb
148
+ - lib/superbolt/file_manager.rb
149
+ - lib/superbolt/file_packer.rb
150
+ - lib/superbolt/file_unpacker.rb
134
151
  - lib/superbolt/incoming_message.rb
135
152
  - lib/superbolt/message_handler.rb
136
153
  - lib/superbolt/messenger.rb
@@ -150,11 +167,15 @@ files:
150
167
  - spec/app_spec.rb
151
168
  - spec/config_spec.rb
152
169
  - spec/connection_spec.rb
170
+ - spec/file_packer_spec.rb
171
+ - spec/file_unpacker_spec.rb
172
+ - spec/incoming_message_spec.rb
153
173
  - spec/messenger_spec.rb
154
174
  - spec/queue_spec.rb
155
175
  - spec/router_spec.rb
156
176
  - spec/spec_helper.rb
157
177
  - spec/superbolt_spec.rb
178
+ - spec/support/commodore.jpg
158
179
  - spec/support/queue_helpers.rb
159
180
  - superbolt.gemspec
160
181
  homepage: ''
@@ -185,9 +206,13 @@ test_files:
185
206
  - spec/app_spec.rb
186
207
  - spec/config_spec.rb
187
208
  - spec/connection_spec.rb
209
+ - spec/file_packer_spec.rb
210
+ - spec/file_unpacker_spec.rb
211
+ - spec/incoming_message_spec.rb
188
212
  - spec/messenger_spec.rb
189
213
  - spec/queue_spec.rb
190
214
  - spec/router_spec.rb
191
215
  - spec/spec_helper.rb
192
216
  - spec/superbolt_spec.rb
217
+ - spec/support/commodore.jpg
193
218
  - spec/support/queue_helpers.rb