rmm5t-rwebthumb 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rwebthumb/job.rb +16 -14
- data/test/job_test.rb +23 -10
- metadata +2 -2
data/lib/rwebthumb/job.rb
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
module Simplificator
|
2
|
-
module Webthumb
|
2
|
+
module Webthumb
|
3
3
|
class Job < Base
|
4
|
-
|
4
|
+
|
5
5
|
attr_reader :duration_estimate, :submission_datetime, :cost, :job_id, :url
|
6
|
-
|
6
|
+
|
7
7
|
# Constant for the status attribute when job is beeing processed
|
8
8
|
STATUS_PROCESSING = 100
|
9
9
|
# Constant for the status attribute when job is done
|
10
10
|
STATUS_PICKUP = 200
|
11
|
-
|
11
|
+
|
12
12
|
# Factory method to build a Job object from a REXML xml element
|
13
13
|
def self.from_thumbnail_xml(api_key, xml)
|
14
14
|
job_element = REXML::XPath.first(xml, '/webthumb/jobs/job')
|
15
|
+
return nil if job_element.nil?
|
16
|
+
|
15
17
|
submission_datetime = self.parse_webthumb_datetime(job_element.attributes['time'])
|
16
18
|
Job.new(api_key, job_element.text, job_element.attributes['url'], submission_datetime, job_element.attributes['estimate'].to_i, job_element.attributes['cost'].to_i)
|
17
|
-
end
|
19
|
+
end
|
18
20
|
# Factory method to create a Job object from a status XML.
|
19
21
|
# this does not set all attributes of the Job (url, duration_estimate, cost) since the API of webthumb does not
|
20
22
|
# return the same information on job creation and status requests.
|
21
23
|
def self.from_status_xml(api_key, xml)
|
22
24
|
status_element = REXML::XPath.first(xml, '/webthumb/jobStatus/status')
|
23
25
|
submission_datetime = self.parse_webthumb_datetime(status_element.attributes['submissionTime'])
|
24
|
-
job = Job.new(api_key, status_element.attributes['id'], nil, submission_datetime, 5, nil,
|
26
|
+
job = Job.new(api_key, status_element.attributes['id'], nil, submission_datetime, 5, nil,
|
25
27
|
status_element.text == 'Complete' ? STATUS_PICKUP : STATUS_PROCESSING)
|
26
28
|
end
|
27
29
|
# Constructor.
|
@@ -42,7 +44,7 @@ module Simplificator
|
|
42
44
|
@status = status
|
43
45
|
@cache = {}
|
44
46
|
end
|
45
|
-
|
47
|
+
|
46
48
|
# Checks the status of the job on webthumb server.
|
47
49
|
# Returns one of the STATUS_XXX constants from Base.
|
48
50
|
# A call to this method updates the @status attribute.
|
@@ -54,7 +56,7 @@ module Simplificator
|
|
54
56
|
end
|
55
57
|
@status
|
56
58
|
end
|
57
|
-
|
59
|
+
|
58
60
|
def fetch_when_complete(size = :small)
|
59
61
|
while not pickup?
|
60
62
|
sleep @duration_estimate
|
@@ -62,7 +64,7 @@ module Simplificator
|
|
62
64
|
end
|
63
65
|
fetch(size)
|
64
66
|
end
|
65
|
-
|
67
|
+
|
66
68
|
# Fetch an image from the webthumb server.
|
67
69
|
# If the job has not yet finished then the server will return an error so check status first or use fetch_when_complete()
|
68
70
|
# Images are cached in the context of this Job so consequent calls are not requested from server again.
|
@@ -74,7 +76,7 @@ module Simplificator
|
|
74
76
|
end
|
75
77
|
@cache[size]
|
76
78
|
end
|
77
|
-
|
79
|
+
|
78
80
|
# Write the data to disk.
|
79
81
|
# *data: the bytes of the image as returned by fetch/fetch_when_complete
|
80
82
|
# *name: a filename
|
@@ -87,16 +89,16 @@ module Simplificator
|
|
87
89
|
file
|
88
90
|
end
|
89
91
|
end
|
90
|
-
|
92
|
+
|
91
93
|
# Is the status attribute set to STATUS_PICKUP ?
|
92
94
|
def pickup?
|
93
95
|
@status == STATUS_PICKUP
|
94
|
-
end
|
96
|
+
end
|
95
97
|
# Is the status attribute set to STATUS_PROCESSING ?
|
96
98
|
def processing?
|
97
99
|
@status == STATUS_PROCESSING
|
98
100
|
end
|
99
|
-
|
101
|
+
|
100
102
|
private
|
101
103
|
def build_fetch_xml(size = :small)
|
102
104
|
raise WebthumbException.new("size parameter must be one of #{VALID_SIZES.join(', ')} but was #{size}") unless Base::VALID_SIZES.include?(size)
|
@@ -118,4 +120,4 @@ module Simplificator
|
|
118
120
|
end
|
119
121
|
end
|
120
122
|
end
|
121
|
-
end
|
123
|
+
end
|
data/test/job_test.rb
CHANGED
@@ -1,18 +1,25 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
class JobTest < Test::Unit::TestCase
|
3
|
-
|
4
|
-
|
5
|
-
<
|
6
|
-
<
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
JOB_XML = <<-EOF
|
4
|
+
<webthumb>
|
5
|
+
<jobs>
|
6
|
+
<job estimate='20' time='2008-02-27 12:49:48' url='http://blog.joshuaeichorn.com' cost='1'>wt47c5f71c37c3a</job>
|
7
|
+
</jobs>
|
8
|
+
</webthumb>
|
9
|
+
EOF
|
10
|
+
|
11
|
+
JOBLESS_XML = <<-EOF
|
12
|
+
<webthumb>
|
13
|
+
</webthumb>
|
14
|
+
EOF
|
15
|
+
|
16
|
+
def setup_job_from_xml(xml)
|
11
17
|
job_xml = REXML::Document.new(xml)
|
12
18
|
@job = Job.from_thumbnail_xml('1234', job_xml)
|
13
19
|
end
|
14
|
-
def test_from_thumbnail_xml
|
15
20
|
|
21
|
+
def test_from_thumbnail_xml
|
22
|
+
setup_job_from_xml(JOB_XML)
|
16
23
|
assert_equal('1234', @job.api_key)
|
17
24
|
assert_equal(20, @job.duration_estimate)
|
18
25
|
assert_equal(Time.parse('2008-02-27 19:49:48 UTC'), @job.submission_datetime)
|
@@ -21,15 +28,21 @@ class JobTest < Test::Unit::TestCase
|
|
21
28
|
assert_equal('wt47c5f71c37c3a', @job.job_id)
|
22
29
|
end
|
23
30
|
|
24
|
-
|
25
31
|
def test_build_fetch()
|
32
|
+
setup_job_from_xml(JOB_XML)
|
26
33
|
xml = @job.send(:build_fetch_xml)
|
27
34
|
assert_equal('small', REXML::XPath.first(xml, 'fetch/size').text)
|
28
35
|
assert_equal('wt47c5f71c37c3a', REXML::XPath.first(xml, 'fetch/job').text)
|
29
36
|
end
|
30
37
|
|
31
38
|
def test_build_status_xml()
|
39
|
+
setup_job_from_xml(JOB_XML)
|
32
40
|
xml = @job.send(:build_status_xml)
|
33
41
|
assert_equal('wt47c5f71c37c3a', REXML::XPath.first(xml, 'status/job').text)
|
34
42
|
end
|
43
|
+
|
44
|
+
def test_from_thumbnail_xml_without_any_jobs()
|
45
|
+
setup_job_from_xml(JOBLESS_XML)
|
46
|
+
assert_nil @job
|
47
|
+
end
|
35
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmm5t-rwebthumb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simplificator GmbH
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-09-
|
12
|
+
date: 2008-09-10 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|