ungulate 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'ungulate'
3
+ require 'ungulate/server'
4
4
 
5
5
  if ARGV[0].nil?
6
6
  $stderr.puts "Must provide a queue name after calling the server"
@@ -11,8 +11,8 @@ logger = Logger.new STDERR
11
11
 
12
12
  loop do
13
13
  begin
14
- sleep 1
15
- Ungulate.run(ARGV[0])
14
+ sleep ENV['SLEEP'] || 2
15
+ Ungulate::Server.run(ARGV[0])
16
16
  rescue StandardError => e
17
17
  logger.error e.message
18
18
  end
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'right_aws'
3
+ require 'RMagick'
4
+ require 'mime/types'
5
+
6
+ module Ungulate
7
+ class Job
8
+ attr_accessor :bucket, :key, :queue, :versions
9
+
10
+ def self.s3
11
+ @s3 ||=
12
+ RightAws::S3.new(ENV['AMAZON_ACCESS_KEY_ID'],
13
+ ENV['AMAZON_SECRET_ACCESS_KEY'])
14
+ end
15
+
16
+ def self.sqs
17
+ @sqs ||=
18
+ RightAws::SqsGen2.new(ENV['AMAZON_ACCESS_KEY_ID'],
19
+ ENV['AMAZON_SECRET_ACCESS_KEY'])
20
+ end
21
+
22
+ def self.pop(queue_name)
23
+ job = new
24
+ job.queue = sqs.queue queue_name
25
+ message = job.queue.pop
26
+ attributes = YAML.load message.to_s
27
+ job.attributes = attributes if attributes
28
+ job
29
+ end
30
+
31
+ def initialize
32
+ @logger = Ungulate::Server.logger
33
+ self.versions = []
34
+ end
35
+
36
+ def attributes=(options)
37
+ self.bucket = Job.s3.bucket(options[:bucket])
38
+ self.key = options[:key]
39
+ self.versions = options[:versions]
40
+ end
41
+
42
+ def processed_versions
43
+ @processed_versions ||=
44
+ versions.map do |name, instruction|
45
+ method, x, y = instruction
46
+ image = Magick::Image.from_blob(source).first
47
+ @logger.info "Performing #{method} with #{x}, #{y}"
48
+ [name, image.send(method, x, y)]
49
+ end
50
+ end
51
+
52
+ def source
53
+ if @source
54
+ @source
55
+ else
56
+ @logger.info "Grabbing source image #{key}"
57
+ @source = bucket.get key
58
+ end
59
+ end
60
+
61
+ def process
62
+ return false if processed_versions.empty?
63
+ processed_versions.each do |version, image|
64
+ version_key = version_key version
65
+ @logger.info "Storing #{version} @ #{version_key}"
66
+ bucket.put(version_key,
67
+ image.to_blob,
68
+ {},
69
+ 'public-read',
70
+ {'Content-Type' => MIME::Types.type_for(image.format).to_s})
71
+ end
72
+ end
73
+
74
+ def version_key(version)
75
+ dirname = File.dirname(key)
76
+ extname = File.extname(key)
77
+ basename = File.basename(key, extname)
78
+ "#{dirname}/#{basename}_#{version}#{extname}".sub(/^\.\//, '')
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,16 @@
1
+ require 'logger'
2
+ require 'ungulate/job'
3
+
4
+ module Ungulate
5
+ module Server
6
+ def self.logger
7
+ @logger ||= ::Logger.new STDOUT
8
+ end
9
+
10
+ def self.run(queue_name)
11
+ logger.info "Checking for job on #{queue_name}"
12
+ Job.pop(queue_name).process
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,15 @@
1
+ module ViewHelpers
2
+ def ungulate_upload_form_for(upload, &block)
3
+ concat(%Q(<form action="#{upload.bucket_url}" enctype="multipart/form-data" method="post">\n))
4
+ concat("<div>\n")
5
+ concat(%Q(<input name="key" type="hidden" value="#{upload.key}" />\n))
6
+ concat(%Q(<input name="AWSAccessKeyId" type="hidden" value="#{upload.access_key_id}" />\n))
7
+ concat(%Q(<input name="acl" type="hidden" value="#{upload.acl}" />\n))
8
+ concat(%Q(<input name="policy" type="hidden" value="#{upload.policy}" />\n))
9
+ concat(%Q(<input name="signature" type="hidden" value="#{upload.signature}" />\n))
10
+ concat(%Q(<input name="success_action_redirect" type="hidden" value="#{upload.success_action_redirect}" />\n))
11
+ yield
12
+ concat("\n</div>\n")
13
+ concat("</form>\n")
14
+ end
15
+ end
data/lib/ungulate.rb CHANGED
@@ -1,90 +1,11 @@
1
1
  require 'rubygems'
2
- require 'right_aws'
3
- require 'RMagick'
4
- require 'mime/types'
2
+ require 'ungulate/server'
5
3
 
6
4
  module Ungulate
7
- def self.logger
8
- @logger ||= Logger.new STDOUT
9
- end
10
-
11
- def self.run(queue_name)
12
- logger.info "Checking for job on #{queue_name}"
13
- Job.pop(queue_name).process
14
- end
15
-
16
- class Job
17
- attr_accessor :bucket, :key, :queue, :versions
18
-
19
- def self.s3
20
- @s3 ||=
21
- RightAws::S3.new(ENV['AMAZON_ACCESS_KEY_ID'],
22
- ENV['AMAZON_SECRET_ACCESS_KEY'])
23
- end
24
-
25
- def self.sqs
26
- @sqs ||=
27
- RightAws::SqsGen2.new(ENV['AMAZON_ACCESS_KEY_ID'],
28
- ENV['AMAZON_SECRET_ACCESS_KEY'])
29
- end
30
-
31
- def self.pop(queue_name)
32
- job = new
33
- job.queue = sqs.queue queue_name
34
- message = job.queue.pop
35
- attributes = YAML.load message.to_s
36
- job.attributes = attributes if attributes
37
- job
38
- end
39
-
40
- def initialize
41
- @logger = Ungulate.logger
42
- self.versions = []
43
- end
44
-
45
- def attributes=(options)
46
- self.bucket = Job.s3.bucket(options[:bucket])
47
- self.key = options[:key]
48
- self.versions = options[:versions]
49
- end
50
-
51
- def processed_versions
52
- @processed_versions ||=
53
- versions.map do |name, instruction|
54
- method, x, y = instruction
55
- image = Magick::Image.from_blob(source).first
56
- @logger.info "Performing #{method} with #{x}, #{y}"
57
- [name, image.send(method, x, y)]
58
- end
59
- end
60
-
61
- def source
62
- if @source
63
- @source
64
- else
65
- @logger.info "Grabbing source image #{key}"
66
- @source = bucket.get key
67
- end
68
- end
69
-
70
- def process
71
- return false if processed_versions.empty?
72
- processed_versions.each do |version, image|
73
- version_key = version_key version
74
- @logger.info "Storing #{version} @ #{version_key}"
75
- bucket.put(version_key,
76
- image.to_blob,
77
- {},
78
- 'public-read',
79
- {'Content-Type' => MIME::Types.type_for(image.format).to_s})
80
- end
81
- end
5
+ end
82
6
 
83
- def version_key(version)
84
- dirname = File.dirname(key)
85
- extname = File.extname(key)
86
- basename = File.basename(key, extname)
87
- "#{dirname}/#{basename}_#{version}#{extname}".sub(/^\.\//, '')
88
- end
89
- end
7
+ if defined? Rails
8
+ return if ActionView::Base.instance_methods.include_method? :ungulate_upload_form_for
9
+ require 'ungulate/view_helpers'
10
+ ActionView::Base.send :include, ViewHelpers
90
11
  end
@@ -1,41 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'ungulate/job'
2
3
 
3
4
  module Ungulate
4
- describe "run" do
5
- before do
6
- @versions = {
7
- :thumb => [ :resize_to_fit, 100, 200 ],
8
- :large => [ :resize_to_fit, 200, 300 ],
9
- }
10
-
11
- @data = mock('Data')
12
- @job = mock('Job',
13
- :versions => @versions,
14
- :source => @data,
15
- :process => nil,
16
- :store => nil)
17
-
18
- Job.stub(:pop).and_return(@job)
19
-
20
- @image = mock('RMagick::Image')
21
-
22
- @datum = mock('Datum')
23
- @data_array = [@datum]
24
-
25
- Magick::Image.stub(:from_blob).with(@data).and_return(@data_array)
26
- end
27
-
28
- after { Ungulate.run('queuename') }
29
-
30
- it "should pop a job from the provided queue" do
31
- Job.should_receive(:pop).with('queuename')
32
- end
33
-
34
- it "should process the job" do
35
- @job.should_receive(:process)
36
- end
37
- end
38
-
39
5
  describe Job do
40
6
  before do
41
7
  ENV['AMAZON_ACCESS_KEY_ID'] = 'test-key-id'
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'ungulate/server'
3
+
4
+ module Ungulate
5
+ describe Server do
6
+ describe "run" do
7
+ before do
8
+ @versions = {
9
+ :thumb => [ :resize_to_fit, 100, 200 ],
10
+ :large => [ :resize_to_fit, 200, 300 ],
11
+ }
12
+
13
+ @data = mock('Data')
14
+ @job = mock('Job',
15
+ :versions => @versions,
16
+ :source => @data,
17
+ :process => nil,
18
+ :store => nil)
19
+
20
+ Job.stub(:pop).and_return(@job)
21
+
22
+ @image = mock('RMagick::Image')
23
+
24
+ @datum = mock('Datum')
25
+ @data_array = [@datum]
26
+
27
+ Magick::Image.stub(:from_blob).with(@data).and_return(@data_array)
28
+ end
29
+
30
+ after { Ungulate::Server.run('queuename') }
31
+
32
+ it "should pop a job from the provided queue" do
33
+ Job.should_receive(:pop).with('queuename')
34
+ end
35
+
36
+ it "should process the job" do
37
+ @job.should_receive(:process)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'ungulate/view_helpers'
3
+
4
+ class Includer
5
+ include ViewHelpers
6
+ attr_accessor :output_buffer
7
+
8
+ def concat(string)
9
+ self.output_buffer << string
10
+ end
11
+ end
12
+
13
+ module Ungulate
14
+ describe ViewHelpers do
15
+ describe "ungulate_upload_form_for" do
16
+ def helper
17
+ @helper ||= Includer.new
18
+ end
19
+
20
+ before do
21
+ @file_upload = mock('Ungulate::FileUpload',
22
+ :access_key_id => 'awsaccesskeyid',
23
+ :acl => 'private',
24
+ :bucket_url => 'http://static.test.s3.amazonaws.com/',
25
+ :key => 'some/key',
26
+ :policy => 'thepolicy',
27
+ :signature => 'thesignature',
28
+ :success_action_redirect => '/some/place')
29
+ helper.output_buffer = ''
30
+ helper.ungulate_upload_form_for(@file_upload) do
31
+ helper.concat('text in the middle')
32
+ end
33
+ end
34
+
35
+ it "should have a complete form" do
36
+ helper.output_buffer.should == <<-HTML
37
+ <form action="http://static.test.s3.amazonaws.com/" enctype="multipart/form-data" method="post">
38
+ <div>
39
+ <input name="key" type="hidden" value="some/key" />
40
+ <input name="AWSAccessKeyId" type="hidden" value="awsaccesskeyid" />
41
+ <input name="acl" type="hidden" value="private" />
42
+ <input name="policy" type="hidden" value="thepolicy" />
43
+ <input name="signature" type="hidden" value="thesignature" />
44
+ <input name="success_action_redirect" type="hidden" value="/some/place" />
45
+ text in the middle
46
+ </div>
47
+ </form>
48
+ HTML
49
+ end
50
+ end
51
+ end
52
+ end
data/ungulate.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ungulate}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Bruce"]
12
- s.date = %q{2010-04-11}
12
+ s.date = %q{2010-04-15}
13
13
  s.default_executable = %q{ungulate_server.rb}
14
14
  s.description = %q{WIP}
15
15
  s.email = %q{andrew@camelpunch.com}
@@ -35,10 +35,15 @@ Gem::Specification.new do |s|
35
35
  "features/support.rb",
36
36
  "lib/ungulate.rb",
37
37
  "lib/ungulate/file_upload.rb",
38
+ "lib/ungulate/job.rb",
39
+ "lib/ungulate/server.rb",
40
+ "lib/ungulate/view_helpers.rb",
38
41
  "spec/spec.opts",
39
42
  "spec/spec_helper.rb",
40
43
  "spec/ungulate/file_upload_spec.rb",
41
- "spec/ungulate_spec.rb",
44
+ "spec/ungulate/job_spec.rb",
45
+ "spec/ungulate/server_spec.rb",
46
+ "spec/ungulate/view_helpers_spec.rb",
42
47
  "ungulate.gemspec"
43
48
  ]
