dummy_image 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -6
- data/lib/dummy_image/image.rb +65 -0
- data/lib/dummy_image/middleware.rb +18 -0
- data/lib/dummy_image/request_parser.rb +50 -0
- data/lib/dummy_image/version.rb +3 -0
- data/spec/dummy_image/request_parser_spec.rb +8 -0
- data/spec/spec_helper.rb +4 -0
- metadata +12 -6
data/README.md
CHANGED
@@ -24,12 +24,10 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
This is a rack app that responds to the following URLS:
|
26
26
|
|
27
|
-
/
|
28
|
-
/width
|
29
|
-
/width/
|
30
|
-
/width/
|
31
|
-
|
32
|
-
Where *mount* is the location where the app is mounted
|
27
|
+
/height.(format)
|
28
|
+
/height/width.(format)
|
29
|
+
/height/width/foreground.(format)
|
30
|
+
/height/width/foreground/background.(format)
|
33
31
|
|
34
32
|
## Rails Usage
|
35
33
|
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module DummyImage
|
2
|
+
class Image
|
3
|
+
|
4
|
+
attr_reader :arguments
|
5
|
+
|
6
|
+
def self.find(arguments)
|
7
|
+
new(arguments).image
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize arguments
|
11
|
+
@arguments = arguments
|
12
|
+
end
|
13
|
+
|
14
|
+
def image_name
|
15
|
+
[width, height, fgcolor, bgcolor].join("_") + "." + format
|
16
|
+
end
|
17
|
+
|
18
|
+
def width
|
19
|
+
arguments.width
|
20
|
+
end
|
21
|
+
|
22
|
+
def height
|
23
|
+
arguments.height
|
24
|
+
end
|
25
|
+
|
26
|
+
def fgcolor
|
27
|
+
arguments.fgcolor
|
28
|
+
end
|
29
|
+
|
30
|
+
def bgcolor
|
31
|
+
arguments.bgcolor
|
32
|
+
end
|
33
|
+
|
34
|
+
def format
|
35
|
+
arguments.format
|
36
|
+
end
|
37
|
+
|
38
|
+
def file_path
|
39
|
+
["tmp", image_name].join "/"
|
40
|
+
end
|
41
|
+
|
42
|
+
def image
|
43
|
+
unless File.exists? file_path
|
44
|
+
create_image!
|
45
|
+
end
|
46
|
+
image_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def caption
|
50
|
+
[width, height].join(" x ")
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_image!
|
54
|
+
ImageBuilder.build width, height, fgcolor, bgcolor, caption, file_path
|
55
|
+
end
|
56
|
+
|
57
|
+
module ImageBuilder
|
58
|
+
extend self
|
59
|
+
def build width, height, fgcolor, bgcolor, caption, file_path
|
60
|
+
system "convert -size #{width}x#{height} -background '##{bgcolor}' -fill '##{fgcolor}' -gravity Center +pointsize label:' #{caption} ' #{file_path}"
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module DummyImage
|
2
|
+
module Middleware
|
3
|
+
extend self
|
4
|
+
IMAGE_DIR = ::File.expand_path("tmp")
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
requested_path = env["PATH_INFO"]
|
8
|
+
env_hash = { "PATH_INFO" => image_path(requested_path),
|
9
|
+
"REQUEST_METHOD" => "GET" }
|
10
|
+
Rack::File.new(IMAGE_DIR).call( env_hash )
|
11
|
+
end
|
12
|
+
|
13
|
+
def image_path(requested_path)
|
14
|
+
arguments = RequestParser.new(requested_path)
|
15
|
+
Image.find arguments
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module DummyImage
|
2
|
+
class RequestParser
|
3
|
+
|
4
|
+
def initialize(path)
|
5
|
+
@path, @format = path.split(".", 2)
|
6
|
+
end
|
7
|
+
|
8
|
+
def format
|
9
|
+
valid_format @format
|
10
|
+
end
|
11
|
+
|
12
|
+
def height
|
13
|
+
@height ||= valid_size(arguments[0]) || "300"
|
14
|
+
end
|
15
|
+
|
16
|
+
def width
|
17
|
+
@width ||= valid_size(arguments[1]) || height
|
18
|
+
end
|
19
|
+
|
20
|
+
def fgcolor
|
21
|
+
@fgcolor ||= valid_color(arguments[2]) || "333333"
|
22
|
+
end
|
23
|
+
|
24
|
+
def bgcolor
|
25
|
+
@bgcolor ||= valid_color(arguments[3]) || "CCCCCC"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def arguments
|
31
|
+
@arguments ||= @path.split("/").tap &:shift
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_format format
|
35
|
+
if ["png", "jpg", "jpeg", "gif"].include? format
|
36
|
+
format
|
37
|
+
else
|
38
|
+
"png"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def valid_size size
|
43
|
+
size[/^\d+$/] if size
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_color color
|
47
|
+
color[/^[\da-fA-F]+$/] if color
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -60,4 +60,12 @@ describe DummyImage::RequestParser do
|
|
60
60
|
its(:bgcolor) { should == "505050" }
|
61
61
|
its(:format) { should == "png" }
|
62
62
|
end
|
63
|
+
|
64
|
+
context "with invalid data" do
|
65
|
+
let(:path) { "/wut/who/waaa/derp" }
|
66
|
+
its(:height) { should == "300" }
|
67
|
+
its(:width) { should == "300" }
|
68
|
+
its(:fgcolor) { should == "333333" }
|
69
|
+
its(:bgcolor) { should == "CCCCCC" }
|
70
|
+
end
|
63
71
|
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dummy_image
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,12 +10,12 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-04-10 00:00:00.000000000 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rspec
|
18
|
-
requirement: &
|
18
|
+
requirement: &2153846780 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '2.8'
|
24
24
|
type: :development
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *2153846780
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rack-test
|
29
|
-
requirement: &
|
29
|
+
requirement: &2153846280 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - =
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: 0.6.1
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *2153846280
|
38
38
|
description: Generate dummy images via Rack
|
39
39
|
email:
|
40
40
|
- dave@davelyon.net
|
@@ -43,10 +43,15 @@ executables: []
|
|
43
43
|
extensions: []
|
44
44
|
extra_rdoc_files: []
|
45
45
|
files:
|
46
|
+
- lib/dummy_image/image.rb
|
47
|
+
- lib/dummy_image/middleware.rb
|
48
|
+
- lib/dummy_image/request_parser.rb
|
49
|
+
- lib/dummy_image/version.rb
|
46
50
|
- lib/dummy_image.rb
|
47
51
|
- README.md
|
48
52
|
- spec/dummy_image/image_spec.rb
|
49
53
|
- spec/dummy_image/request_parser_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
50
55
|
has_rdoc: true
|
51
56
|
homepage: http://davelyon.net
|
52
57
|
licenses: []
|
@@ -76,3 +81,4 @@ summary: A tiny rack app to generate dummy images on the fly for development.
|
|
76
81
|
test_files:
|
77
82
|
- spec/dummy_image/image_spec.rb
|
78
83
|
- spec/dummy_image/request_parser_spec.rb
|
84
|
+
- spec/spec_helper.rb
|