simplificator-rwebthumb 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +3 -3
- data/lib/rwebthumb/job.rb +24 -3
- data/lib/rwebthumb/webthumb.rb +52 -10
- data/test/webthumb_test.rb +21 -1
- metadata +2 -3
data/README
CHANGED
@@ -4,12 +4,12 @@
|
|
4
4
|
A Ruby wrapper for the webthumb API from http://webthumb.bluga.net
|
5
5
|
|
6
6
|
= dependencies
|
7
|
-
|
7
|
+
* tzinfo:http://tzinfo.rubyforge.org (install: sudo gem install tzinfo)
|
8
8
|
|
9
9
|
= Installation
|
10
10
|
sudo gem update --system (in case you are not yet on version 1.2.0 or higher)
|
11
11
|
sudo gem sources -a http://gems.github.com (only once)
|
12
|
-
sudo gem install rwebthumb
|
12
|
+
sudo gem install simplificator-rwebthumb
|
13
13
|
|
14
14
|
|
15
15
|
= Usage
|
@@ -37,7 +37,7 @@ job.fetch_when_complete(:large)
|
|
37
37
|
# once thumbnails are fetched they are cached within the job. so a new fetch will not go to the server again
|
38
38
|
|
39
39
|
# there is a helper method to write the images to disk
|
40
|
-
job.write_file(, '/tmp/test.jpg')
|
40
|
+
job.write_file(job.fetch(:custom), '/tmp/test.jpg')
|
41
41
|
|
42
42
|
# if you have a job ID then you can use this to get a Job object and then use the fetch_xyz methods
|
43
43
|
wt.job_status(JOB_ID)
|
data/lib/rwebthumb/job.rb
CHANGED
@@ -15,14 +15,23 @@ module Simplificator
|
|
15
15
|
submission_datetime = self.parse_webthumb_datetime(job_element.attributes['time'])
|
16
16
|
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
17
|
end
|
18
|
-
|
18
|
+
# Factory method to create a Job object from a status XML.
|
19
|
+
# this does not set all attributes of the Job (url, duration_estimate, cost) since the API of webthumb does not
|
20
|
+
# return the same information on job creation and status requests.
|
19
21
|
def self.from_status_xml(api_key, xml)
|
20
22
|
status_element = REXML::XPath.first(xml, '/webthumb/jobStatus/status')
|
21
23
|
submission_datetime = self.parse_webthumb_datetime(status_element.attributes['submissionTime'])
|
22
24
|
job = Job.new(api_key, status_element.attributes['id'], nil, submission_datetime, 5, nil,
|
23
25
|
status_element.text == 'Complete' ? STATUS_PICKUP : STATUS_PROCESSING)
|
24
26
|
end
|
25
|
-
|
27
|
+
# Constructor.
|
28
|
+
# *api_key: webthumb API key. Required by all the operations which query the server
|
29
|
+
# *job_id: id of the job. Required.
|
30
|
+
# *url: the url of the site to snapshot. Optional
|
31
|
+
# *submission_datetime: UTC Datetime of job submission
|
32
|
+
# *duration_estimate: integer value indicating estimated job duration in seconds
|
33
|
+
# *cost: integer value indicating how many credit this request costet. Optional
|
34
|
+
# *status: one of the STATUS_XXX constants defined in Base. Defaults to STATUS_PROCESSING
|
26
35
|
def initialize(api_key, job_id, url, submission_datetime, duration_estimate, cost, status = STATUS_PROCESSING)
|
27
36
|
super(api_key)
|
28
37
|
@job_id = job_id
|
@@ -34,12 +43,14 @@ module Simplificator
|
|
34
43
|
@cache = {}
|
35
44
|
end
|
36
45
|
|
46
|
+
# Checks the status of the job on webthumb server.
|
47
|
+
# Returns one of the STATUS_XXX constants from Base.
|
48
|
+
# A call to this method updates the @status attribute.
|
37
49
|
def check_status
|
38
50
|
response = do_request(build_status_xml())
|
39
51
|
@status = REXML::XPath.first(response, '/webthumb/jobStatus/status').text == 'Complete' ? STATUS_PICKUP : STATUS_PROCESSING
|
40
52
|
if pickup?
|
41
53
|
@completion_time = response.attributes['completionTime']
|
42
|
-
@duration = 'not yet... need to convert times first'
|
43
54
|
end
|
44
55
|
@status
|
45
56
|
end
|
@@ -52,6 +63,10 @@ module Simplificator
|
|
52
63
|
fetch(size)
|
53
64
|
end
|
54
65
|
|
66
|
+
# Fetch an image from the webthumb server.
|
67
|
+
# If the job has not yet finished then the server will return an error so check status first or use fetch_when_complete()
|
68
|
+
# Images are cached in the context of this Job so consequent calls are not requested from server again.
|
69
|
+
# Cache is experimental, dont know if it is a good idea.
|
55
70
|
def fetch(size = :small)
|
56
71
|
unless @cache.has_key?(size)
|
57
72
|
response = do_request(build_fetch_xml(size))
|
@@ -60,6 +75,10 @@ module Simplificator
|
|
60
75
|
@cache[size]
|
61
76
|
end
|
62
77
|
|
78
|
+
# Write the data to disk.
|
79
|
+
# *data: the bytes of the image as returned by fetch/fetch_when_complete
|
80
|
+
# *name: a filename
|
81
|
+
# Will return a File object
|
63
82
|
def write_file(data, name)
|
64
83
|
raise WebthumbException.new('NO data given') if data == nil || data.size == 0
|
65
84
|
File.open(name, 'wb+') do |file|
|
@@ -69,9 +88,11 @@ module Simplificator
|
|
69
88
|
end
|
70
89
|
end
|
71
90
|
|
91
|
+
# Is the status attribute set to STATUS_PICKUP ?
|
72
92
|
def pickup?
|
73
93
|
@status == STATUS_PICKUP
|
74
94
|
end
|
95
|
+
# Is the status attribute set to STATUS_PROCESSING ?
|
75
96
|
def processing?
|
76
97
|
@status == STATUS_PROCESSING
|
77
98
|
end
|
data/lib/rwebthumb/webthumb.rb
CHANGED
@@ -4,21 +4,54 @@ require 'date'
|
|
4
4
|
module Simplificator
|
5
5
|
module Webthumb
|
6
6
|
|
7
|
-
|
7
|
+
#
|
8
|
+
# Main access point for the webthumb API.
|
9
|
+
# All methods calling the Server need the api key beeing set.
|
10
|
+
#
|
8
11
|
class Webthumb < Base
|
12
|
+
# Request thumbnail creation from webthumb server. A Job object holding
|
13
|
+
# request information is returned (most important the job_id)
|
14
|
+
# Some keys have been ruby-ized (i.e. outputType -> :output_type). All keys are
|
15
|
+
# Symbols. Check out the webthumb API description for detailed explanation of the options
|
16
|
+
# http://webthumb.bluga.net/apidoc
|
17
|
+
# Options:
|
18
|
+
# * url: the url to take the snapshot from
|
19
|
+
# * output_type: chose output format (jpg or png)
|
20
|
+
# * width: width of the browser
|
21
|
+
# * height: height of the browser
|
22
|
+
# * fullthumb: full sized snapshot (true or false)
|
23
|
+
# * custom_thumbnail: create a custom sized thumbnail. A hash with width and height entries
|
24
|
+
# * effect: specify a visual effect (mirror, dropshadow or border)
|
25
|
+
# * delay: wait until the snapshot is taken
|
26
|
+
# * notify: callback url which is called after snapshot is taken
|
27
|
+
# * excerpt: taking an excerpt snapshot. A hash with for entries. width, height, x, y
|
28
|
+
# * videothumb: experimental option, check Joshs blog (http://blog.joshuaeichorn.com/archives/2008/07/08/videothumb-addon-to-webthumb-is-alpha/)
|
29
|
+
# (true or false)
|
30
|
+
# Only the url option is required. Check webthumb API for default values. Check also the different constants
|
31
|
+
# defined in Base.rb for valid values
|
32
|
+
#
|
9
33
|
def thumbnail(options = {})
|
10
34
|
Job.from_thumbnail_xml(@api_key, do_request(build_thumbnail_xml(options)))
|
11
35
|
end
|
12
|
-
|
36
|
+
|
37
|
+
# Request the job status from server.
|
38
|
+
# This can be used to look up job status from server when you just have the job ID, e.g. when you want to retrieve
|
39
|
+
# the thumbs later or when you use callbacks.
|
40
|
+
# The Job object returned from this method does not have all attributes set since. Webthumbs API does not return
|
41
|
+
# the requested URL, the duration estimation and the cost values when checking the status. I hope this will change someday.
|
13
42
|
def job_status(job_id)
|
43
|
+
raise WebthumbException.new('Job id is required') if job_id == nil || job_id == ''
|
14
44
|
Job.from_status_xml(@api_key, do_request(build_job_status_xml(job_id)))
|
15
45
|
end
|
16
46
|
|
47
|
+
# Check your credit status on the webthumbs server
|
48
|
+
# Returns a hash with two keys. See webthumb API for detailed information.
|
49
|
+
# * reserve: an integer
|
50
|
+
# * subscription: an integer
|
17
51
|
def credits()
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
{:reserve => elements['reserve'].text.to_i, :subscription => elements['subscription'].text.to_i}
|
52
|
+
response = do_request(build_credits_xml())
|
53
|
+
credit_element = REXML::XPath.first(response, '/webthumb/credits')
|
54
|
+
{:reserve => credit_element['reserve'].text.to_i, :subscription => credit_element['subscription'].text.to_i}
|
22
55
|
end
|
23
56
|
|
24
57
|
private
|
@@ -30,11 +63,15 @@ module Simplificator
|
|
30
63
|
request = root.add_element('request')
|
31
64
|
request.add_element('url').add_text(options[:url])
|
32
65
|
add_element(request, options, :output_type, 'outputType')
|
33
|
-
|
34
|
-
|
66
|
+
# these elements have the same name as the key in options hash
|
67
|
+
[:width, :height, :effect, :delay, :notify].each {|item| add_element(request, options, item)}
|
68
|
+
# these options need conversion from true/false to 0/1
|
35
69
|
if options[:fullthumb] == true
|
36
70
|
request.add_element('fullthumb').add_text('1')
|
37
71
|
end
|
72
|
+
if options[:videothumb] == true
|
73
|
+
request.add_element('videothumb').add_text('1')
|
74
|
+
end
|
38
75
|
if options.has_key?(:custom_thumbnail)
|
39
76
|
request.add_element('customThumbnail',
|
40
77
|
'width' => options[:custom_thumbnail][:width].to_s,
|
@@ -54,10 +91,15 @@ module Simplificator
|
|
54
91
|
root
|
55
92
|
end
|
56
93
|
|
94
|
+
def build_credits_xml()
|
95
|
+
root = build_root_node()
|
96
|
+
root.add_element('credits')
|
97
|
+
root
|
98
|
+
end
|
99
|
+
|
57
100
|
def validate_thumbnail_options(options)
|
58
101
|
raise WebthumbException.new('Need an URL') if options[:url] == nil || options[:url] == ''
|
59
|
-
raise WebthumbException.new("output_type is invalid: #{options[:output_type]}") if options.has_key?(:output_type) and (not Base::VALID_OUTPUT_TYPES.include?(options[:output_type]))
|
60
|
-
|
102
|
+
#raise WebthumbException.new("output_type is invalid: #{options[:output_type]}") if options.has_key?(:output_type) and (not Base::VALID_OUTPUT_TYPES.include?(options[:output_type]))
|
61
103
|
end
|
62
104
|
end
|
63
105
|
|
data/test/webthumb_test.rb
CHANGED
@@ -4,8 +4,28 @@ class WebthumbTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
|
6
6
|
def test_build_fetch()
|
7
|
-
xml = Webthumb.new('1234').send(:build_thumbnail_xml,
|
7
|
+
xml = Webthumb.new('1234').send(:build_thumbnail_xml,
|
8
|
+
:url => 'http://simplificator.com',
|
9
|
+
:videothumb => true, :fullthumb => true, :effect => 'dropshadow', :output_type => :jpg,
|
10
|
+
:width => 1024, :height => 2048, :custom_thumbnail => {:width => 300, :height => 400},
|
11
|
+
:excerpt => {:x => 30, :y => 40, :width => 600, :height => 345}, :delay => 20, :notify => 'http://foo.bar.com')
|
8
12
|
assert_equal('http://simplificator.com', REXML::XPath.first(xml, 'request/url').text)
|
13
|
+
assert_equal('1', REXML::XPath.first(xml, 'request/videothumb').text)
|
14
|
+
assert_equal('1', REXML::XPath.first(xml, 'request/fullthumb').text)
|
15
|
+
assert_equal('dropshadow', REXML::XPath.first(xml, 'request/effect').text)
|
16
|
+
assert_equal('jpg', REXML::XPath.first(xml, 'request/outputType').text)
|
17
|
+
assert_equal('1024', REXML::XPath.first(xml, 'request/width').text)
|
18
|
+
assert_equal('2048', REXML::XPath.first(xml, 'request/height').text)
|
19
|
+
assert_equal('300', REXML::XPath.first(xml, 'request/customThumbnail').attributes['width'])
|
20
|
+
assert_equal('400', REXML::XPath.first(xml, 'request/customThumbnail').attributes['height'])
|
21
|
+
assert_equal('30', REXML::XPath.first(xml, 'request/excerpt/x').text)
|
22
|
+
assert_equal('40', REXML::XPath.first(xml, 'request/excerpt/y').text)
|
23
|
+
assert_equal('600', REXML::XPath.first(xml, 'request/excerpt/width').text)
|
24
|
+
assert_equal('345', REXML::XPath.first(xml, 'request/excerpt/height').text)
|
25
|
+
assert_equal('20', REXML::XPath.first(xml, 'request/delay').text)
|
26
|
+
assert_equal('http://foo.bar.com', REXML::XPath.first(xml, 'request/notify').text)
|
27
|
+
|
28
|
+
|
9
29
|
end
|
10
30
|
|
11
31
|
def test_build_job_status_xml()
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplificator-rwebthumb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-07-
|
12
|
+
date: 2008-07-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,7 +29,6 @@ files:
|
|
29
29
|
- test/base_test.rb
|
30
30
|
- test/helper.rb
|
31
31
|
- test/job_test.rb
|
32
|
-
- test/run_test.rb
|
33
32
|
- test/webthumb_test.rb
|
34
33
|
- README
|
35
34
|
- init.rb
|