transloadit 1.2.0 → 2.0.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.
Files changed (44) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +3 -1
  3. data/.travis.yml +4 -4
  4. data/CHANGELOG.md +26 -0
  5. data/LICENSE +1 -1
  6. data/README.md +141 -16
  7. data/examples/README.md +185 -0
  8. data/examples/basic/audio-concat-transcoder.rb +40 -0
  9. data/examples/basic/audio-transcoder.rb +37 -0
  10. data/examples/basic/image-transcoder.rb +27 -0
  11. data/examples/basic/main.rb +69 -0
  12. data/examples/basic/media-transcoder.rb +13 -0
  13. data/lib/transloadit.rb +31 -0
  14. data/lib/transloadit/api_model.rb +73 -0
  15. data/lib/transloadit/assembly.rb +103 -63
  16. data/lib/transloadit/exception.rb +27 -0
  17. data/lib/transloadit/request.rb +23 -41
  18. data/lib/transloadit/response/assembly.rb +25 -0
  19. data/lib/transloadit/template.rb +63 -0
  20. data/lib/transloadit/version.rb +1 -1
  21. data/test/fixtures/cassettes/create_template.yml +48 -0
  22. data/test/fixtures/cassettes/delete_template.yml +44 -0
  23. data/test/fixtures/cassettes/fetch_assemblies.yml +44 -0
  24. data/test/fixtures/cassettes/fetch_assembly_notifications.yml +44 -0
  25. data/test/fixtures/cassettes/fetch_assembly_ok.yml +36 -0
  26. data/test/fixtures/cassettes/fetch_billing.yml +44 -0
  27. data/test/fixtures/cassettes/{fetch_bored.yml → fetch_root.yml} +6 -6
  28. data/test/fixtures/cassettes/fetch_template.yml +44 -0
  29. data/test/fixtures/cassettes/fetch_templates.yml +44 -0
  30. data/test/fixtures/cassettes/rate_limit_fail.yml +105 -0
  31. data/test/fixtures/cassettes/rate_limit_succeed.yml +79 -0
  32. data/test/fixtures/cassettes/replay_assembly.yml +49 -0
  33. data/test/fixtures/cassettes/replay_assembly_notification.yml +48 -0
  34. data/test/fixtures/cassettes/submit_assembly.yml +1 -36
  35. data/test/fixtures/cassettes/update_template.yml +48 -0
  36. data/test/test_helper.rb +4 -0
  37. data/test/unit/test_transloadit.rb +14 -1
  38. data/test/unit/transloadit/test_api.rb +50 -0
  39. data/test/unit/transloadit/test_assembly.rb +178 -47
  40. data/test/unit/transloadit/test_request.rb +28 -20
  41. data/test/unit/transloadit/test_response.rb +44 -0
  42. data/test/unit/transloadit/test_template.rb +118 -0
  43. data/transloadit.gemspec +3 -3
  44. metadata +70 -33
@@ -1,43 +1,8 @@
1
1
  ---
2
2
  http_interactions:
3
- - request:
4
- method: get
5
- uri: http://api2.transloadit.com/instances/bored
6
- body:
7
- encoding: US-ASCII
8
- string: ''
9
- headers:
10
- Accept:
11
- - ! '*/*; q=0.5, application/xml'
12
- Accept-Encoding:
13
- - gzip, deflate
14
- User-Agent:
15
- - Transloadit Ruby SDK 0.0.1
16
- response:
17
- status:
18
- code: 200
19
- message: OK
20
- headers:
21
- Access-Control-Allow-Headers:
22
- - X-Requested-With, Content-Type, Accept, Content-Length
23
- Access-Control-Allow-Methods:
24
- - POST, GET, PUT, DELETE, OPTIONS
25
- Access-Control-Allow-Origin:
26
- - ! '*'
27
- Content-Type:
28
- - text/plain
29
- Content-Length:
30
- - '98'
31
- Connection:
32
- - keep-alive
33
- body:
34
- encoding: UTF-8
35
- string: ! '{"ok":"BORED_INSTANCE_FOUND","host":"vivian.transloadit.com","api2_host":"vivian.transloadit.com"}'
36
- http_version: '1.1'
37
- recorded_at: Fri, 08 Mar 2013 15:13:10 GMT
38
3
  - request:
