pictopus 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  = Pictopus
2
2
 
3
- Pictopus is a Rails developer tool that allows you to use ImageMagick to
3
+ Pictopus is a Rack developer tool that allows you to use ImageMagick to
4
4
  automatically generate gradient images to use in your design. It is
5
- implemented using Rails Engines for the routing and simply runs ImageMagick
5
+ implemented using Rack middleware for the routing and simply runs ImageMagick
6
6
  through the command line (using the 'convert' command).
7
7
 
8
8
  == Installation
@@ -16,10 +16,16 @@ file (NOTE: It is <b>not</b> recommended to include the Pictopus gem in
16
16
  your production environment).
17
17
 
18
18
  config.gem 'pictopus'
19
+
20
+ To include it in your Rack application, simply add these lines to your
21
+ rackup:
22
+
23
+ require 'rack/pictopus'
24
+ use Rack::Pictopus, '/path/to/your/static/dir'
19
25
 
20
26
  == Usage
21
27
 
22
- If you have ImageMagick installed in your Rails app, you can create
28
+ If you have ImageMagick installed in your system, you can create
23
29
  arbitrary gradient images just by naming them as a URL. They will
24
30
  automatically be created by ImageMagick and cached to your public
25
31
  directory, meaning that the generation happens once. You can then
@@ -64,10 +70,10 @@ similar sites to create additional "commands".
64
70
 
65
71
  * Fork the project.
66
72
  * Make your feature addition or bug fix.
67
- * Add tests for it. This is important so I don't break it in a future version unintentionally.
73
+ * <b>Add tests for it.</b> This is important so I don't break it in a future version unintentionally.
68
74
  * Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
69
75
  * Send us a pull request. Bonus points for topic branches.
70
76
 
71
77
  == Copyright
72
78
 
73
- Copyright (c) 2009 Michael Bleigh. See LICENSE for details.
79
+ Copyright (c) 2009 Intridea, Inc. and Michael Bleigh See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -2,7 +2,7 @@ module Pictopus
2
2
  class CommandLineError < StandardError; end
3
3
 
4
4
  class << self
5
- def options
5
+ def default_options
6
6
  @options ||= {
7
7
  :command_path => nil,
8
8
  :log => true,
@@ -11,18 +11,20 @@ module Pictopus
11
11
  }
12
12
  end
13
13
 
14
- def run(cmd, params="", expected_outcodes = 0)
14
+ def run(cmd, params="", options = {})
15
+ options[:expected_outcodes] ||= 0
15
16
  command = %Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")
16
- command = "#{command} 2>#{bit_bucket}" if Pictopus.options[:swallow_stderr]
17
- Rails.logger.info(command) if Pictopus.options[:log_command]
18
- output = `#{command}`
19
- unless Array(expected_outcodes).include?($?.exitstatus)
17
+ command = "#{command} 2>#{bit_bucket}" if Pictopus.default_options[:swallow_stderr]
18
+ #Rails.logger.info(command) if Pictopus.default_options[:log_command]
19
+ puts command
20
+ output = system(command)
21
+ unless Array(options[:expected_outcodes]).include?($?.exitstatus)
20
22
  raise Pictopus::CommandLineError, "Error while running #{cmd}"
21
23
  end
22
24
  end
23
25
 
24
26
  def path_for_command(command)
25
- path = [options[:command_path], command].compact!
27
+ path = [default_options[:command_path], command].compact!
26
28
  File.join(*path)
27
29
  end
28
30
 
@@ -30,4 +32,6 @@ module Pictopus
30
32
  File.exists?("/dev/null") ? "/dev/null" : "NUL"
31
33
  end
32
34
  end
33
- end
35
+ end
36
+
37
+ require 'pictopus/gradient'
@@ -39,6 +39,7 @@ module Pictopus
39
39
  # Run the actual command to generate the image
40
40
  # for this instance.
41
41
  def run!
42
+ FileUtils.mkdir_p(output_directory) unless File.directory?(File.dirname(output_directory))
42
43
  Pictopus.run('convert', self.to_args)
43
44
  end
44
45
 
@@ -65,7 +66,7 @@ module Pictopus
65
66
  end
66
67
 
67
68
  def output_directory # :nodoc:
68
- File.join(Rails.root, 'public', 'images', 'pictopus', 'gradient')
69
+ File.join(@options[:output_path], 'images', 'pictopus', 'gradient')
69
70
  end
70
71
 
71
72
  def file_name # :nodoc:
