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 CHANGED
@@ -1,8 +1,8 @@
1
1
  # UEncode
2
2
 
3
- A simple Ruby gem to consume the [uEncode](http://www.uencode.com) API.
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
- ## Synopsis
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.configure_video_output { |c| c.destination = "http://your_destination_url/transcoded.mp4"; c.container = "mpeg4" }
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 = UEncode::Medium.new
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.configure_video { |c| c.bitrate = 300000; c.codec = "h264"}
24
+ #create video output and set configuration created above
25
+ out = UEncode::VideoOutput.new :container => 'mpeg4'
26
+ out.streams.configure configuration
22
27
 
23
- video.configure_audio do |c|
24
- c.bitrate = 64000
25
- c.codec = "aac"
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
- capture = UEncode::CaptureOutput.new :destination => "http://whatever.com/foo.zip", :rate => "every 30s"
33
- job.add_capture capture
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
- puts response.code # => 'Ok'
40
- puts response.message # => 'Your job was created successfully'
41
- puts response.jobid # => 1234567
42
- puts response.userdata # => 'This is a simple test'
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
- ## Accepted parameters
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
- Currently all the uEncode API parameters are supported (or so I think :)
70
+ puts response.code
71
+ puts response.data
72
+ ```
48
73
 
49
- Currently the gem does not validate parameters values, so pay attention to the API docs.
74
+ ## Parameters
50
75
 
51
- You can see the whole list of accepted parameters in the [uEncode API documentation](http://www.uencode.com/api/300#response_codes).
76
+ All version 300 uEncode API parameters are supported.
52
77
 
53
- For the UEncode classes that map to each complex API parameters (like Crop, Size, FrameRate, etc) take a look at the spec file at /spec/elements_spec.rb or read the docs.
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
- ## Credits
80
+ ##License
56
81
 
57
- This gem was created during my work to integrate the uEncode API in our application at [DailyDigital](http://www.dailydigital.com). Thanks for letting me open source it!
82
+ This project is released under The MIT License
58
83
 
59
- ## Running the specs
84
+ Copyright © 2011, uEncode, Cassio Marques
60
85
 
61
- * bundle install
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.
@@ -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
- def to_xml
6
- %Q{
7
- <crop>
8
- <width>#{width}</width>
9
- <height>#{height}</height>
10
- <x>#{x}</x>
11
- <y>#{y}</y>
12
- </crop>
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
- end
19
+
16
20
 
17
21
  class Size
18
- ATTRIBUTES = [:width, :height]
19
-
20
- def to_xml
21
- %Q{
22
- <size>
23
- <width>#{width}</width>
24
- <height>#{height}</heignt>
25
- </size>
26
- }
27
- end
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
- class FrameRate
50
- include RateElement
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; "framerate"; end
70
+ def root_name; "destination"; end
54
71
  end
55
-
56
- class Par < FrameRate
57
- include RateElement
58
-
72
+
73
+ class Source
74
+ include Transfer
75
+
59
76
  private
60
- def root_name; "par"; end
77
+ def root_name; "source"; end
61
78
  end
62
79
 
63
80
  class CaptureOutput
64
- ATTRIBUTES = [:destination, :rate, :stretch, :crop, :size]
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
- <output>
74
- <capture>
113
+ <capture>
75
114
  <rate>#{rate}</rate>
76
- <destination>#{destination}</destination>
77
- #{@crop ? @crop.to_xml : ""}
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
- # a list of Medium
91
- attr_reader :items
92
- attr_writer :destination, :container
132
+ attr_writer :container
133
+ attr_accessor :streams
93
134
 
94
135
  def initialize(options)
95
- @items = []
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
- <destination>#{destination}</destination>
152
+ <destinations>
153
+ #{@destinations.inject("") { |s, dest| s << dest.to_xml}}
154
+ </destinations>
108
155
  <container>#{container}</container>
109
- <media>
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
- # Medium is a single video to transcode
119
- class Medium
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 = {"video" => { ... }, "audio" => { ... }
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 "framerate" and "par" values must be also hashes, with the following format:
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["video"]
144
- audio = hash["audio"]
145
- configure_video do |c|
146
- video.each_pair { |key, value| c.send("#{key}=", value) }
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
- configure_audio do |c|
150
- audio.each_pair { |key, value| c.send("#{key}=", value) }
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>#{video.bitrate}</birate>
175
- <codec>#{video.codec}</birate>
176
- #{!video.cbr.nil? ? '<cbr>' + video.cbr.to_s + '</cbr>' : ""}
177
- #{video.crop ? video.crop.to_xml : ""}
178
- #{video.deinterlace.nil? ? "" : '<deinterlace>' + video.deinterlace.to_s + '</deinterlace>'}
179
- #{video.framerate ? video.framerate.to_xml : ""}
180
- #{video.height.nil? ? "" : '<height>' + video.height.to_s + '</height>'}
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
- #{[nil, false].include?(video.stretch) ? "" : '<stretch>' + video.stretch.to_s + '</stretch>'}
187
- #{video.width.nil? ? "" : '<width>' + video.width.to_s + '</width>'}
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 Medium
240
+ # The video configs for each VideoStream
201
241
  class VideoConfig
202
- attr_accessor :bitrate, :codec, :cbr, :crop, :deinterlace, :framerate, :height, :keyframe_interval,
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 framerate=(_framerate)
214
- _framerate = FrameRate.new(_framerate) unless _framerate.instance_of?(FrameRate) || _framerate.nil?
215
- instance_variable_set :@framerate, _framerate
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 par=(_par)
219
- _par = Par.new(_par) unless _par.instance_of?(Par) || _par.nil?
220
- instance_variable_set :@par, _par
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 Medium
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 = [:source, :userdata, :notify]
281
+ ATTRIBUTES = [:userdata, :callback, :customerkey]
231
282
 
232
- include Enumerable
283
+ include AttrSetting
233
284
 
234
- def self.from_hash(hash)
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
- @video_output = VideoOutput.new options[:video_output] || {}
295
+ @video_outputs = []
240
296
  @captures = []
297
+ source_video = options[:source_video]
241
298
  super
242
299
  end
243
300
 
244
- def configure_video_output
245
- yield @video_output
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
- <customerkey>#{UEncode.customer_key}</customerkey>
268
- <source>#{source}</source>
312
+ #{source_video.nil? ? "" : source_video.to_xml}
269
313
  #{userdata.nil? ? "" : '<userdata>' + userdata + '</userdata>'}
270
- #{notify.nil? ? "" : '<notify>' + notify + '</notify>'}
314
+ #{callback.nil? ? "" : '<callback>' + callback + '</callback>'}
271
315
  <outputs>
272
- #{@video_output.to_xml}
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
- Nokogiri::XML(xml).to_xml
321
+ xml.gsub(/\n?/, "").gsub(/>\s+</, "><")
278
322
  end
279
323
  end
280
324
 
281
- [Size, Crop, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting }
325
+ [SizeBase, Size, MaxSize, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting }
282
326
  end
@@ -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
- response = self.class.put "/jobs?version=300", :body => @job.to_xml
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
- doc = Nokogiri::XML response_xml.body
20
- code = doc.xpath("//code").text
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
- :message => message,
28
- :jobid => jobid,
29
- :userdata => userdata
39
+ :data => data
30
40
  )
31
41
  end
32
42
  end
@@ -1,29 +1,35 @@
1
+ # Copyright (c) 2011, uEncode, Cassio Marques
2
+
1
3
  module UEncode
2
4
  class Response
3
- class BadRequestError < StandardError; end
4
- class InvalidKeyError < StandardError; end
5
- class NotActiveError < StandardError; end
6
- class ServerError < StandardError; end
7
- class UnknownError < StandardError; end
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, :message, :jobid, :userdata]
13
+ ATTRIBUTES = [:code, :data]
10
14
 
11
15
  include AttrSetting
12
16
 
13
17
  def initialize(options)
14
- check_response_code options[:code], options[:message]
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, message)
20
- return if code == 'Ok'
23
+ def check_response_code(code, data)
24
+ return if code == 200
21
25
  case code
22
- when 'BadRequest'; raise BadRequestError, message
23
- when 'InvalidKey'; raise InvalidKeyError, message
24
- when 'NotActive'; raise NotActiveError, message
25
- when 'ServerError'; raise ServerError, message
26
- else raise UnknownError, "#{code}: #{message}"
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
@@ -1,3 +1,5 @@
1
+ # Copyright (c) 2011, uEncode, Cassio Marques
2
+
1
3
  module UEncode
2
- VERSION = "0.0.3"
4
+ VERSION = "3.0.0"
3
5
  end
data/lib/uencode.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # Copyright (c) 2011, uEncode, Cassio Marques
2
+
1
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
5