imagery 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -99,14 +99,21 @@ Then you may proceed using it.
99
99
  # For cases where we want to use S3 for some and normal filesystem for others
100
100
  class S3Photo < Imagery::Model
101
101
  include Imagery::S3
102
- self.s3_bucket = 'my-bucket'
102
+ s3_bucket 'my-bucket'
103
103
  end
104
104
 
105
105
  # then maybe some other files are using cloudfront
106
106
  class CloudfrontPhoto < Imagery::Model
107
107
  include Imagery::S3
108
- self.s3_bucket = 'my-bucket'
109
- self.s3_distribution_domain = 'assets.site.com'
108
+ s3_bucket 'my-bucket'
109
+ s3_distribution_domain 'assets.site.com'
110
+ end
111
+
112
+ # some might be using S3 EU, in which case you can specify the s3_host
113
+ class CustomS3Host < Imagery::Model
114
+ include Imagery::S3
115
+ s3_host 'http://my.custom.host'
116
+ s3_bucket 'my-bucket-name'
110
117
  end
111
118
 
112
119
  3. Flexibility and Extensibility
@@ -133,7 +140,7 @@ Now you can just start using it:
133
140
 
134
141
  class Imagery::Model
135
142
  include Imagery::S3
136
- self.s3_bucket = 'my-bucket'
143
+ s3_bucket 'my-bucket'
137
144
  end
138
145
 
139
146
  i = Imagery.new(Photo.new(1001))
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/imagery.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{imagery}
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 = ["Cyril David"]
12
- s.date = %q{2010-05-31}
12
+ s.date = %q{2010-06-07}
13
13
  s.description = %q{Uses ImageMagick directly underneath. Nuff said.}
14
14
  s.email = %q{cyx.ucron@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,12 +26,14 @@ Gem::Specification.new do |s|
26
26
  "imagery.gemspec",
27
27
  "lib/imagery.rb",
28
28
  "lib/imagery/faking.rb",
29
+ "lib/imagery/missing.rb",
29
30
  "lib/imagery/model.rb",
30
31
  "lib/imagery/s3.rb",
31
32
  "lib/imagery/test.rb",
32
33
  "test/fixtures/lake.jpg",
33
34
  "test/helper.rb",
34
35
  "test/test_imagery.rb",
36
+ "test/test_missing.rb",
35
37
  "test/test_with_s3.rb"
36
38
  ]