39
4
  method: post
40
- uri: http://vivian.transloadit.com/assemblies
5
+ uri: http://api2.transloadit.com/assemblies
41
6
  body:
42
7
  encoding: UTF-8
43
8
  string: params=%7B%22steps%22%3A%7B%22a479db2c601661d8f914caf9cf258c0b%22%3A%7B%22robot%22%3A%22%2Fvideo%2Fthumbs%22%7D%7D%2C%22redirect_url%22%3A%22http%3A%2F%2Ffoo.bar%2F%22%2C%22auth%22%3A%7B%22key%22%3A%22%22%7D%2C%22file_0%22%3A%22%23%3CFile%3A0x00000100c63738%3E%22%7D
@@ -0,0 +1,48 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: http://api2.transloadit.com/templates/55c965a063a311e6ba2d379ef10b28f7
6
+ body:
7
+ encoding: UTF-8
8
+ string: params=%7B%22auth%22%3A%7B%22key%22%3A%22%22%7D%7D
9
+ headers:
10
+ Accept:
11
+ - "*/*"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Transloadit Ruby SDK 1.2.0
16
+ Content-Length:
17
+ - '50'
18
+ Content-Type:
19
+ - application/x-www-form-urlencoded
20
+ Host:
21
+ - api2.transloadit.com
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Access-Control-Allow-Headers:
28
+ - X-Requested-With, Content-Type, Cache-Control, Accept, Content-Length
29
+ Access-Control-Allow-Methods:
30
+ - POST, GET, PUT, DELETE, OPTIONS
31
+ Access-Control-Allow-Origin:
32
+ - "*"
33
+ Cache-Control:
34
+ - no-cache
35
+ Content-Type:
36
+ - application/json; charset=utf-8
37
+ Date:
38
+ - Wed, 24 Aug 2016 09:58:34 GMT
39
+ Content-Length:
40
+ - '75'
41
+ Connection:
42
+ - keep-alive
43
+ body:
44
+ encoding: UTF-8
45
+ string: '{"ok":"TEMPLATE_UPDATED", "template_id":"55c965a063a311e6ba2d379ef10b28f7"}'
46
+ http_version:
47
+ recorded_at: Wed, 24 Aug 2016 09:58:33 GMT
48
+ recorded_with: VCR 3.0.3
@@ -15,3 +15,7 @@ VCR.configure do |c|
15
15
  c.default_cassette_options = { :record => :none }
16
16
  c.hook_into :webmock
17
17
  end
18
+
19
+ def values_from_post_body(body)
20
+ Addressable::URI.parse('?' + URI.decode(body)).query_values
21
+ end
@@ -65,7 +65,7 @@ describe Transloadit do
65
65
  step.options.must_equal :width => 320
66
66
  end
67
67
 
68
- it 'must create assemblies' do
68
+ it 'must create assembly api instances' do
69
69
  step = @transloadit.step(nil, nil)
70
70
  assembly = @transloadit.assembly :steps => step
71
71
 
@@ -83,6 +83,19 @@ describe Transloadit do
83
83
  assembly.steps.must_equal steps.inject({}) {|h,s| h.merge s }
84
84
  end
85
85
 
86
+ it 'must get user billing report' do
87
+ VCR.use_cassette 'fetch_billing' do
88
+ bill = Transloadit.new(:key => '').bill(9, 2016)
89
+ bill['ok'].must_equal 'BILL_FOUND'
90
+ bill['invoice_id'].must_equal '76fe5df1c93a0a530f3e583805cf98b4'
91
+ end
92
+ end
93
+
94
+ it 'must create template api instances' do
95
+ template = @transloadit.template
96
+ template.must_be_kind_of Transloadit::Template
97
+ end
98
+
86
99
  it 'must inspect like a hash' do
87
100
  @transloadit.inspect.must_equal @transloadit.to_hash.inspect
88
101
  end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ describe Transloadit::ApiModel do
