editable-image 0.23 → 0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +3 -1
- data/editable-image.gemspec +5 -5
- data/lib/editable-image.rb +5 -0
- data/lib/editable-image/exceptions.rb +6 -0
- data/lib/picnik/picnik.rb +63 -0
- data/test/files/test.txt +0 -0
- data/test/picnik_test.rb +95 -26
- metadata +8 -5
- data/lib/picnik.rb +0 -55
data/README
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
This is currently alpha software.
|
2
2
|
|
3
3
|
A gem for interacting with online image editors. Currently, only Picnik is
|
4
|
-
supported. This is not a straight API wrapper.See
|
4
|
+
supported. This is not a straight API wrapper. See
|
5
5
|
http://github.com/haikuwebdev/editable_image_sample/ for a sample Rails app
|
6
6
|
using attachment_fu and this gem to edit images.
|
7
7
|
|
8
|
+
You'll need the shoulda gem to run the tests.
|
9
|
+
|
8
10
|
The gem is served from rubyforge. http://rubyforge.org/projects/editable-image/
|
data/editable-image.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "editable-image"
|
3
|
-
s.version = "0.
|
4
|
-
s.date = "2008-
|
3
|
+
s.version = "0.25"
|
4
|
+
s.date = "2008-09-02"
|
5
5
|
s.summary = "Simplified interface to web-based image editors."
|
6
6
|
s.email = "tj@haikuwebdev.com"
|
7
|
-
s.homepage = "
|
7
|
+
s.homepage = ""
|
8
8
|
s.description = "Simplified interface to web-based image editors."
|
9
9
|
s.has_rdoc = false
|
10
10
|
s.authors = ["TJ Stankus"]
|
11
|
-
s.files = ["README", "editable-image.
|
12
|
-
s.test_files = ["test/picnik_test.rb", "test/files/logo.gif"]
|
11
|
+
s.files = ["editable-image.gemspec", "README", "lib/multipart.rb", "lib/editable-image.rb", "lib/editable-image/exceptions.rb", "lib/picnik/picnik.rb"]
|
12
|
+
s.test_files = ["test/picnik_test.rb", "test/files/logo.gif", "test/files/test.txt"]
|
13
13
|
s.add_dependency("mime-types", [">= 1.15"])
|
14
14
|
s.add_dependency("Shoulda", [">= 1.1.1"])
|
15
15
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module EditableImage
|
2
|
+
class Picnik
|
3
|
+
|
4
|
+
PICNIK_API_PARAMETERS = %w(
|
5
|
+
apikey sig expires locale import returntype title export export_agent
|
6
|
+
export_field export_method export_title redirect imageid original_thumb
|
7
|
+
replace thumbs out_format out_quality out_maxsize out_maxwidth out_maxheight
|
8
|
+
exclude host_name default_in default_out page close_target expand_button
|
9
|
+
)
|
10
|
+
|
11
|
+
def self.url(full_filename, parameters)
|
12
|
+
raise EditableImage::InvalidParametersError unless api_key_in?(parameters)
|
13
|
+
url = ''
|
14
|
+
File.open(full_filename) do |file|
|
15
|
+
raise EditableImage::InvalidFileTypeError unless image?(full_filename)
|
16
|
+
http = Net::HTTP.new('www.picnik.com')
|
17
|
+
http.start do |http|
|
18
|
+
request = Net::HTTP::Post.new('/service/')
|
19
|
+
request.multipart_params = request_parameters(file, parameters)
|
20
|
+
response = http.request(request)
|
21
|
+
url = response.body
|
22
|
+
end
|
23
|
+
end
|
24
|
+
url
|
25
|
+
rescue EditableImage::InvalidFileTypeError => e
|
26
|
+
raise e, "File must be an image."
|
27
|
+
rescue EditableImage::InvalidParametersError => e
|
28
|
+
raise e, "Parameters must include the apikey."
|
29
|
+
rescue Errno::ENOENT => e
|
30
|
+
raise EditableImage::InvalidFilenameError.new, e.message
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.request_parameters(file, parameters)
|
36
|
+
required_parameters = { :import => 'image_data', :image_data => file, :returntype => 'text' }
|
37
|
+
scrub_parameters(required_parameters.merge(parameters))
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.scrub_parameters(parameters)
|
41
|
+
scrubbed_parameters = {}
|
42
|
+
parameters.each do |key, value|
|
43
|
+
scrubbed_parameters[parameter_key(key)] = value
|
44
|
+
end
|
45
|
+
scrubbed_parameters
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.parameter_key(key)
|
49
|
+
key = key.to_s
|
50
|
+
PICNIK_API_PARAMETERS.include?(key) ? '_' + key : key
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.image?(filename)
|
54
|
+
MIME::Types.of(filename).first.media_type == 'image'
|
55
|
+
end
|
56
|
+
|
57
|
+
# NOTE: Generalize, DRY up if needed. For now YAGNI.
|
58
|
+
def self.api_key_in?(parameters)
|
59
|
+
scrub_parameters(parameters).has_key?('_apikey')
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
data/test/files/test.txt
ADDED
File without changes
|
data/test/picnik_test.rb
CHANGED
@@ -1,32 +1,30 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../lib/
|
1
|
+
require File.dirname(__FILE__) + '/../lib/editable-image'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'shoulda'
|
4
4
|
|
5
|
+
|
5
6
|
class PicnikTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
# TODO: test response
|
8
|
-
# TODO: test bad filename passed to url
|
9
|
-
# TODO: test non-image filename passed to url
|
10
|
-
# TODO: exception for not passing apikey to parameters?
|
11
|
-
|
7
|
+
|
12
8
|
context "Request parameters" do
|
13
9
|
setup do
|
14
|
-
|
15
|
-
|
10
|
+
EditableImage::Picnik.class_eval do
|
11
|
+
def self.public_request_parameters(*args)
|
12
|
+
request_parameters(*args)
|
13
|
+
end
|
16
14
|
end
|
17
15
|
@file = File.open("files/logo.gif")
|
18
|
-
parameters = {:apikey => '
|
19
|
-
@request_parameters = Picnik.public_request_parameters(@file, parameters)
|
16
|
+
parameters = {:apikey => 'test_picnik_apikey'}
|
17
|
+
@request_parameters = EditableImage::Picnik.public_request_parameters(@file, parameters)
|
20
18
|
end
|
21
19
|
|
22
20
|
teardown do
|
23
21
|
@file.close
|
24
22
|
end
|
25
23
|
|
26
|
-
should "include _import
|
24
|
+
should "include _import" do
|
27
25
|
assert @request_parameters.keys.include?('_import')
|
28
26
|
end
|
29
|
-
|
27
|
+
|
30
28
|
should "have correct value for _import" do
|
31
29
|
assert_equal 'image_data', @request_parameters['_import']
|
32
30
|
end
|
@@ -37,35 +35,106 @@ class PicnikTest < Test::Unit::TestCase
|
|
37
35
|
|
38
36
|
should "not have nil image_data" do
|
39
37
|
assert_not_nil @request_parameters['image_data']
|
40
|
-
end
|
38
|
+
end
|
39
|
+
|
40
|
+
should "include _returntype" do
|
41
|
+
assert @request_parameters.keys.include?('_returntype')
|
42
|
+
end
|
43
|
+
|
44
|
+
should "have returntype of text" do
|
45
|
+
assert_equal 'text', @request_parameters['_returntype']
|
46
|
+
end
|
47
|
+
|
41
48
|
end
|
42
49
|
|
43
50
|
context "Parameter key" do
|
44
51
|
setup do
|
45
|
-
|
46
|
-
|
52
|
+
EditableImage::Picnik.class_eval do
|
53
|
+
def self.public_parameter_key(*args)
|
54
|
+
parameter_key(*args)
|
55
|
+
end
|
47
56
|
end
|
48
57
|
end
|
49
|
-
|
58
|
+
|
50
59
|
should "be stringified" do
|
51
|
-
assert_equal 'foo', Picnik.public_parameter_key(:foo)
|
60
|
+
assert_equal 'foo', EditableImage::Picnik.public_parameter_key(:foo)
|
52
61
|
end
|
53
|
-
|
62
|
+
|
54
63
|
should "add underscore for Picnik API parameter" do
|
55
|
-
assert_equal '_apikey', Picnik.public_parameter_key(:apikey)
|
64
|
+
assert_equal '_apikey', EditableImage::Picnik.public_parameter_key(:apikey)
|
56
65
|
end
|
57
|
-
|
66
|
+
|
58
67
|
should "not add underscore for non-Picnik API parameter" do
|
59
|
-
assert_equal 'foo', Picnik.public_parameter_key('foo')
|
68
|
+
assert_equal 'foo', EditableImage::Picnik.public_parameter_key('foo')
|
60
69
|
end
|
61
|
-
|
70
|
+
|
62
71
|
should "not add underscore for Picnik API parameter that already has one" do
|
63
|
-
assert_equal '_apikey', Picnik.public_parameter_key('_apikey')
|
72
|
+
assert_equal '_apikey', EditableImage::Picnik.public_parameter_key('_apikey')
|
64
73
|
end
|
65
|
-
|
74
|
+
|
66
75
|
should "keep underscore for non Picnik API parameter" do
|
67
|
-
assert_equal '_method', Picnik.public_parameter_key('_method')
|
76
|
+
assert_equal '_method', EditableImage::Picnik.public_parameter_key('_method')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "A bad filename" do
|
81
|
+
setup do
|
82
|
+
@filename = 'bad_filename'
|
83
|
+
@parameters = {:apikey => 'test'}
|
84
|
+
end
|
85
|
+
|
86
|
+
should "raise EditableImage::InvalidFilenameError exception" do
|
87
|
+
assert_raise(EditableImage::InvalidFilenameError) do
|
88
|
+
EditableImage::Picnik.url(@filename, @parameters)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
should "have exception message containing 'no such file'" do
|
93
|
+
begin
|
94
|
+
EditableImage::Picnik.url(@filename, @parameters)
|
95
|
+
rescue EditableImage::InvalidFilenameError => e
|
96
|
+
assert_match /no such file/i, e.message
|
97
|
+
end
|
68
98
|
end
|
69
99
|
end
|
70
100
|
|
101
|
+
context "A non-image file" do
|
102
|
+
setup do
|
103
|
+
@non_image_filename = File.expand_path("files/test.txt")
|
104
|
+
@parameters = {:apikey => 'test'}
|
105
|
+
end
|
106
|
+
|
107
|
+
should "raise EditableImage::InvalidFileTypeError exception" do
|
108
|
+
assert_raise(EditableImage::InvalidFileTypeError) do
|
109
|
+
EditableImage::Picnik.url(@non_image_filename, @parameters)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
should "have exception message containing 'foo'" do
|
114
|
+
begin
|
115
|
+
EditableImage::Picnik.url(@non_image_filename, @parameters)
|
116
|
+
rescue EditableImage::InvalidFileTypeError => e
|
117
|
+
assert_match /must be an image/i, e.message
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "A request for url without apikey in parameters" do
|
123
|
+
|
124
|
+
should "raise EditableImage::InvalidParametersError" do
|
125
|
+
assert_raise(EditableImage::InvalidParametersError) do
|
126
|
+
EditableImage::Picnik.url('some_filename', {})
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
should "raise EditableImage::InvalidParametersError exception with message containing 'must include the apikey'" do
|
131
|
+
begin
|
132
|
+
EditableImage::Picnik.url('some_filename', {})
|
133
|
+
rescue EditableImage::InvalidParametersError => e
|
134
|
+
assert_match /must include the apikey/i, e.message
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
71
140
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: editable-image
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.25"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Stankus
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-09-02 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -41,12 +41,14 @@ extensions: []
|
|
41
41
|
extra_rdoc_files: []
|
42
42
|
|
43
43
|
files:
|
44
|
-
- README
|
45
44
|
- editable-image.gemspec
|
45
|
+
- README
|
46
46
|
- lib/multipart.rb
|
47
|
-
- lib/
|
47
|
+
- lib/editable-image.rb
|
48
|
+
- lib/editable-image/exceptions.rb
|
49
|
+
- lib/picnik/picnik.rb
|
48
50
|
has_rdoc: false
|
49
|
-
homepage:
|
51
|
+
homepage: ""
|
50
52
|
post_install_message:
|
51
53
|
rdoc_options: []
|
52
54
|
|
@@ -74,3 +76,4 @@ summary: Simplified interface to web-based image editors.
|
|
74
76
|
test_files:
|
75
77
|
- test/picnik_test.rb
|
76
78
|
- test/files/logo.gif
|
79
|
+
- test/files/test.txt
|
data/lib/picnik.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'multipart'
|
3
|
-
|
4
|
-
class Picnik
|
5
|
-
|
6
|
-
PICNIK_API_PARAMETERS = %w(
|
7
|
-
apikey sig expires locale import returntype title export export_agent
|
8
|
-
export_field export_method export_title redirect imageid original_thumb
|
9
|
-
replace thumbs out_format out_quality out_maxsize out_maxwidth out_maxheight
|
10
|
-
exclude host_name default_in default_out page close_target expand_button
|
11
|
-
)
|
12
|
-
|
13
|
-
class << self
|
14
|
-
|
15
|
-
def url(full_filename, parameters)
|
16
|
-
url = ''
|
17
|
-
File.open(full_filename) do |file|
|
18
|
-
http = Net::HTTP.new('www.picnik.com')
|
19
|
-
http.start do |http|
|
20
|
-
request = Net::HTTP::Post.new('/service/')
|
21
|
-
request.multipart_params = request_parameters(file, parameters)
|
22
|
-
response = http.request(request)
|
23
|
-
url = parse_image_url_from(response)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
url
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def request_parameters(file, parameters)
|
32
|
-
required_parameters = { :import => 'image_data', :image_data => file, :returntype => 'text' }
|
33
|
-
scrub_parameters(required_parameters.merge(parameters))
|
34
|
-
end
|
35
|
-
|
36
|
-
def scrub_parameters(parameters)
|
37
|
-
scrubbed_parameters = {}
|
38
|
-
parameters.each do |key, value|
|
39
|
-
scrubbed_parameters[parameter_key(key)] = value
|
40
|
-
end
|
41
|
-
scrubbed_parameters
|
42
|
-
end
|
43
|
-
|
44
|
-
def parameter_key(key)
|
45
|
-
key = key.to_s
|
46
|
-
PICNIK_API_PARAMETERS.include?(key) ? '_' + key : key
|
47
|
-
end
|
48
|
-
|
49
|
-
def parse_image_url_from(response)
|
50
|
-
response.body
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|