37
39
  s.homepage = %q{http://github.com/sinefunc/imagery}
@@ -42,6 +44,7 @@ Gem::Specification.new do |s|
42
44
  s.test_files = [
43
45
  "test/helper.rb",
44
46
  "test/test_imagery.rb",
47
+ "test/test_missing.rb",
45
48
  "test/test_with_s3.rb"
46
49
  ]
47
50
 
data/lib/imagery.rb CHANGED
@@ -2,12 +2,13 @@ require 'escape'
2
2
  require 'fileutils'
3
3
 
4
4
  module Imagery
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
 
7
- autoload :Model, "imagery/model"
8
- autoload :Faking, "imagery/faking"
9
- autoload :S3, "imagery/s3"
10
- autoload :Test, "imagery/test"
7
+ autoload :Model, "imagery/model"
8
+ autoload :Faking, "imagery/faking"
9
+ autoload :S3, "imagery/s3"
10
+ autoload :Missing, "imagery/missing"
11
+ autoload :Test, "imagery/test"
11
12
 
12
13
  # Syntactic sugar for Imagery::Model::new
13
14
  # @see Imagery::Model#initialize for details
@@ -33,5 +33,11 @@ module Imagery
33
33
 
34
34
  super
35
35
  end
36
+
37
+ def delete
38
+ return true if self.class.mode == :fake
39
+
40
+ super
41
+ end
36
42
  end
37
43
  end
@@ -0,0 +1,19 @@
1
+ module Imagery
2
+ module Missing
3
+ def existing=(existing)
4
+ @existing = existing
5
+ end
6
+
7
+ def existing
8
+ @existing
9
+ end
10
+
11
+ def url(size = self.default_size)
12
+ if existing.to_s.empty?
13
+ return ['', 'missing', namespace, filename(size)].join('/')
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/imagery/model.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Imagery
2
2
  class Model
3
- UnknownSize = Class.new(StandardError)
3
+ UnknownSize = Class.new(StandardError)
4
+ UndefinedRoot = Class.new(StandardError)
4
5
 
5
6
  @@directory = 'public/system'
6
7
  @@default = { :original => ["1920x1200>"] }
@@ -90,31 +91,47 @@ module Imagery
90
91
 
91
92
  root_path(directory, namespace, key, filename(size))
92
93
  end
93
-
94
- # Gives the absolute URI path for use in a web context.
94
+
95
+ # The Web module is basically here to let plugins override
96
+ # the url as they see fit.
95
97
  #
96
- # Photo = Class.new(Struct.new(:id))
97
- # i = Imagery.new(Photo.new(1001))
98
- # i.url(:original) == '/system/photo/1001/original.png
99
- # # => true
100
- #
101
- # i.file(:thumb)
102
- # # raise Imagery::Model::UnknownSize
103
- #
104
- # i.sizes = { :thumb => ["100x100"] }
105
- # i.file(:thumb) == '/system/photo/1001/thumb.png
106
- # # => true
98
+ # @example
99
+ # module FunkyUrls
100
+ # def url(size = default_size)
101
+ # super.gsub('/system', '/funky')
102
+ # end
103
+ # end
107
104
  #
108
- # @param [Symbol] size the size specific url.
109
- # @raise [UnknownSize] if the size is not found in
110
- # Imagery::Model#sizes.
111
- # @return [String] the absolute URI path of the size specific url e.g.
112
- # /system/photo/1/thumb.png
113
- # where photo is the namespace and 1 is the key.
114
- def url(size = self.default_size)
115
- file(size).split('public').last
105
+ # class Imagery::Model
106
+ # include FunkyUrls
107
+ # end
108
+ module Web
109
+ # Gives the absolute URI path for use in a web context.
110
+ #
111
+ # Photo = Class.new(Struct.new(:id))
112
+ # i = Imagery.new(Photo.new(1001))
113
+ # i.url(:original) == '/system/photo/1001/original.png
114
+ # # => true
115
+ #
116
+ # i.file(:thumb)
117
+ # # raise Imagery::Model::UnknownSize
118
+ #
119
+ # i.sizes = { :thumb => ["100x100"] }
120
+ # i.file(:thumb) == '/system/photo/1001/thumb.png
121
+ # # => true
122
+ #
123
+ # @param [Symbol] size the size specific url.
124
+ # @raise [UnknownSize] if the size is not found in
125
+ # Imagery::Model#sizes.
126
+ # @return [String] the absolute URI path of the size specific url e.g.
127
+ # /system/photo/1/thumb.png
128
+ # where photo is the namespace and 1 is the key.
129
+ def url(size = self.default_size)
130
+ file(size).split('public').last
131
+ end
116
132
  end
117
-
133
+ include Web
134
+
118
135
  # This module is basically here so that plugins like Imagery::S3
119
136
  # can override #save and #delete and call super.
120
137
  module Persistence
@@ -190,7 +207,7 @@ module Imagery
190
207
 
191
208
  def root(root = defined?(ROOT_DIR) && ROOT_DIR)
192
209
  @root ||= root if root
193
- @root
210
+ @root || raise(UndefinedRoot, "You must define Imagery::Model#root or have a ROOT_DIR constant present")
194
211
  end
195
212
 
196
213
  def root_path(*args)
data/lib/imagery/s3.rb CHANGED
@@ -3,13 +3,49 @@ require 'aws/s3'
3
3
  module Imagery
4
4
  module S3
5
5
  def self.included(base)
6
+ base.extend Configs
6
7
  class << base
7
- attr_accessor :s3_bucket, :s3_distribution_domain
8
+ attr_writer :s3_bucket, :s3_distribution_domain, :s3_host
8
9
  end
9
10
  end
10
-
11
- S3_HOST = "http://s3.amazonaws.com"
11
+
12
+ module Configs
13
+ def s3_bucket(bucket = nil)
14
+ @s3_bucket = bucket if bucket
15
+ @s3_bucket || raise(UndefinedBucket, BUCKET_ERROR_MSG)
16
+ end
17
+
18
+ BUCKET_ERROR_MSG = (<<-MSG).gsub(/^ {6}/, '')
19
+
20
+ You need to define a bucket name. Example:
21
+
22
+ class Imagery::Model
23
+ include Imagery::S3
24
+
25
+ s3_bucket 'my-bucket-name'
26
+ end
27
+ MSG
28
+
29
+ def s3_distribution_domain(domain = nil)
30
+ @s3_distribution_domain = domain if domain
31
+ @s3_distribution_domain
32
+ end
12
33
 
34
+ # Allows you to customize the S3 host. Usually happens when you use
35
+ # amazon S3 EU.
36
+ #
37
+ # @param [String] host the custom host you want to use instead.
38
+ # @return [String] the s3 host currently set.
39
+ def s3_host(host = nil)
40
+ @s3_host = host if host
41
+ @s3_host || S3_HOST
42
+ end
43
+ end
44
+
45
+ UndefinedBucket = Class.new(StandardError)
46
+
47
+ S3_HOST = "http://s3.amazonaws.com"
48
+
13
49
  # Returns a url publicly accessible. If a distribution domain is set,
14
50
  # then the url will be based on that.
15
51
  #
@@ -18,7 +54,7 @@ module Imagery
18
54
  # class Imagery::Model
19
55
  # include Imagery::S3
20
56
  #
21
- # self.s3_bucket = 'bucket-name'
57
+ # s3_bucket 'bucket-name'
22
58
  # end
23
59
  #
24
60
  # Photo = Class.new(Struct.new(:id))
@@ -36,13 +72,13 @@ module Imagery
36
72
  #
37
73
  # class CloudFront < Imagery::Model
38
74
  # include Imagery::S3
39
- # self.s3_bucket = 'cloudfront'
40
- # self.s3_distribution_domain = 'assets.site.com'
75
+ # s3_bucket 'cloudfront'
76
+ # s3_distribution_domain 'assets.site.com'
41
77
  # end
42
78
  #
43
79
  # class RegularS3 < Imagery::Model
44
80
  # include Imagery::S3
45
- # self.s3_bucket = 'cloudfront'
81
+ # s3_bucket 'cloudfront'
46
82
  # end
47
83
  #
48
84
  # @param [Symbol] size the preferred size you want for the url.
@@ -51,7 +87,7 @@ module Imagery
51
87
  if domain = self.class.s3_distribution_domain
52
88
  [domain, namespace, key, filename(size)].join('/')
53
89
  else
54
- [S3_HOST, self.class.s3_bucket, namespace, key, filename(size)].join('/')
90
+ [self.class.s3_host, self.class.s3_bucket, namespace, key, filename(size)].join('/')
55
91
  end
56
92
  end
57
93
 
Binary file
data/test/test_imagery.rb CHANGED
@@ -16,6 +16,14 @@ class TestImagery < Test::Unit::TestCase
16
16
  assert_equal '1001', Imagery.new(Photo.new(1001)).key
17
17
  end
18
18
 
19
+ test "root_path when root not defined" do
20
+ imagery = Imagery.new(Photo.new(1001))
21
+
22
+ assert_raise Imagery::Model::UndefinedRoot do
23
+ imagery.file
24
+ end
25
+ end
26
+
19
27
  test "root_path when no ROOT_DIR" do
20
28
  imagery = Imagery.new(Photo.new(1001))
21
29
  imagery.root = '/'
@@ -119,7 +127,7 @@ class TestImagery < Test::Unit::TestCase
119
127
 
120
128
  test "when mode == :fake" do
121
129
  time = Benchmark.realtime {
122
- Imagery.faked {
130
+ Imagery::Model.faked {
123
131
  assert @imagery.save(File.open(FIXTURES + '/lake.jpg'))
124
132
  }
125
133
  }
@@ -0,0 +1,30 @@
1
+ require "helper"
2
+
3
+ class MissingTest < Test::Unit::TestCase
4
+ Photo = Class.new(Struct.new(:id))
5
+
6
+ test "adding it using extend" do
7
+ imagery = Imagery.new(Photo.new(1001))
8
+ imagery.extend Imagery::Missing
9
+ imagery.existing = ""
10
+ assert_equal '/missing/photo/original.png', imagery.url
11
+ end
12
+
13
+ class WithMissing < Imagery::Model
14
+ include Imagery::Missing
15
+ end
16
+
17
+ test "adding it using include" do
18
+ imagery = WithMissing.new(Photo.new(1001))
19
+ imagery.existing = ""
20
+
21
+ assert_equal '/missing/photo/original.png', imagery.url
22
+ end
23
+
24
+ test "still returns as normal when not missing" do
25
+ imagery = WithMissing.new(Photo.new(1001))
26
+ imagery.root = '/tmp'
27
+ imagery.existing = 'lake.jpg'
28
+ assert_equal '/system/photo/1001/original.png', imagery.url
29
+ end
30
+ end
data/test/test_with_s3.rb CHANGED
@@ -5,12 +5,26 @@ class WithS3Test < Test::Unit::TestCase
5
5
  include Imagery::Faking
6
6
  include Imagery::S3
7
7
 
8
- self.s3_bucket = "tmp-bucket-name"
8
+ s3_bucket "tmp-bucket-name"
9
9
  end
10
10
 
11
+ class NoBucket < Imagery::Model
12
+ include Imagery::Faking
13
+ include Imagery::S3
14
+ end
15
+
16
+ class WithS3CustomHost < Imagery::Model
17
+ include Imagery::Faking
18
+ include Imagery::S3
19
+
20
+ s3_bucket "tmp-bucket-name"
21
+ s3_host "http://test.host"
22
+ end
23
+
24
+
11
25
  SuperSecretPhoto = Class.new(Struct.new(:id))
12
26
 
13
- test "urls" do
27
+ test "default urls" do
14
28
  imagery = WithS3.new(SuperSecretPhoto.new(1001))
15
29
  imagery.root = '/tmp'
16
30
  imagery.sizes = {
@@ -28,6 +42,43 @@ class WithS3Test < Test::Unit::TestCase
28
42
  assert_equal s % 'large', imagery.url(:large)
29
43
  end
30
44
 
45
+ test "customized s3_host urls" do
46
+ imagery = WithS3CustomHost.new(SuperSecretPhoto.new(1001))
47
+ imagery.root = '/tmp'
48
+ imagery.sizes = {
49
+ :thumb => ["56x56^"],
50
+ :small => ["100x100^", "100x100"],
51
+ :large => ["200x200>", "200x200"]
52
+ }
53
+
54
+ s = 'http://test.host/tmp-bucket-name/supersecretphoto/1001/%s.png'
55
+
56
+ assert_equal s % 'original', imagery.url
57
+ assert_equal s % 'original', imagery.url(:original)
58
+ assert_equal s % 'thumb', imagery.url(:thumb)
59
+ assert_equal s % 'small', imagery.url(:small)
60
+ assert_equal s % 'large', imagery.url(:large)
61
+ end
62
+
63
+ test "url when no bucket" do
64
+ imagery = NoBucket.new(SuperSecretPhoto.new(1001))
65
+ imagery.sizes = {
66
+ :thumb => ["56x56^"],
67
+ :small => ["100x100^", "100x100"],
68
+ :large => ["200x200>", "200x200"]
69
+ }
70
+
71
+ assert_raise Imagery::S3::UndefinedBucket do
72
+ imagery.url
73
+ end
74
+
75
+ begin
76
+ imagery.url
77
+ rescue Imagery::S3::UndefinedBucket => e
78
+ assert_equal Imagery::S3::Configs::BUCKET_ERROR_MSG, e.message
79
+ end
80
+ end
81
+
31
82
  test "urls with a distribution domain" do
32
83
  imagery = WithS3.new(SuperSecretPhoto.new(1001))
33
84
  imagery.root = '/tmp'
@@ -39,7 +90,7 @@ class WithS3Test < Test::Unit::TestCase
39
90
 
40
91
  s = 'http://assets.site.com/supersecretphoto/1001/%s.png'
41
92
 
42
- WithS3.s3_distribution_domain = 'http://assets.site.com'
93
+ WithS3.s3_distribution_domain 'http://assets.site.com'
43
94
 
44
95
  assert_equal s % 'original', imagery.url
45
96
  assert_equal s % 'original', imagery.url(:original)
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
  - Cyril David
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-31 00:00:00 +08:00
17
+ date: 2010-06-07 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -84,12 +84,14 @@ files:
84
84
  - imagery.gemspec
85
85
  - lib/imagery.rb
86
86
  - lib/imagery/faking.rb
87
+ - lib/imagery/missing.rb
87
88
  - lib/imagery/model.rb
88
89
  - lib/imagery/s3.rb
89
90
  - lib/imagery/test.rb
90
91
  - test/fixtures/lake.jpg
91
92
  - test/helper.rb
92
93
  - test/test_imagery.rb
94
+ - test/test_missing.rb
93
95
  - test/test_with_s3.rb
94
96
  has_rdoc: true
95
97
  homepage: http://github.com/sinefunc/imagery
@@ -124,4 +126,5 @@ summary: Image resizing without all the bloat
124
126
  test_files:
125
127
  - test/helper.rb
126
128
  - test/test_imagery.rb
129
+ - test/test_missing.rb
127
130
  - test/test_with_s3.rb