encoder-rb 0.0.3

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.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in encoding-rb.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ encoding-rb
2
+
3
+ Encoding.com ruby wrapper
4
+
5
+ Copyright (c) 2012 Big Bang Technology Inc. Under the MIT License
6
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :unit
6
+
7
+ desc 'Test the ruby_encoding_wrapper plugin.'
8
+ RSpec::Core::RakeTask.new('unit') do |t|
9
+ t.pattern = 'spec/*/{*_spec.rb}'
10
+ end
11
+
data/encoder.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "encoder/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "encoder-rb"
7
+ s.version = Encoder::VERSION
8
+ s.authors = ["Big Bang Technology Inc."]
9
+ s.email = ["cameron@bigbangtechnology.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Ruby Client Library for Encoding.com}
12
+
13
+ s.rubyforge_project = "encoder-rb"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "fakeweb"
23
+
24
+ s.add_runtime_dependency "builder"
25
+ s.add_runtime_dependency "nokogiri"
26
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,3 @@
1
+ module Encoder
2
+ VERSION = "0.0.3"
3
+ end
data/lib/encoder.rb ADDED
@@ -0,0 +1,159 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'builder'
6
+ require 'nokogiri'
7
+ require 'encoder/version'
8
+
9
+ module EncodingActions
10
+ ADD_MEDIA = "AddMedia"
11
+ GET_STATUS = "GetStatus"
12
+ CANCEL_MEDIA = "CancelMedia"
13
+ end
14
+
15
+ module EncodingStatusType
16
+ NEW = "New"
17
+ WAITING = "Waiting for encoder"
18
+ PROCESSING = "Processing"
19
+ SAVING = "Saving"
20
+ FINISHED = "Finished"
21
+ ERROR = "Error"
22
+ end
23
+
24
+ module ErrorMessage
25
+ AUTHENTICATION = "Wrong user id or key!"
26
+ NO_FORMATS = "No formats specified!"
27
+ NO_SOURCE = "Source file is not indicated!"
28
+ end
29
+
30
+ module RequestResponse
31
+ ERROR = nil
32
+ end
33
+
34
+ module Encoder
35
+ def self.new
36
+ Client.new
37
+ end
38
+
39
+ class Client
40
+ attr_reader :user_id, :user_key, :url, :last_error
41
+
42
+ def initialize(user_id=nil, user_key=nil)
43
+ @user_id = user_id
44
+ @user_key = user_key
45
+ @url = 'http://manage.encoding.com/'
46
+ end
47
+
48
+ def request_encoding(source=nil, notify_url=nil)
49
+ #{ :size, :bitrate, :audio_bitrate, :audio_sample_rate,
50
+ #:audio_channels_number, :framerate, :two_pass, :cbr,
51
+ #:deinterlacing, :destination, :add_meta
52
+
53
+ xml = Builder::XmlMarkup.new :indent=>2
54
+ xml.instruct!
55
+ xml.query do |q|
56
+ q.userid @user_id
57
+ q.userkey @user_key
58
+ q.action EncodingActions::ADD_MEDIA
59
+ q.source source
60
+ q.notify notify_url
61
+
62
+ yield q
63
+ end
64
+
65
+ response = request_send(xml.target!)
66
+ return RequestResponse::ERROR if request_error?(response)
67
+
68
+ document = Nokogiri::XML(response.body)
69
+ return RequestResponse::ERROR if api_error?(document)
70
+
71
+ document.css("MediaID").text.to_i
72
+ end
73
+
74
+ def request_status(media_id)
75
+ xml = Builder::XmlMarkup.new :indent=>2
76
+ xml.instruct!
77
+ xml.query do |q|
78
+ q.userid @user_id
79
+ q.userkey @user_key
80
+ q.action EncodingActions::GET_STATUS
81
+ q.mediaid media_id
82
+ end
83
+
84
+ response = request_send(xml.target!)
85
+ return RequestResponse::ERROR if request_error?(response)
86
+
87
+ document = Nokogiri::XML(response.body)
88
+ return RequestResponse::ERROR if api_error?(document)
89
+
90
+ status = document.css("response > status").text
91
+ progress = document.css("progress").text.to_i
92
+
93
+ # there is a bug where the progress reports
94
+ # as 100% if the status is 'Waiting for encoder'
95
+ if (status == EncodingStatusType::WAITING)
96
+ progress = 0
97
+ end
98
+
99
+ status = {
100
+ :message => status,
101
+ :progress => progress
102
+ }
103
+ end
104
+
105
+ def cancel_media(media_id)
106
+ xml = Builder::XmlMarkup.new :indent => 2
107
+ xml.instruct!
108
+ xml.query do |q|
109
+ q.userid @user_id
110
+ q.userkey @user_key
111
+ q.action EncodingActions::CANCEL_MEDIA
112
+ q.mediaid media_id
113
+ end
114
+
115
+ response = request_send(xml.target!)
116
+ return RequestResponse::ERROR if request_error?(response)
117
+
118
+ document = Nokogiri::XML(response.body)
119
+ return RequestResponse::ERROR if api_error?(document)
120
+
121
+ true
122
+
123
+ end
124
+
125
+
126
+ private
127
+
128
+ def request_error?(response)
129
+ if response.code =~ /(4|5)\d+/
130
+ @last_error = response.message
131
+ true
132
+ else
133
+ false
134
+ end
135
+ end
136
+
137
+ def api_error?(document)
138
+ if document.css('errors error').length > 0
139
+ @last_error = document.css('errors error').text
140
+ true
141
+ else
142
+ false
143
+ end
144
+ end
145
+
146
+ def request_send(xml)
147
+ url = URI.parse(@url)
148
+ request = Net::HTTP::Post.new(url.path)
149
+ request.form_data = { :xml => xml }
150
+
151
+ Net::HTTP.new(url.host, url.port).start { |http|
152
+ http.request(request)
153
+ }
154
+ end
155
+ end
156
+ end
157
+
158
+
159
+
data/spec/.DS_Store ADDED
Binary file
@@ -0,0 +1,12 @@
1
+ dir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift "#{dir}/../lib"
3
+
4
+ require 'fakeweb'
5
+ require 'encoder'
6
+
7
+ FakeWeb.allow_net_connect = false
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ end
@@ -0,0 +1,254 @@
1
+ require 'spec_helper'
2
+
3
+ def fake_error(message)
4
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><errors><error>#{message}</error></errors></response>")
5
+ end
6
+
7
+ describe Encoder do
8
+
9
+ before do
10
+ FakeWeb.clean_registry
11
+ @sut = Encoder.new
12
+ end
13
+
14
+ describe "#request_encoding" do
15
+
16
+ describe "invalid user id or key" do
17
+ before do
18
+ fake_error(ErrorMessage::AUTHENTICATION)
19
+ @result = @sut.request_encoding {|query|}
20
+ end
21
+ it 'should be nil' do
22
+ @result.should be_nil
23
+ end
24
+ it 'should have "Wrong user id or key" as an error' do
25
+ @sut.last_error.should =~ /#{ErrorMessage::AUTHENTICATION}/
26
+ end
27
+ end
28
+
29
+ describe "the server returns a 404 error code" do
30
+ before do
31
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['404', 'Not Found'])
32
+ @result = @sut.request_encoding { |query| }
33
+ end
34
+
35
+ it 'should return nil' do
36
+ @result.should be_nil
37
+ end
38
+
39
+ it 'should have "Not Found" as an error' do
40
+ @sut.last_error.should == 'Not Found'
41
+ end
42
+
43
+ end
44
+
45
+ describe "the server returns a 500 error code" do
46
+ before do
47
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['500', 'Server Error'])
48
+ @result = @sut.request_encoding { |query| }
49
+ end
50
+
51
+ it 'should return nil' do
52
+ @result.should be_nil
53
+ end
54
+
55
+ it 'should have "Server Error" as an error' do
56
+ @sut.last_error.should == 'Server Error'
57
+ end
58
+
59
+ end
60
+
61
+ describe "no specified format(s)" do
62
+ before do
63
+ fake_error(ErrorMessage::NO_FORMATS)
64
+ @result = @sut.request_encoding {|query|}
65
+ end
66
+
67
+ it "should have 'No formats specified' as an error" do
68
+ @sut.last_error.should =~ /#{ErrorMessage::NO_FORMATS}/
69
+ end
70
+ end
71
+
72
+ describe "no source specified" do
73
+ before do
74
+ fake_error(ErrorMessage::NO_SOURCE)
75
+ @result = @sut.request_encoding do |query|
76
+ query.format do |f|
77
+ f.output('iphone')
78
+ end
79
+ end
80
+ end
81
+
82
+ it 'should have "Source file is not indicated" as an error' do
83
+ @sut.last_error.should =~ /#{ErrorMessage::NO_SOURCE}/
84
+ end
85
+
86
+ end
87
+
88
+ describe "valid encoding request" do
89
+ before do
90
+ @media_id = 1234
91
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><message>Added</message><MediaID>#{@media_id}</MediaID></response>")
92
+ @result = @sut.request_encoding { |query| }
93
+ end
94
+ it 'should return a MediaID' do
95
+ @result.should == @media_id
96
+ end
97
+ end
98
+
99
+
100
+ end
101
+
102
+ describe "#request_status" do
103
+ describe "invalid user id or key" do
104
+ before do
105
+ fake_error("Wrong user id or key!")
106
+ @result = @sut.request_status(1234)
107
+ end
108
+ it 'should be nil' do
109
+ @result.should be_nil
110
+ end
111
+ it 'should have "Wrong user id or key" as an error' do
112
+ @sut.last_error.should =~ /Wrong user id or key/
113
+ end
114
+ end
115
+
116
+ describe "the server returns a 404 error code" do
117
+ before do
118
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['404', 'Not Found'])
119
+ @result = @sut.request_status(1234)
120
+ end
121
+
122
+ it 'should return nil' do
123
+ @result.should be_nil
124
+ end
125
+
126
+ it 'should have "Not Found" as an error' do
127
+ @sut.last_error.should == 'Not Found'
128
+ end
129
+
130
+ end
131
+
132
+ describe "the server returns a 500 error code" do
133
+ before do
134
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['500', 'Server Error'])
135
+ @result = @sut.request_status(1234)
136
+ end
137
+
138
+ it 'should return nil' do
139
+ @result.should be_nil
140
+ end
141
+
142
+ it 'should have "Server Error" as an error' do
143
+ @sut.last_error.should == 'Server Error'
144
+ end
145
+
146
+ end
147
+
148
+ describe "valid status request" do
149
+ before do
150
+ @media_id = 1234
151
+ @progress = 10
152
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><id>#{@media_id}</id><status>#{EncodingStatusType::PROCESSING}</status><progress>#{@progress}</progress></response>")
153
+ @result = @sut.request_status(@media_id)
154
+ end
155
+
156
+ it 'should return exactly "Processing"' do
157
+ @result[ :message ].should == EncodingStatusType::PROCESSING
158
+ end
159
+
160
+ it 'should return 10% progress' do
161
+ @result[ :progress ].should == @progress
162
+ end
163
+
164
+ end
165
+
166
+ describe "waiting for encoder" do
167
+ before do
168
+ @media_id = 1234
169
+ @progress = 100
170
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><id>#{@media_id}</id><status>#{EncodingStatusType::WAITING}</status><progress>#{@progress}</progress></response>")
171
+ @result = @sut.request_status(@media_id)
172
+ end
173
+ it 'should have 0% progress' do
174
+ @result[ :progress ].should be 0
175
+ end
176
+ end
177
+
178
+ describe "finished encoding with multiple formats" do
179
+ before do
180
+ @media_id = 1234
181
+ @progress = 100
182
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><id>#{@media_id}</id><status>#{EncodingStatusType::FINISHED}</status><progress>#{@progress}</progress><format><status>#{EncodingStatusType::FINISHED}</status></format><format><status>#{EncodingStatusType::FINISHED}</status></format></response>")
183
+ @result = @sut.request_status(@media_id)
184
+ end
185
+
186
+ it "should have a status of exactly Finished" do
187
+ @result[ :message ].should == EncodingStatusType::FINISHED
188
+ end
189
+ end
190
+
191
+ end
192
+
193
+ describe "#cancel_media" do
194
+
195
+ describe "invalid user id or key" do
196
+ before do
197
+ fake_error(ErrorMessage::AUTHENTICATION)
198
+ @result = @sut.cancel_media(1234)
199
+ end
200
+ it 'should be nil' do
201
+ @result.should be_nil
202
+ end
203
+ it 'should have "Wrong user id or key" as an error' do
204
+ @sut.last_error.should =~ /#{ErrorMessage::AUTHENTICATION}/
205
+ end
206
+ end
207
+
208
+ describe "the server returns a 404 error code" do
209
+ before do
210
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['404', 'Not Found'])
211
+ @result = @sut.cancel_media(1234)
212
+ end
213
+
214
+ it 'should return nil' do
215
+ @result.should be_nil
216
+ end
217
+
218
+ it 'should have "Not Found" as an error' do
219
+ @sut.last_error.should == 'Not Found'
220
+ end
221
+
222
+ end
223
+
224
+ describe "the server returns a 500 error code" do
225
+ before do
226
+ FakeWeb.register_uri(:post, "http://manage.encoding.com/", :body => '', :status => ['500', 'Server Error'])
227
+ @result = @sut.cancel_media(1234)
228
+ end
229
+
230
+ it 'should return nil' do
231
+ @result.should be_nil
232
+ end
233
+
234
+ it 'should have "Server Error" as an error' do
235
+ @sut.last_error.should == 'Server Error'
236
+ end
237
+
238
+ end
239
+
240
+ describe "valid cancel request" do
241
+ before do
242
+ @media_id = 1234
243
+ FakeWeb.register_uri(:post, 'http://manage.encoding.com/', :body => "<?xml version=\"1.0\"?><response><message>Deleted</message></response></response>")
244
+ @result = @sut.cancel_media(@media_id)
245
+ end
246
+
247
+ it 'should return truthy' do
248
+ @result.should be true
249
+ end
250
+ end
251
+
252
+ end
253
+
254
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encoder-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Big Bang Technology Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70119603624500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70119603624500
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70119603623840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70119603623840
36
+ - !ruby/object:Gem::Dependency
37
+ name: fakeweb
38
+ requirement: &70119603623080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70119603623080
47
+ - !ruby/object:Gem::Dependency
48
+ name: builder
49
+ requirement: &70119603622220 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70119603622220
58
+ - !ruby/object:Gem::Dependency
59
+ name: nokogiri
60
+ requirement: &70119603621440 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70119603621440
69
+ description:
70
+ email:
71
+ - cameron@bigbangtechnology.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .DS_Store
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - MIT-LICENSE
81
+ - README
82
+ - Rakefile
83
+ - encoder.gemspec
84
+ - lib/.DS_Store
85
+ - lib/encoder.rb
86
+ - lib/encoder/version.rb
87
+ - spec/.DS_Store
88
+ - spec/spec_helper.rb
89
+ - spec/unit/encoder_spec.rb
90
+ homepage: ''
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project: encoder-rb
110
+ rubygems_version: 1.8.10
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Ruby Client Library for Encoding.com
114
+ test_files:
115
+ - spec/spec_helper.rb
116
+ - spec/unit/encoder_spec.rb
117
+ has_rdoc: