uencode 0.0.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +57 -31
- data/lib/uencode/elements.rb +159 -115
- data/lib/uencode/request.rb +21 -11
- data/lib/uencode/response.rb +20 -14
- data/lib/uencode/version.rb +3 -1
- data/lib/uencode.rb +2 -0
- data/uencode.gemspec +4 -12
- metadata +72 -156
- data/spec/elements_spec.rb +0 -532
- data/spec/fixtures/configuration.yaml +0 -23
- data/spec/fixtures/job_with_one_video_and_no_capture.yml +0 -87
- data/spec/request_spec.rb +0 -53
- data/spec/response_spec.rb +0 -48
- data/spec/spec_helper.rb +0 -16
- data/spec/uencode_spec.rb +0 -14
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# UEncode
|
2
2
|
|
3
|
-
A
|
3
|
+
A Ruby gem to interact with the [uEncode](http://www.uencode.com) API. This extends the work of [Cassio Marques](https://github.com/cassiomarques/).
|
4
4
|
|
5
|
-
##
|
5
|
+
## Creating a job
|
6
6
|
|
7
7
|
``` ruby
|
8
8
|
require 'uencode'
|
@@ -12,53 +12,79 @@ UEncode.configure do |c|
|
|
12
12
|
c.customer_key = "your_uencode_api_key"
|
13
13
|
end
|
14
14
|
|
15
|
-
job = UEncode::Job.new :source => "http://your_source_video_url/foo.avi", :userdata => "This is a simple test"
|
16
15
|
|
17
|
-
job
|
16
|
+
#create job and set source video
|
17
|
+
job = UEncode::Job.new :userdata => "AnyDataUsefulToYou", :callback => "Your Email Address or Callack Url"
|
18
|
+
job.source_video = UEncode::Source.new :url => "http://your.source.url/foo.flv", :timeout => 600
|
18
19
|
|
19
|
-
video
|
20
|
+
#create configuration for a video output
|
21
|
+
configuration = {:video => {:bitrate => 1000, :codec => 'h264', :profile => 'baseline', :size => {:width => 640, :height => 480}, :fix_rotation => true, :force_sqare_pixels => true}}
|
22
|
+
configuration[:audio] = {:codec => "aac", :bitrate => 128000, :channels => 2, :samplerate => 36000}
|
20
23
|
|
21
|
-
video
|
24
|
+
#create video output and set configuration created above
|
25
|
+
out = UEncode::VideoOutput.new :container => 'mpeg4'
|
26
|
+
out.streams.configure configuration
|
22
27
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
c.samplerate = 44100
|
27
|
-
c.channels = 1
|
28
|
-
end
|
29
|
-
|
30
|
-
job << video
|
28
|
+
#add a destination to the output and add output to the job object
|
29
|
+
out.add_destination UEncode::Destination.new :url => "http://your.desstination.url/foo.mp4", :timeout => 600
|
30
|
+
job.add_video(out)
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
#create a capture output
|
33
|
+
out = UEncode::CaptureOutput.new :rate=>'every 5s', :size=>{:width=>640, :height=>480}, :zip=>true, :prefix=>'frames'
|
34
|
+
out.add_destination UEncode::Destination.new :url=>'http://your.destination.url/capture.zip'
|
35
|
+
job.add_capture(out)
|
34
36
|
|
37
|
+
#create a request from the job and send to uEncode.
|
35
38
|
request = UEncode::Request.new job
|
36
|
-
|
37
39
|
response = request.send
|
38
40
|
|
39
|
-
|
40
|
-
puts response.
|
41
|
-
|
42
|
-
|
41
|
+
#response.code contains the status code returned by the server (200, 400, 406, 500, etc).
|
42
|
+
puts response.code
|
43
|
+
|
44
|
+
#response.data is a hash created from the xml returned by the server. Included is the key used to check status of the job.
|
45
|
+
puts response.data
|
43
46
|
```
|
44
47
|
|
45
|
-
##
|
48
|
+
## Checking job status
|
49
|
+
``` ruby
|
50
|
+
require 'uencode'
|
51
|
+
|
52
|
+
UEncode.configure do |c|
|
53
|
+
c.customer_key = "your_uencode_api_key"
|
54
|
+
end
|
55
|
+
|
56
|
+
#Get a list of jobs. Including the userdata parameter will return all jobs where the userdata matches the value you specified. Otherwise, all current active (not completed) jobs will be returned.
|
57
|
+
status = UEncode::JobStatus.new :userdata => 'AnyDataUsefulToYou'
|
58
|
+
request = UEncode::Request.new status
|
59
|
+
response = request.send
|
60
|
+
|
61
|
+
#Data is a hash created from the xml returned by the API. Consult the API docs for details.
|
62
|
+
puts response.code
|
63
|
+
puts response.data
|
64
|
+
|
65
|
+
#Get details, including progress of individual tasks, of a specific job.
|
66
|
+
status = UEncode::JobStatus.new :key => 'API job key'
|
67
|
+
request = UEncode::Request.new status
|
68
|
+
response = request.send
|
46
69
|
|
47
|
-
|
70
|
+
puts response.code
|
71
|
+
puts response.data
|
72
|
+
```
|
48
73
|
|
49
|
-
|
74
|
+
## Parameters
|
50
75
|
|
51
|
-
|
76
|
+
All version 300 uEncode API parameters are supported.
|
52
77
|
|
53
|
-
|
78
|
+
The gem does not validate input. Consult the [uEncode API documentation](http://www.uencode.com/api/300/introduction) for all parameters and their accepted values.
|
54
79
|
|
55
|
-
##
|
80
|
+
##License
|
56
81
|
|
57
|
-
This
|
82
|
+
This project is released under The MIT License
|
58
83
|
|
59
|
-
|
84
|
+
Copyright © 2011, uEncode, Cassio Marques
|
60
85
|
|
61
|
-
|
62
|
-
* rspec spec
|
86
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
63
87
|
|
88
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
64
89
|
|
90
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/uencode/elements.rb
CHANGED
@@ -1,30 +1,35 @@
|
|
1
|
+
# Copyright (c) 2011, uEncode, Cassio Marques
|
2
|
+
|
1
3
|
module UEncode
|
2
|
-
class Crop
|
3
|
-
ATTRIBUTES = [:width, :height, :x, :y]
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
module SizeBase
|
6
|
+
ATTRIBUTES = [:width, :height]
|
7
|
+
|
8
|
+
include AttrSetting
|
9
|
+
|
10
|
+
def to_xml
|
11
|
+
%Q{
|
12
|
+
<#{root_name}>
|
13
|
+
<width>#{width}</width>
|
14
|
+
<height>#{height}</height>
|
15
|
+
</#{root_name}>
|
16
|
+
}
|
17
|
+
end
|
14
18
|
end
|
15
|
-
|
19
|
+
|
16
20
|
|
17
21
|
class Size
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
include UEncode::SizeBase
|
23
|
+
|
24
|
+
private
|
25
|
+
def root_name; "size"; end
|
26
|
+
end
|
27
|
+
|
28
|
+
class MaxSize
|
29
|
+
include UEncode::SizeBase
|
30
|
+
|
31
|
+
private
|
32
|
+
def root_name; "max_size"; end
|
28
33
|
end
|
29
34
|
|
30
35
|
module RateElement
|
@@ -46,38 +51,75 @@ module UEncode
|
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
49
|
-
|
50
|
-
|
54
|
+
module Transfer
|
55
|
+
ATTRIBUTES = [:url, :authentication_type, :timeout, :public_read]
|
56
|
+
|
57
|
+
include AttrSetting
|
58
|
+
|
59
|
+
def to_xml
|
60
|
+
%Q{
|
61
|
+
<#{root_name}#{@authentication_type ? ' authentication_type="' + @authentication_type + '"' : ""}#{@timeout ? ' timeout="'+ @timeout.to_s + '"' : ""}#{@public_read ? ' public_read="' + @public_read.to_s + '"' : ""}>#{@url}</#{root_name}>
|
62
|
+
}
|
63
|
+
end
|
64
|
+
end
|
51
65
|
|
66
|
+
class Destination
|
67
|
+
include Transfer
|
68
|
+
|
52
69
|
private
|
53
|
-
def root_name; "
|
70
|
+
def root_name; "destination"; end
|
54
71
|
end
|
55
|
-
|
56
|
-
class
|
57
|
-
include
|
58
|
-
|
72
|
+
|
73
|
+
class Source
|
74
|
+
include Transfer
|
75
|
+
|
59
76
|
private
|
60
|
-
def root_name; "
|
77
|
+
def root_name; "source"; end
|
61
78
|
end
|
62
79
|
|
63
80
|
class CaptureOutput
|
64
|
-
ATTRIBUTES = [:
|
81
|
+
ATTRIBUTES = [:rate, :stretch, :crop, :zip, :prefix]
|
65
82
|
|
66
83
|
def initialize(options)
|
84
|
+
@destinations = []
|
67
85
|
super
|
68
|
-
@stretch = false if @stretch.nil?
|
69
86
|
end
|
70
87
|
|
88
|
+
def add_destination(dest)
|
89
|
+
@destinations << dest
|
90
|
+
end
|
91
|
+
|
92
|
+
def size=(_size)
|
93
|
+
_size = Size.new(_size) unless _size.instance_of?(Size) || _size.nil?
|
94
|
+
instance_variable_set :@size, _size
|
95
|
+
end
|
96
|
+
|
97
|
+
def size
|
98
|
+
@size
|
99
|
+
end
|
100
|
+
|
101
|
+
def max_size=(_max_size)
|
102
|
+
_max_size = MaxSize.new(_max_size) unless _max_size.instance_of?(MaxSize) || _max_size.nil?
|
103
|
+
instance_variable_set :@max_size, _max_size
|
104
|
+
end
|
105
|
+
|
106
|
+
def max_size
|
107
|
+
@max_size
|
108
|
+
end
|
109
|
+
|
110
|
+
|
71
111
|
def to_xml
|
72
112
|
%Q{
|
73
|
-
<
|
74
|
-
<capture>
|
113
|
+
<capture>
|
75
114
|
<rate>#{rate}</rate>
|
76
|
-
<
|
77
|
-
|
115
|
+
#{!zip.nil? ? '<zip>' + zip.to_s + '</zip>' : ""}
|
116
|
+
#{!prefix.nil? ? '<prefix>' + prefix.to_s + '</prefix>' : ""}
|
117
|
+
<destinations>
|
118
|
+
#{@destinations.inject("") { |s, dest| s << dest.to_xml}}
|
119
|
+
</destinations>
|
78
120
|
#{@size ? @size.to_xml : ""}
|
121
|
+
#{@max_size ? @max_size.to_xml : ""}
|
79
122
|
</capture>
|
80
|
-
<//output>
|
81
123
|
}
|
82
124
|
end
|
83
125
|
end
|
@@ -87,36 +129,38 @@ module UEncode
|
|
87
129
|
|
88
130
|
include Enumerable
|
89
131
|
|
90
|
-
|
91
|
-
|
92
|
-
attr_writer :destination, :container
|
132
|
+
attr_writer :container
|
133
|
+
attr_accessor :streams
|
93
134
|
|
94
135
|
def initialize(options)
|
95
|
-
@
|
136
|
+
@streams = VideoStreams.new
|
137
|
+
@destinations = []
|
96
138
|
super
|
97
139
|
end
|
98
140
|
|
99
141
|
def each
|
100
142
|
@items.each { |item| yield item }
|
101
143
|
end
|
144
|
+
|
145
|
+
def add_destination(dest)
|
146
|
+
@destinations << dest
|
147
|
+
end
|
102
148
|
|
103
149
|
def to_xml
|
104
150
|
%Q{
|
105
|
-
<output>
|
106
151
|
<video>
|
107
|
-
<
|
152
|
+
<destinations>
|
153
|
+
#{@destinations.inject("") { |s, dest| s << dest.to_xml}}
|
154
|
+
</destinations>
|
108
155
|
<container>#{container}</container>
|
109
|
-
|
110
|
-
#{@items.inject("") { |s, item| s << item.to_xml }}
|
111
|
-
</media>
|
156
|
+
#{@streams.to_xml}
|
112
157
|
</video>
|
113
|
-
</output>
|
114
158
|
}
|
115
159
|
end
|
116
160
|
end
|
117
161
|
|
118
|
-
#
|
119
|
-
class
|
162
|
+
# VideoStreams represents the audio and video streams for a single output
|
163
|
+
class VideoStreams
|
120
164
|
attr_reader :video_config, :audio_config
|
121
165
|
|
122
166
|
def initialize
|
@@ -126,28 +170,28 @@ module UEncode
|
|
126
170
|
|
127
171
|
# Configures the transcoding using a nested hash with the following format:
|
128
172
|
#
|
129
|
-
# config = {
|
173
|
+
# config = {:video => { ... }, :audio => { ... }
|
130
174
|
#
|
131
175
|
# The keys for the "video" hash can be any of the following: bitrate, codec, cbr, crop,
|
132
176
|
# deinterlace, framerate, height, keyframe_interval, maxbitrate, par, profile, passes,
|
133
177
|
# stretch, width.
|
134
178
|
#
|
135
|
-
# The
|
136
|
-
#
|
137
|
-
# {"numerator" => 10, "denominator" => 11}
|
138
|
-
#
|
139
|
-
# The keys for the "audio" hash can be any of the following:
|
179
|
+
# The keys for the :audio hash can be any of the following:
|
140
180
|
# codec, bitrate, channels, samplerate.
|
141
181
|
#
|
142
182
|
def configure(hash)
|
143
|
-
video = hash[
|
144
|
-
audio = hash[
|
145
|
-
|
146
|
-
|
183
|
+
video = hash[:video]
|
184
|
+
audio = hash[:audio]
|
185
|
+
if !video.nil?
|
186
|
+
configure_video do |c|
|
187
|
+
video.each_pair { |key, value| c.send("#{key}=", value) }
|
188
|
+
end
|
147
189
|
end
|
148
190
|
|
149
|
-
|
150
|
-
|
191
|
+
if !audio.nil?
|
192
|
+
configure_audio do |c|
|
193
|
+
audio.each_pair { |key, value| c.send("#{key}=", value) }
|
194
|
+
end
|
151
195
|
end
|
152
196
|
end
|
153
197
|
|
@@ -169,114 +213,114 @@ module UEncode
|
|
169
213
|
|
170
214
|
def to_xml
|
171
215
|
%Q{
|
172
|
-
<medium>
|
173
216
|
<video>
|
174
|
-
<bitrate
|
175
|
-
<codec
|
176
|
-
#{!video.
|
177
|
-
#{video.
|
178
|
-
#{video.
|
179
|
-
#{
|
180
|
-
#{
|
181
|
-
#{video.keyframe_interval.nil? ? "" : '<keyframe_interval>' + video.keyframe_interval.to_s + '</keyframe_interval>'}
|
182
|
-
#{video.maxbitrate.nil? ? "" : '<maxbitrate>' + video.maxbitrate.to_s + '</maxbitrate>'}
|
183
|
-
#{video.par ? video.par.to_xml : ""}
|
184
|
-
#{video.profile.nil? ? "" : '<profile>' + video.profile + '</profile>'}
|
217
|
+
#{!video.bitrate.nil? ? '<bitrate>' + video.bitrate.to_s + '</bitrate>' : ""}
|
218
|
+
#{!video.codec.nil? ? '<codec>' + video.codec + '</codec>' : ""}
|
219
|
+
#{!video.profile.nil? ? '<profile>' + video.profile + '</profile>' : ""}
|
220
|
+
#{!video.quality.nil? ? '<quality>' + video.quality.to_s + '</quality>' : ""}
|
221
|
+
#{!video.framerate.nil? ? '<framerate>' + video.framerate.to_s + '</framerate>' : ""}
|
222
|
+
#{!@video_config.size.nil? ? @video_config.size.to_xml : ""}
|
223
|
+
#{@video_config.max_size ? @video_config.max_size.to_xml : ""}
|
185
224
|
#{video.passes.nil? ? "" : '<passes>' + video.passes.to_s + '</passes>'}
|
186
|
-
#{
|
187
|
-
#{video.
|
225
|
+
#{video.deinterlace.nil? ? "" : '<deinterlace>' + video.deinterlace.to_s + '</deinterlace>'}
|
226
|
+
#{video.fix_rotation.nil? ? "" : '<fix_rotation>' + video.fix_rotation.to_s + '</fix_rotation>'}
|
227
|
+
#{video.force_square_pixels.nil? ? "" : '<force_square_pixels>' + video.force_square_pixels.to_s + '</force_square_pixels>'}
|
188
228
|
</video>
|
189
229
|
<audio>
|
190
230
|
#{audio.codec.nil? ? "" : '<codec>' + audio.codec + '</codec>'}
|
191
231
|
#{audio.bitrate.nil? ? "" : '<bitrate>' + audio.bitrate.to_s + '</bitrate>'}
|
192
232
|
#{audio.channels.nil? ? "" : '<channels>' + audio.channels.to_s + '</channels>'}
|
193
233
|
#{audio.samplerate.nil? ? "" : '<samplerate>' + audio.samplerate.to_s + '</samplerate>'}
|
234
|
+
#{audio.quality.nil? ? "" : '<quality>' + video.quality.to_s + '</quality>'}
|
194
235
|
</audio>
|
195
|
-
</medium>
|
196
236
|
}
|
197
237
|
end
|
198
238
|
end
|
199
239
|
|
200
|
-
# The video configs for each
|
240
|
+
# The video configs for each VideoStream
|
201
241
|
class VideoConfig
|
202
|
-
attr_accessor :bitrate, :codec, :
|
203
|
-
:maxbitrate, :par, :profile, :passes, :stretch, :width
|
242
|
+
attr_accessor :bitrate, :codec, :profile, :quality, :framerate, :passes, :deinterlace, :fix_rotation, :force_square_pixels
|
204
243
|
|
205
244
|
def initialize
|
206
|
-
@cbr = false
|
207
245
|
@deinterlace = false
|
208
246
|
@profile = "main"
|
209
247
|
@passes = 1
|
210
|
-
@stretch = false
|
211
248
|
end
|
212
249
|
|
213
|
-
def
|
214
|
-
|
215
|
-
instance_variable_set :@
|
250
|
+
def size=(_size)
|
251
|
+
_size = Size.new(_size) unless _size.instance_of?(Size) || _size.nil?
|
252
|
+
instance_variable_set :@size, _size
|
216
253
|
end
|
217
|
-
|
218
|
-
def
|
219
|
-
|
220
|
-
|
254
|
+
|
255
|
+
def size
|
256
|
+
@size
|
257
|
+
end
|
258
|
+
|
259
|
+
def max_size=(_max_size)
|
260
|
+
_max_size = MaxSize.new(_max_size) unless _max_size.instance_of?(MaxSize) || _max_size.nil?
|
261
|
+
instance_variable_set :@max_size, _max_size
|
262
|
+
end
|
263
|
+
|
264
|
+
def max_size
|
265
|
+
@max_size
|
221
266
|
end
|
222
267
|
end
|
223
268
|
|
224
|
-
# The audio configs for each
|
269
|
+
# The audio configs for each VideoStream
|
225
270
|
class AudioConfig
|
226
|
-
attr_accessor :codec, :bitrate, :channels, :samplerate
|
271
|
+
attr_accessor :codec, :bitrate, :channels, :samplerate, :quality
|
272
|
+
end
|
273
|
+
|
274
|
+
class JobStatus
|
275
|
+
ATTRIBUTES = [:key, :userdata, :customerkey]
|
276
|
+
|
277
|
+
include AttrSetting
|
227
278
|
end
|
228
279
|
|
229
280
|
class Job
|
230
|
-
ATTRIBUTES = [:
|
281
|
+
ATTRIBUTES = [:userdata, :callback, :customerkey]
|
231
282
|
|
232
|
-
include
|
283
|
+
include AttrSetting
|
233
284
|
|
234
|
-
def
|
235
|
-
new(
|
285
|
+
def source_video=(_source)
|
286
|
+
_source = Source.new(_source) unless _source.instance_of?(Source) || _source.nil?
|
287
|
+
instance_variable_set :@source_video, _source
|
288
|
+
end
|
289
|
+
|
290
|
+
def source_video
|
291
|
+
@source_video
|
236
292
|
end
|
237
293
|
|
238
294
|
def initialize(options)
|
239
|
-
@
|
295
|
+
@video_outputs = []
|
240
296
|
@captures = []
|
297
|
+
source_video = options[:source_video]
|
241
298
|
super
|
242
299
|
end
|
243
300
|
|
244
|
-
def
|
245
|
-
|
246
|
-
end
|
247
|
-
|
248
|
-
def items
|
249
|
-
@video_output.items
|
250
|
-
end
|
251
|
-
|
252
|
-
def <<(item)
|
253
|
-
@video_output.items << item
|
301
|
+
def add_video(video)
|
302
|
+
@video_outputs << video
|
254
303
|
end
|
255
304
|
|
256
305
|
def add_capture(capture)
|
257
306
|
@captures << capture
|
258
307
|
end
|
259
308
|
|
260
|
-
def each(&block)
|
261
|
-
@video_output.each &block
|
262
|
-
end
|
263
|
-
|
264
309
|
def to_xml
|
265
310
|
xml = %Q{
|
266
311
|
<job>
|
267
|
-
|
268
|
-
<source>#{source}</source>
|
312
|
+
#{source_video.nil? ? "" : source_video.to_xml}
|
269
313
|
#{userdata.nil? ? "" : '<userdata>' + userdata + '</userdata>'}
|
270
|
-
#{
|
314
|
+
#{callback.nil? ? "" : '<callback>' + callback + '</callback>'}
|
271
315
|
<outputs>
|
272
|
-
#{@
|
316
|
+
#{@video_outputs.inject("") { |s, vid| s << vid.to_xml}}
|
273
317
|
#{@captures.inject("") { |s, cap| s << cap.to_xml }}
|
274
318
|
</outputs>
|
275
319
|
</job>
|
276
320
|
}
|
277
|
-
|
321
|
+
xml.gsub(/\n?/, "").gsub(/>\s+</, "><")
|
278
322
|
end
|
279
323
|
end
|
280
324
|
|
281
|
-
[Size,
|
325
|
+
[SizeBase, Size, MaxSize, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting }
|
282
326
|
end
|
data/lib/uencode/request.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
# Copyright (c) 2011, uEncode, Cassio Marques
|
2
|
+
|
1
3
|
module UEncode
|
2
4
|
class Request
|
5
|
+
|
3
6
|
include HTTParty
|
7
|
+
require 'rubygems'
|
8
|
+
require 'crack'
|
4
9
|
|
5
|
-
base_uri "https://www.uencode.com"
|
6
10
|
format :xml
|
7
11
|
|
8
12
|
def initialize(job)
|
@@ -10,23 +14,29 @@ module UEncode
|
|
10
14
|
end
|
11
15
|
|
12
16
|
def send
|
13
|
-
|
17
|
+
url_base = "https://api.uencode.com/300/jobs"
|
18
|
+
key = @job.customerkey || UEncode.customer_key
|
19
|
+
if @job.instance_of?(Job)
|
20
|
+
response = self.class.post url_base, :body => @job.to_xml, :headers=>{"Authorization"=>key, "Accept"=>"text/xml"}
|
21
|
+
elsif !@job.key.nil?
|
22
|
+
response = self.class.get url_base + "/" + @job.key, :headers=>{"Authorization"=>key, "Accept"=>"text/xml"}
|
23
|
+
else
|
24
|
+
url = url_base
|
25
|
+
if !@job.userdata.nil?
|
26
|
+
url += "?userdata=" + URI::escape(@job.userdata)
|
27
|
+
end
|
28
|
+
response = self.class.get url, :headers=>{"Authorization"=>key, "Accept"=>"text/xml"}
|
29
|
+
end
|
14
30
|
parse_response response
|
15
31
|
end
|
16
32
|
|
17
33
|
private
|
18
34
|
def parse_response(response_xml)
|
19
|
-
|
20
|
-
|
21
|
-
message = doc.xpath("//message").text
|
22
|
-
jobid = doc.xpath("//jobid").text
|
23
|
-
userdata = doc.xpath("//userdata").text
|
24
|
-
|
35
|
+
code = response_xml.code
|
36
|
+
data = Crack::XML.parse(response_xml.body)
|
25
37
|
Response.new(
|
26
38
|
:code => code,
|
27
|
-
:
|
28
|
-
:jobid => jobid,
|
29
|
-
:userdata => userdata
|
39
|
+
:data => data
|
30
40
|
)
|
31
41
|
end
|
32
42
|
end
|
data/lib/uencode/response.rb
CHANGED
@@ -1,29 +1,35 @@
|
|
1
|
+
# Copyright (c) 2011, uEncode, Cassio Marques
|
2
|
+
|
1
3
|
module UEncode
|
2
4
|
class Response
|
3
|
-
class BadRequestError
|
4
|
-
class
|
5
|
-
class
|
6
|
-
class
|
7
|
-
class
|
5
|
+
class BadRequestError < StandardError; end
|
6
|
+
class NotAuthorizedError < StandardError; end
|
7
|
+
class BadRequesError < StandardError; end
|
8
|
+
class UnacceptableError < StandardError; end
|
9
|
+
class NotFoundError < StandardError; end
|
10
|
+
class ServerError < StandardError; end
|
11
|
+
class UnknownError < StandardError; end
|
8
12
|
|
9
|
-
ATTRIBUTES = [:code, :
|
13
|
+
ATTRIBUTES = [:code, :data]
|
10
14
|
|
11
15
|
include AttrSetting
|
12
16
|
|
13
17
|
def initialize(options)
|
14
|
-
check_response_code options[:code], options[:
|
18
|
+
check_response_code options[:code], options[:data]
|
15
19
|
super
|
16
20
|
end
|
17
21
|
|
18
22
|
private
|
19
|
-
def check_response_code(code,
|
20
|
-
return if code ==
|
23
|
+
def check_response_code(code, data)
|
24
|
+
return if code == 200
|
21
25
|
case code
|
22
|
-
when
|
23
|
-
when
|
24
|
-
when
|
25
|
-
when
|
26
|
-
|
26
|
+
when 401; raise NotAuthorizedError, data
|
27
|
+
when 403; raise NotAuthorizedError, data
|
28
|
+
when 400; raise BadRequestError, data
|
29
|
+
when 406; raise UnacceptableError, data
|
30
|
+
when 404; raise NotFoundError, data
|
31
|
+
when 500; raise ServerError, message
|
32
|
+
else raise UnknownError, "#{code} : #{data}"
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
data/lib/uencode/version.rb
CHANGED