4
+ let(:foo) { 'foo' }
5
+ let(:bar) { 'bar' }
6
+ let(:transloadit) { Transloadit.new(:key => '') }
7
+
8
+ let(:api) { Transloadit::ApiModel.new(
9
+ transloadit,
10
+ :foo => foo,
11
+ :bar => bar
12
+ )}
13
+
14
+ it 'must allow initialization' do
15
+ Transloadit::ApiModel.new(transloadit).
16
+ must_be_kind_of Transloadit::ApiModel
17
+
18
+ Transloadit::Template.new(transloadit).
19
+ must_be_kind_of Transloadit::Template
20
+ end
21
+
22
+ describe 'when initialized' do
23
+ it 'must store a pointer to the transloadit instance' do
24
+ api.transloadit.must_equal transloadit
25
+ end
26
+
27
+ it 'must remember the options passed' do
28
+ api.options.must_equal(
29
+ :foo => foo,
30
+ :bar => bar
31
+ )
32
+ end
33
+
34
+ it 'must inspect like a hash' do
35
+ api.inspect.must_equal api.to_hash.inspect
36
+ end
37
+
38
+ it 'must produce Transloadit-compatible hash output' do
39
+ api.to_hash.must_equal(
40
+ :auth => transloadit.to_hash,
41
+ :foo => foo,
42
+ :bar => bar
43
+ )
44
+ end
45
+
46
+ it 'must produce Transloadit-compatible JSON output' do
47
+ api.to_json.must_equal MultiJson.dump(api.to_hash)
48
+ end
49
+ end
50
+ end
@@ -5,9 +5,8 @@ describe Transloadit::Assembly do
5
5
  @transloadit = Transloadit.new(:key => '')
6
6
  end
7
7
 
8
- it 'must allow initialization' do
9
- Transloadit::Assembly.new(@transloadit).
10
- must_be_kind_of Transloadit::Assembly
8
+ it 'must inherit from Transloadit::ApiModel class' do
9
+ (Transloadit::Assembly < Transloadit::ApiModel).must_equal true
11
10
  end
12
11
 
13
12
  describe 'when initialized' do
@@ -20,17 +19,6 @@ describe Transloadit::Assembly do
20
19
  :redirect_url => @redirect
21
20
  end
22
21
 
23
- it 'must store a pointer to the transloadit instance' do
24
- @assembly.transloadit.must_equal @transloadit
25
- end
26
-
27
- it 'must remember the options passed' do
28
- @assembly.options.must_equal(
29
- :steps => @step,
30
- :redirect_url => @redirect
31
- )
32
- end
33
-
34
22
  it 'must wrap its step in a hash' do
35
23
  @assembly.steps.must_equal @step.to_hash
36
24
  end
@@ -45,10 +33,6 @@ describe Transloadit::Assembly do
45
33
  @assembly.steps.must_equal :foo => 1
46
34
  end
47
35
 
48
- it 'must inspect like a hash' do
49
- @assembly.inspect.must_equal @assembly.to_hash.inspect
50
- end
51
-
52
36
  it 'must produce Transloadit-compatible hash output' do
53
37
  @assembly.to_hash.must_equal(
54
38
  :auth => @transloadit.to_hash,
@@ -57,13 +41,9 @@ describe Transloadit::Assembly do
57
41
  )
58
42
  end
59
43
 
60
- it 'must produce Transloadit-compatible JSON output' do
61
- @assembly.to_json.must_equal MultiJson.dump(@assembly.to_hash)
62
- end
63
-
64
44
  it 'must submit files for upload' do
65
45
  VCR.use_cassette 'submit_assembly' do
66
- response = @assembly.submit! open('lib/transloadit/version.rb')
46
+ response = @assembly.create! open('lib/transloadit/version.rb')
67
47
  response.code.must_equal 302
