editable-image 0.22
Sign up to get free protection for your applications and to get access to all the features.
- data/README +7 -0
- data/editable-image.gemspec +15 -0
- data/lib/multipart.rb +37 -0
- data/lib/picnik.rb +57 -0
- data/test/files/logo.gif +0 -0
- data/test/picnik_test.rb +71 -0
- metadata +74 -0
data/README
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
This is currently alpha software.
|
2
|
+
|
3
|
+
A gem for interacting with online image editors. Currently, only Picnik is
|
4
|
+
supported. This is not a straight API wrapper.
|
5
|
+
|
6
|
+
See http://github.com/haikuwebdev/editable_image_sample/ for a sample Rails app
|
7
|
+
using attachment_fu and this gem to edit images.
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "editable-image"
|
3
|
+
s.version = "0.22"
|
4
|
+
s.date = "2008-06-12"
|
5
|
+
s.summary = "Simplified interface to web-based image editors."
|
6
|
+
s.email = "tj@haikuwebdev.com"
|
7
|
+
s.homepage = "http://github.com/haikuwebdev/editable-image/"
|
8
|
+
s.description = "Simplified interface to web-based image editors."
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["TJ Stankus"]
|
11
|
+
s.files = ["README", "editable-image.gemspec", "lib/multipart.rb", "lib/picnik.rb"]
|
12
|
+
s.test_files = ["test/picnik_test.rb", "test/files/logo.gif"]
|
13
|
+
s.add_dependency("mime-types", [">= 1.15"])
|
14
|
+
s.add_dependency("Shoulda", [">= 1.1.1"])
|
15
|
+
end
|
data/lib/multipart.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# See http://pivots.pivotallabs.com/users/damon/blog/articles/227-standup-04-27-07-testing-file-uploads
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'mime/types'
|
5
|
+
|
6
|
+
class Net::HTTP::Post
|
7
|
+
|
8
|
+
def multipart_params=(params = {})
|
9
|
+
boundary_token = [Array.new(8) {rand(256)}].join
|
10
|
+
self.content_type = "multipart/form-data; boundary=#{boundary_token}"
|
11
|
+
boundary_marker = "--#{boundary_token}\r\n"
|
12
|
+
self.body = params.map { |param_name, param_value|
|
13
|
+
boundary_marker + case param_value
|
14
|
+
when String
|
15
|
+
text_to_multipart(param_name, param_value)
|
16
|
+
when File
|
17
|
+
file_to_multipart(param_name, param_value)
|
18
|
+
end
|
19
|
+
}.join('') + "--#{boundary_token}--\r\n"
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def file_to_multipart(key, file)
|
25
|
+
filename = File.basename(file.path)
|
26
|
+
mime_types = MIME::Types.of(filename)
|
27
|
+
mime_type = mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
|
28
|
+
part = "Content-Disposition: form-data; name=#{key}; filename=#{filename}\r\n"
|
29
|
+
part += "Content-Transfer-Encoding: binary\r\n"
|
30
|
+
part += "Content-Type: #{mime_type}\r\n\r\n#{file.read}\r\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
def text_to_multipart(key,value)
|
34
|
+
"Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{value}\r\n"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/picnik.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'multipart'
|
3
|
+
|
4
|
+
class Picnik
|
5
|
+
|
6
|
+
PICNIK_API_PARAMETERS = %w(
|
7
|
+
apikey import returntype title export export_agent export_field
|
8
|
+
export_method export_title imageid original_thumb redirect replace exclude
|
9
|
+
host_name default_in default_out page close_target expand_button
|
10
|
+
)
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
def url(full_filename, parameters)
|
15
|
+
url = ''
|
16
|
+
File.open(full_filename) do |file|
|
17
|
+
http = Net::HTTP.new('www.picnik.com')
|
18
|
+
http.start do |http|
|
19
|
+
request = Net::HTTP::Post.new('/service/')
|
20
|
+
request.multipart_params = request_parameters(file, parameters)
|
21
|
+
response = http.request(request)
|
22
|
+
url = parse_image_url_from(response)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
url
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def request_parameters(file, parameters)
|
31
|
+
import_parameters = { :import => 'image_data', :image_data => file }
|
32
|
+
scrub_parameters(import_parameters.merge(parameters))
|
33
|
+
end
|
34
|
+
|
35
|
+
def scrub_parameters(parameters)
|
36
|
+
scrubbed_parameters = {}
|
37
|
+
parameters.each do |key, value|
|
38
|
+
scrubbed_parameters[parameter_key(key)] = value
|
39
|
+
end
|
40
|
+
scrubbed_parameters
|
41
|
+
end
|
42
|
+
|
43
|
+
def parameter_key(key)
|
44
|
+
key = key.to_s
|
45
|
+
PICNIK_API_PARAMETERS.include?(key) ? '_' + key : key
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_image_url_from(response)
|
49
|
+
body = response.body
|
50
|
+
start_index = body.index('>') + 1
|
51
|
+
end_index = body.index('<', start_index)
|
52
|
+
body.slice((start_index...end_index))
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/test/files/logo.gif
ADDED
Binary file
|
data/test/picnik_test.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/picnik'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
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
|
+
|
12
|
+
context "Request parameters" do
|
13
|
+
setup do
|
14
|
+
def Picnik.public_request_parameters(*args)
|
15
|
+
request_parameters(*args)
|
16
|
+
end
|
17
|
+
@file = File.open("files/logo.gif")
|
18
|
+
parameters = {:apikey => 'this_is_a_test_picnik_apikey'}
|
19
|
+
@request_parameters = Picnik.public_request_parameters(@file, parameters)
|
20
|
+
end
|
21
|
+
|
22
|
+
teardown do
|
23
|
+
@file.close
|
24
|
+
end
|
25
|
+
|
26
|
+
should "include _import with correct value" do
|
27
|
+
assert @request_parameters.keys.include?('_import')
|
28
|
+
end
|
29
|
+
|
30
|
+
should "have correct value for _import" do
|
31
|
+
assert_equal 'image_data', @request_parameters['_import']
|
32
|
+
end
|
33
|
+
|
34
|
+
should "include image_data" do
|
35
|
+
assert @request_parameters.keys.include?('image_data')
|
36
|
+
end
|
37
|
+
|
38
|
+
should "not have nil image_data" do
|
39
|
+
assert_not_nil @request_parameters['image_data']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "Parameter key" do
|
44
|
+
setup do
|
45
|
+
def Picnik.public_parameter_key(*args)
|
46
|
+
parameter_key(*args)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
should "be stringified" do
|
51
|
+
assert_equal 'foo', Picnik.public_parameter_key(:foo)
|
52
|
+
end
|
53
|
+
|
54
|
+
should "add underscore for Picnik API parameter" do
|
55
|
+
assert_equal '_apikey', Picnik.public_parameter_key(:apikey)
|
56
|
+
end
|
57
|
+
|
58
|
+
should "not add underscore for non-Picnik API parameter" do
|
59
|
+
assert_equal 'foo', Picnik.public_parameter_key('foo')
|
60
|
+
end
|
61
|
+
|
62
|
+
should "not add underscore for Picnik API parameter that already has one" do
|
63
|
+
assert_equal '_apikey', Picnik.public_parameter_key('_apikey')
|
64
|
+
end
|
65
|
+
|
66
|
+
should "keep underscore for non Picnik API parameter" do
|
67
|
+
assert_equal '_method', Picnik.public_parameter_key('_method')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: editable-image
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.22"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TJ Stankus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-12 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mime-types
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "1.15"
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: Shoulda
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.1.1
|
32
|
+
version:
|
33
|
+
description: Simplified interface to web-based image editors.
|
34
|
+
email: tj@haikuwebdev.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- README
|
43
|
+
- editable-image.gemspec
|
44
|
+
- lib/multipart.rb
|
45
|
+
- lib/picnik.rb
|
46
|
+
has_rdoc: false
|
47
|
+
homepage: http://github.com/haikuwebdev/editable-image/
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.1.1
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Simplified interface to web-based image editors.
|
72
|
+
test_files:
|
73
|
+
- test/picnik_test.rb
|
74
|
+
- test/files/logo.gif
|