44
49
  s.homepage = %q{http://github.com/camelpunch/ungulate}
@@ -49,7 +54,9 @@ Gem::Specification.new do |s|
49
54
  s.test_files = [
50
55
  "spec/spec_helper.rb",
51
56
  "spec/ungulate/file_upload_spec.rb",
52
- "spec/ungulate_spec.rb"
57
+ "spec/ungulate/job_spec.rb",
58
+ "spec/ungulate/server_spec.rb",
59
+ "spec/ungulate/view_helpers_spec.rb"
53
60
  ]
54
61
 
55
62
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Bruce
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-11 00:00:00 +01:00
17
+ date: 2010-04-15 00:00:00 +01:00
18
18
  default_executable: ungulate_server.rb
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -126,10 +126,15 @@ files:
126
126
  - features/support.rb
127
127
  - lib/ungulate.rb
128
128
  - lib/ungulate/file_upload.rb
129
+ - lib/ungulate/job.rb
130
+ - lib/ungulate/server.rb
131
+ - lib/ungulate/view_helpers.rb
129
132
  - spec/spec.opts
130
133
  - spec/spec_helper.rb
131
134
  - spec/ungulate/file_upload_spec.rb
132
- - spec/ungulate_spec.rb
135
+ - spec/ungulate/job_spec.rb
136
+ - spec/ungulate/server_spec.rb
137
+ - spec/ungulate/view_helpers_spec.rb
133
138
  - ungulate.gemspec
134
139
  has_rdoc: true
135
140
  homepage: http://github.com/camelpunch/ungulate
@@ -164,4 +169,6 @@ summary: Process images using Amazon SQS and S3
164
169
  test_files:
165
170
  - spec/spec_helper.rb
166
171
  - spec/ungulate/file_upload_spec.rb
167
- - spec/ungulate_spec.rb
172
+ - spec/ungulate/job_spec.rb
173
+ - spec/ungulate/server_spec.rb
174
+ - spec/ungulate/view_helpers_spec.rb