68
48
  response.headers[:location].must_match %r{^http://foo.bar/}
69
49
  end
@@ -73,7 +53,9 @@ describe Transloadit::Assembly do
73
53
  include WebMock::API
74
54
 
75
55
  before do
76
- stub_request(:post, 'jane.transloadit.com/assemblies')
56
+ WebMock.reset!
57
+ stub_request(:post, 'api2.transloadit.com/assemblies')
58
+ .to_return(body: '{"ok":"ASSEMBLY_COMPLETED"}')
77
59
  end
78
60
 
79
61
  after do
@@ -81,30 +63,92 @@ describe Transloadit::Assembly do
81
63
  end
82
64
 
83
65
  it 'must allow to send a template id along' do
84
- VCR.use_cassette 'fetch_bored' do
85
- Transloadit::Assembly.new(
86
- @transloadit,
87
- :template_id => 'TEMPLATE_ID'
88
- ).submit!
89
-
90
- assert_requested(:post, 'jane.transloadit.com/assemblies') do |req|
91
- values = values_from_post_body(req.body)
92
- MultiJson.load(values['params'])['template_id'].must_equal 'TEMPLATE_ID'
93
- end
66
+ Transloadit::Assembly.new(
67
+ @transloadit,
68
+ :template_id => 'TEMPLATE_ID'
69
+ ).create!
70
+
71
+ assert_requested(:post, 'api2.transloadit.com/assemblies') do |req|
72
+ values = values_from_post_body(req.body)
73
+ MultiJson.load(values['params'])['template_id'].must_equal 'TEMPLATE_ID'
94
74
  end
95
75
  end
96
76
 
97
77
  it 'must allow to send the fields hash' do
98
- VCR.use_cassette 'fetch_bored' do
99
- Transloadit::Assembly.new(
100
- @transloadit,
101
- :fields => {:tag => 'ninja-cat'}
102
- ).submit!
103
-
104
- assert_requested(:post, 'jane.transloadit.com/assemblies') do |req|
105
- values = values_from_post_body(req.body)
106
- values['tag'].must_equal 'ninja-cat'
107
- MultiJson.load(values['params'])['fields']['tag'].must_equal 'ninja-cat'
78
+ Transloadit::Assembly.new(
79
+ @transloadit,
80
+ :fields => {:tag => 'ninja-cat'}
81
+ ).create!
82
+
83
+ assert_requested(:post, 'api2.transloadit.com/assemblies') do |req|
84
+ values = values_from_post_body(req.body)
85
+ values['tag'].must_equal 'ninja-cat'
86
+ MultiJson.load(values['params'])['fields']['tag'].must_equal 'ninja-cat'
87
+ end
88
+ end
89
+
90
+ it 'must allow steps through the create! method' do
91
+ Transloadit::Assembly.new(@transloadit).create!(
92
+ { :steps => @transloadit.step('thumbs', '/video/thumbs') }
93
+ )
94
+
95
+ assert_requested(:post, 'api2.transloadit.com/assemblies') do |req|
96
+ values = values_from_post_body(req.body)
97
+ MultiJson.load(values['params'])['steps'].must_equal({'thumbs'=>{'robot'=>'/video/thumbs'}})
98
+ end
99
+ end
100
+
101
+ it 'must allow steps passed through the create! method override steps previously set' do
102
+ @assembly.create!({ :steps => @transloadit.step('resize', '/image/resize') })
103
+
104
+ assert_requested(:post, 'api2.transloadit.com/assemblies') do |req|
105
+ values = values_from_post_body(req.body)
106
+ MultiJson.load(values['params'])['steps'].must_equal({"resize"=>{'robot'=>'/image/resize'}})
107
+ end
108
+ end
109
+ end
110
+
111
+ describe 'when using the "submit!" method' do
112
+
113
+ it 'must call the create! method with the same parameters' do
114
+ VCR.use_cassette 'submit_assembly' do
115
+ file = open('lib/transloadit/version.rb')
116
+ mocker = MiniTest::Mock.new
117
+ mocker.expect :call, nil, [file]
118
+ @assembly.stub :create!, mocker do
119
+ @assembly.submit!(file)
120
+ end
121
+ mocker.verify
122
+ end
123
+ end
124
+ end
125
+
126
+ describe 'when rate limit is reached' do
127
+
128
+ it 'must output a warning and retry for a successful request' do
129
+ VCR.use_cassette 'rate_limit_succeed' do
130
+ _, warning = capture_io do
131
+ response = @assembly.create! open('lib/transloadit/version.rb')
132
+ response['ok'].must_equal 'ASSEMBLY_COMPLETED'
133
+ end
134
+ warning.must_equal "Rate limit reached. Waiting for 0 seconds before retrying.\n"
135
+ end
136
+ end
137
+
138
+ it 'must retry only the number of times specified' do
139
+ @assembly.options[:tries] = 1
140
+
141
+ VCR.use_cassette 'rate_limit_succeed' do
142
+ assert_raises Transloadit::Exception::RateLimitReached do
143
+ response = @assembly.create! open('lib/transloadit/version.rb')
144
+ end
145
+ end
146
+ end
147
+
148
+ it 'must raise RateLimitReached exception after multiple retries request' do
149
+ VCR.use_cassette 'rate_limit_fail' do
150
+ assert_raises Transloadit::Exception::RateLimitReached do
151
+ response = @assembly.create! open('lib/transloadit/version.rb')
108
152
  end
109
153
  end
110
154
  end
@@ -125,8 +169,95 @@ describe Transloadit::Assembly do
125
169
  @assembly.to_hash[:steps].keys.must_include @thumbs.name
126
170
  end
127
171
  end
128
- end
129
172
 
130
- def values_from_post_body(body)
131
- Addressable::URI.parse('?' + URI.decode(body)).query_values
173
+ describe 'using assembly API methods' do
174
+ include WebMock::API
175
+
176
+ before do
177
+ WebMock.reset!
178
+ @assembly = Transloadit::Assembly.new @transloadit
179
+ end
180
+
181
+ describe 'when fetching all assemblies' do
182
+
183
+ it 'must perform GET request to /assemblies' do
184
+ stub = stub_request(:get, 'api2.transloadit.com/assemblies?params=%7B%22auth%22:%7B%22key%22:%22%22%7D%7D')
185
+ @assembly.list
186
+
187
+ assert_requested(stub)
188
+ end
189
+
190
+ it 'must return a list of items' do
191
+ VCR.use_cassette 'fetch_assemblies' do
192
+ response = @assembly.list
193
+
194
+ response['items'].must_equal []
195
+ response['count'].must_equal 0
196
+ end
197
+ end
198
+ end
199
+
200
+ describe 'when fetching single assembly' do
201
+
202
+ it 'must perform GET request to /assemblies/[id]' do
203
+ stub = stub_request(:get, 'api2.transloadit.com/assemblies/76fe5df1c93a0a530f3e583805cf98b4')
204
+ @assembly.get '76fe5df1c93a0a530f3e583805cf98b4'
205
+
206
+ assert_requested(stub)
207
+ end
208
+
209
+ it 'must get assembly with specified id' do
210
+ VCR.use_cassette 'fetch_assembly_ok' do
211
+ response = @assembly.get '76fe5df1c93a0a530f3e583805cf98b4'
212
+ response['assembly_id'].must_equal '76fe5df1c93a0a530f3e583805cf98b4'
213
+ end
214
+ end
215
+ end
216
+
217
+ describe 'when fetching assembly notifications' do
218
+
219
+ it 'must perform GET request to /assembly_notifications' do
220
+ stub = stub_request(
221
+ :get,
222
+ 'api2.transloadit.com/assembly_notifications?params=%7B%22auth%22:%7B%22key%22:%22%22%7D%7D'
223
+ )
224
+ @assembly.get_notifications
225
+
226
+ assert_requested(stub)
227
+ end
228
+
229
+ it 'must return a list of items' do
230
+ VCR.use_cassette 'fetch_assembly_notifications' do
231
+ response = @assembly.get_notifications
232
+
233
+ response['items'].must_equal []
234
+ response['count'].must_equal 0
235
+ end
236
+ end
237
+ end
238
+
239
+ describe 'when replaying assembly' do
240
+
241
+ it 'must perform POST request to assemblies/[id]/replay' do
242
+ VCR.use_cassette 'replay_assembly' do
243
+ response = @assembly.replay '55c965a063a311e6ba2d379ef10b28f7'
244
+
245
+ response['ok'].must_equal 'ASSEMBLY_REPLAYING'
246
+ response['assembly_id'].must_equal 'b8590300650211e6b826d727b2cfd9ce'
247
+ end
248
+ end
249
+ end
250
+
251
+ describe 'when replaying assembly notification' do
252
+
253
+ it 'must replay notification of sepcified assembly' do
254
+ VCR.use_cassette 'replay_assembly_notification' do
255
+ response = @assembly.replay_notification '2ea5d21063ad11e6bc93e53395ce4e7d'
256
+
257
+ response['ok'].must_equal 'ASSEMBLY_NOTIFICATION_REPLAYED'
258
+ response['assembly_id'].must_equal '2ea5d21063ad11e6bc93e53395ce4e7d'
259
+ end
260
+ end
261
+ end
262
+ end
132
263
  end