@@ -0,0 +1,36 @@
1
+ require 'pictopus'
2
+
3
+ module Rack
4
+ class Pictopus
5
+ def initialize(app, output_path)
6
+ @app = app
7
+ @root = ::File.expand_path(output_path)
8
+ raise ArgumentError, "#{output_path} is not a directory." unless ::File.directory?(@root)
9
+ end
10
+
11
+ def call(env)
12
+ unless env["PATH_INFO"] =~ /.*\/images\/pictopus\/.*\/.*/
13
+ return @app.call(env)
14
+ end
15
+
16
+ request = ::Rack::Request.new(env)
17
+ kind, command = env["PATH_INFO"].match(/images\/pictopus\/(.*)\/(.*)/i)[1..-1]
18
+
19
+ if ::File.exist?(@root + env["PATH_INFO"])
20
+ return ::Rack::File.new(@root).call(env)
21
+ end
22
+
23
+ case kind
24
+ when "gradient"
25
+ opts = {
26
+ :output_path => @root
27
+ }
28
+ opts[:start_color], opts[:end_color], opts[:orientation], opts[:size], opts[:format] = command.split('.')
29
+ ::Pictopus::Gradient.new(opts).run!
30
+
31
+ # Redirect to this URL since it will now be served.
32
+ [301, {'Location' => request.url}, 'Redirecting to created image.']
33
+ end
34
+ end
35
+ end
36
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pictopus}
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Bleigh"]
12
- s.date = %q{2009-10-27}
12
+ s.date = %q{2009-11-15}
13
13
  s.description = %q{A Rails Engine to dynamically generate images for your application while in development mode.}
14
14
  s.email = %q{michael@intridea.com}
15
15
  s.extra_rdoc_files = [
@@ -23,15 +23,14 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
- "app/controllers/pictopus_controller.rb",
27
- "config/pictopus_routes.rb",
28
26
  "lib/pictopus.rb",
29
27
  "lib/pictopus/gradient.rb",
28
+ "lib/rack/pictopus.rb",
30
29
  "pictopus.gemspec",
31
30
  "rails/init.rb",
32
- "spec/controllers/pictopus_controller_spec.rb",
33
31
  "spec/pictopus/gradient_spec.rb",
34
32
  "spec/pictopus_spec.rb",
33
+ "spec/rack/pictopus_spec.rb",
35
34
  "spec/spec.opts",
36
35
  "spec/spec_helper.rb"
37
36
  ]
@@ -41,9 +40,9 @@ Gem::Specification.new do |s|
41
40
  s.rubygems_version = %q{1.3.5}
42
41
  s.summary = %q{A Rails Engine for dynamically generating images for your application design.}
43
42
  s.test_files = [
44
- "spec/controllers/pictopus_controller_spec.rb",
45
- "spec/pictopus/gradient_spec.rb",
43
+ "spec/pictopus/gradient_spec.rb",
46
44
  "spec/pictopus_spec.rb",
45
+ "spec/rack/pictopus_spec.rb",
47
46
  "spec/spec_helper.rb"
48
47
  ]
49
48
 
@@ -1,19 +1,3 @@
1
- class ActionController::Routing::RouteSet
2
- def load_routes_with_pictopus!
3
- pictopus_routes = File.join(File.dirname(__FILE__),
4
- *%w[.. config pictopus_routes.rb])
5
- unless configuration_files.include? pictopus_routes
6
- add_configuration_file(pictopus_routes)
7
- end
8
- load_routes_without_pictopus!
9
- end
1
+ require 'rack/pictopus'
10
2
 
11
- alias_method_chain :load_routes!, :pictopus
12
- end
13
-
14
- require 'pictopus'
15
-
16
- if Rails.env == 'test'
17
- config.gem 'rspec', :lib => false
18
- config.gem 'remarkable', :lib => false
19
- end
3
+ config.middleware.use(::Rack::Pictopus, File.join(RAILS_ROOT, 'public'))
@@ -20,7 +20,8 @@ describe Pictopus::Gradient do
20
20
  :end_color => 'bb33ff',
21
21
  :orientation => 'v',
22
22
  :size => '30x40',
23
- :format => 'gif'
23
+ :format => 'gif',
24
+ :output_path => "/dev/null"
24
25
  )
25
26
  end
26
27
 
@@ -46,5 +47,9 @@ describe Pictopus::Gradient do
46
47
  @g.orientation = 'horizontal'
47
48
  @g.to_args.should be_include('-rotate -90')
48
49
  end
50
+
51
+ it 'should add the output path' do
52
+ @g.to_args.should match(/\/dev\/null\/images\/pictopus\/gradient\/aaa\.bb33ff\.v\.30x40\.gif$/)
53
+ end
49
54
  end
50
55
  end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require 'rack/pictopus'
