arndtjenssen-picothumbs 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/HISTORY +2 -0
  2. data/LICENSE +20 -0
  3. data/README +43 -0
  4. data/TODO +4 -0
  5. data/lib/picothumbs.rb +66 -0
  6. metadata +59 -0
data/HISTORY ADDED
@@ -0,0 +1,2 @@
1
+ 21-09-2009: added rdoc, bumped to v0.1.1
2
+ 20-09-2009: initial version 0.1
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Arndt Jenssen
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,43 @@
1
+ Picothumbs
2
+
3
+ == Description
4
+ Picothumbs is a ruby gem to allow easy access the web thumbnail generation service at http://www.picothumbs.com
5
+
6
+ == Installation
7
+ sudo gem install arndtjenssen-picothumbs -s http://gems.github.com
8
+
9
+ == Usage
10
+ You need to register (for free) at www.picothumbs.com and retrieve your api-key and storage-key from the profile page.
11
+
12
+ Then in your (rails) application you can do:
13
+
14
+ # create a service request class
15
+ thumbs = Picothumbs.new("your-api-key", "your-storage-key")
16
+
17
+ # request to create the thumbnail from google.com
18
+ thumb.simple_request("www.google.com")
19
+ > request-key
20
+
21
+ # poll if the thumbnail has been created
22
+ thumbs.poll
23
+ > one of: "ok" or "queued"
24
+
25
+ # download the thumbnail to a local path with a specific size
26
+ thumbs.download("/tmp", Picothumbs::MEDIUM)
27
+ > stores a thumbnail in tmp
28
+
29
+ For other possible sizes see http://www.picothumbs.com/features
30
+
31
+ == Gem Home
32
+ * Github Home: http://github.com/arndtjenssen/picothumbs
33
+ * Gem Home: http://www.picothumbs.com/ruby_gem
34
+
35
+
36
+ == Limitations
37
+ Very first version does cover only simple api requests.
38
+
39
+ == Todos
40
+ * Create specs
41
+ * Add rdoc
42
+ * Make request fault tolerant
43
+ * Add REST API requests
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ * Create specs
2
+ * Add rdoc
3
+ * Make request fault tolerant
4
+ * Add REST API requests
@@ -0,0 +1,66 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'open-uri'
5
+
6
+
7
+ #
8
+ # Service class to access the API on www.picothumbs.com to create thumbnails of web pages
9
+ #
10
+
11
+ class Picothumbs
12
+ VERSION = '0.1.1'
13
+
14
+ BASE_API_URL = "http://www.picothumbs.com/api"
15
+ BASE_THUMBNAIL_URL = "http://www.picothumbs.com/images/thumbs"
16
+
17
+ # poll return resposnes
18
+ POLL_OK = "ok"
19
+ POLL_QUEUED = "queued"
20
+ POLL_ERROR = "thumbnail error"
21
+ POLL_EXPIRED = "expired"
22
+
23
+ # thumbnail sizes
24
+ TINY = "tiny"
25
+ SMALL = "small"
26
+ MEDIUM = "medium"
27
+ NORMAL = "normal"
28
+ LARGE = "large"
29
+ EXTRA = "extra"
30
+ ALL_SIZES = %w(tiny small medium normal large extra)
31
+
32
+ attr_reader :response, :request_key, :last_url
33
+ attr_accessor :api_key, :storage_key
34
+
35
+ def initialize(api_key, storage_key)
36
+ @api_key = api_key
37
+ @storage_key = storage_key
38
+ end
39
+
40
+ # Creates a request to create a thumbnail of page with url
41
+ def simple_request(url)
42
+ @last_url = url
43
+ @request_key = open("#{BASE_API_URL}/simple?key=#{@api_key}&url=#{url}"){ |f| f.read }
44
+ end
45
+
46
+ # Polls if a request has been processed
47
+ def poll(request_key = nil)
48
+ request_key = @request_key if request_key.nil?
49
+ @response = open("#{BASE_API_URL}/poll?key=#{@api_key}&request_key=#{request_key}"){|f| f.read}
50
+ end
51
+
52
+ # returns a value > 0 on succesful download and 0 if thumbnail is not yet ready
53
+ # if request_key is nil or not given, the last request key from the simple_request method is used
54
+ # file is stored in path and named 'request_key-size.jpg'
55
+ def download(path, size, request_key = nil, poll_before_download = true)
56
+ if poll_before_download
57
+ return 0 if poll(request_key) != POLL_OK
58
+ end
59
+
60
+ request_key = @request_key if request_key.nil?
61
+ file = File.join(path, "#{request_key}-#{size}.jpg")
62
+
63
+ open(file, "wb").write(open("#{BASE_THUMBNAIL_URL}/#{@storage_key}/#{request_key}-#{size}.jpg").read)
64
+ end
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arndtjenssen-picothumbs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Arndt Jenssen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Allows easy access to the web thumbnail service of www.picothumbs.com
17
+ email: gem@picothumbs.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - README
27
+ - HISTORY
28
+ - TODO
29
+ - lib/picothumbs.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/arndtjenssen/picothumbs
32
+ licenses:
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: picothumbs
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Allows easy access to the web thumbnail service of www.picothumbs.com
58
+ test_files: []
59
+