4
+
5
+ describe Rack::Pictopus do
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ Rack::Pictopus.new(nil, File.expand_path(File.dirname(__FILE__) + '/../'))
10
+ end
11
+
12
+ describe '#gradient' do
13
+ before do
14
+ @opts = {
15
+ :start_color => 'f00',
16
+ :end_color => '00f',
17
+ :size => '50',
18
+ :orientation => 'h',
19
+ :format => 'jpg',
20
+ :output_path => File.expand_path(File.dirname(__FILE__) + '/../')
21
+ }
22
+ end
23
+
24
+ it 'should create a Pictopus::Gradient for the provided params' do
25
+ Pictopus::Gradient.should_receive(:new).with(@opts).and_return(mock('Pictopus::Gradient', :run! => true))
26
+ get '/images/pictopus/gradient/f00.00f.h.50.jpg'
27
+ end
28
+
29
+ it 'should call run! on the gradient' do
30
+ @g = Pictopus::Gradient.new(@opts)
31
+ Pictopus::Gradient.stub!(:new).and_return(@g)
32
+ @g.should_receive(:run!).and_return(true)
33
+ get '/images/pictopus/gradient/f00.00f.h.50.jpg'
34
+ end
35
+
36
+ it 'should redirect to itself (since the image will be generated)' do
37
+ get '/images/pictopus/gradient/f00.00f.h.50.jpg'
38
+
39
+ last_response.status.should == 301
40
+ last_response.headers['location'].should be_include('/images/pictopus/gradient/f00.00f.h.50.jpg')
41
+ end
42
+
43
+ it 'should serve up the file if it already exists' do
44
+ get '/images/pictopus/gradient/f00.00f.h.50.jpg'
45
+ get '/images/pictopus/gradient/f00.00f.h.50.jpg'
46
+ last_response.status.should == 200
47
+ end
48
+ end
49
+
50
+ after do
51
+ FileUtils.rm_rf(File.dirname(__FILE__) + '/../images')
52
+ end
53
+ end
@@ -1,15 +1,8 @@
1
- begin
2
- require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
- rescue LoadError
4
- puts "You need to install rspec in your base app"
5
- exit
6
- end
7
-
1
+ require 'rubygems'
8
2
  require 'pictopus'
9
3
  require 'spec'
10
4
  require 'spec/autorun'
11
-
12
- require 'remarkable_rails'
5
+ require 'rack/test'
13
6
 
14
7
  Spec::Runner.configure do |config|
15
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pictopus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-27 00:00:00 -04:00
12
+ date: 2009-11-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,15 +48,14 @@ files:
48
48
  - README.rdoc
49
49
  - Rakefile
50
50
  - VERSION
51
- - app/controllers/pictopus_controller.rb
52
- - config/pictopus_routes.rb
53
51
  - lib/pictopus.rb
54
52
  - lib/pictopus/gradient.rb
53
+ - lib/rack/pictopus.rb
55
54
  - pictopus.gemspec
56
55
  - rails/init.rb
57
- - spec/controllers/pictopus_controller_spec.rb
58
56
  - spec/pictopus/gradient_spec.rb
59
57
  - spec/pictopus_spec.rb
58
+ - spec/rack/pictopus_spec.rb
60
59
  - spec/spec.opts
61
60
  - spec/spec_helper.rb
62
61
  has_rdoc: true
@@ -88,7 +87,7 @@ signing_key:
88
87
  specification_version: 3
89
88
  summary: A Rails Engine for dynamically generating images for your application design.
90
89
  test_files:
91
- - spec/controllers/pictopus_controller_spec.rb
92
90
  - spec/pictopus/gradient_spec.rb
93
91
  - spec/pictopus_spec.rb
92
+ - spec/rack/pictopus_spec.rb
94
93
  - spec/spec_helper.rb
@@ -1,10 +0,0 @@
1
- class PictopusController < ApplicationController
2
- def gradient
3
- @opts = {}
4
- Pictopus::Gradient::OPTION_KEYS.each{|k| @opts[k] = params[k]}
5
-
6
- Pictopus::Gradient.new(@opts).run!
7
- redirect_to request.request_uri
8
- end
9
- end
10
-
@@ -1,5 +0,0 @@
1
- ActionController::Routing::Routes.draw do |map|
2
- map.with_options(:name_prefix => 'pictopus_', :controller => 'pictopus') do |pictopus|
3
- pictopus.gradient '/images/pictopus/gradient/:start_color.:end_color.:orientation.:size.:format', :action => 'gradient'
4
- end
5
- end
@@ -1,41 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- describe PictopusController do
4
- should_route :get, '/images/pictopus/gradient/fff.000.v.100.png',
5
- :controller => 'pictopus',
6
- :action => 'gradient',
7
- :start_color => 'fff',
8
- :end_color => '000',
9
- :orientation => 'v',
10
- :size => '100',
11
- :format => 'png'
12
-
13
- describe '#gradient' do
14
- before do
15
- @opts = {
16
- :start_color => 'f00',
17
- :end_color => '00f',
18
- :size => '50',
19
- :orientation => 'h',
20
- :format => 'jpg'
21
- }
22
- end
23
-
24
- it 'should create a Pictopus::Gradient for the provided params' do
25
- Pictopus::Gradient.should_receive(:new).with(@opts).and_return(mock('Pictopus::Gradient', :run! => true))
26
- get :gradient, @opts.dup
27
- end
28
-
29
- it 'should call run! on the gradient' do
30
- @g = Pictopus::Gradient.new(@opts)
31
- Pictopus::Gradient.stub!(:new).and_return(@g)
32
- @g.should_receive(:run!).and_return(true)
33
- get :gradient, @opts.dup
34
- end
35
-
36
- it 'should redirect to itself (since the image will be generated)' do
37
- get :gradient, @opts.dup
38
- response.should redirect_to request.request_uri
39
- end
40
- end
41
- end