presto 0.0.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9124e378a7b3b0f59475c54788e8824253fcbf9e
4
- data.tar.gz: dda68438585b0fb4a1c55d2195b72725de6f2ba3
3
+ metadata.gz: c18ef6485c81c5391cedf8c73bd98cc28642879f
4
+ data.tar.gz: c2dd380877ac9bc145cdda90cae8f4675f7f8348
5
5
  SHA512:
6
- metadata.gz: 9f6f23a0791b28f5c8f0fe962d76abbd81d5ce8b5467c8c79002d3fc7f0871d8c15b46113867caca7b43561a0735c651992c8a17ab8bd647e150de4c0bd3d816
7
- data.tar.gz: e120f3060c2a3a97f2652d2e9d5097ffde3ac0b9dd7d071c9ccc7f3edb6673b437b042317c0691caef221d9eb492c60b9441e14a9b4301673655d00df42acda4
6
+ metadata.gz: 9ae8f94ff4999b672dd23dffbb8d8509bfcc349749a77f20b78157b05dd32969e7a3b5db605f2584dd8a27406d21956cf07f8629f06cb5e6272cdb4810c82400
7
+ data.tar.gz: df0be1a885b6edda4c47cab7c6980550477f1cf3a54fa3aa6a133be5890107479fa35e5b28bfc278c277ed2f723ad09cada844f829702beab24bbc7620e0de49
@@ -9,30 +9,32 @@ module Presto
9
9
  new(params).process(image)
10
10
  end
11
11
 
12
- attr_reader :params
12
+ attr_reader :transformation
13
13
 
14
14
  def initialize params={}
15
- @params = ::Presto::Transformation.new(params).tap{ |t| t.validate! }
15
+ @transformation = ::Presto::Transformation.new(params)
16
16
  end
17
17
 
18
18
  def process image
19
- process_fit(image) if params[:w] || params[:h]
20
- process_format(image) if params[:fm]
21
- process_quality(image) if params[:q]
19
+ return image if trans.empty?
20
+ trans.validate!
21
+ process_fit(image) if trans[:w] || trans[:h]
22
+ process_format(image) if trans[:fm]
23
+ process_quality(image) if trans[:q]
22
24
  image
23
25
  end
24
26
 
25
27
  private
26
28
 
27
29
  def process_fit image
28
- send("process_#{params[:fit]}_fit", image)
30
+ send("process_#{trans[:fit]}_fit", image)
29
31
  end
30
32
 
31
33
  def process_crop_fit image
32
34
  d = replaced_dimensions(image)
33
35
  image.combine_options do |cmd|
34
36
  cmd.resize "#{d.width}x#{d.height}^"
35
- cmd.background params[:bg]
37
+ cmd.background trans[:bg]
36
38
  cmd.gravity 'Center'
37
39
  cmd.extent "#{d.width}x#{d.height}"
38
40
  end
@@ -57,34 +59,38 @@ module Presto
57
59
  d = replaced_dimensions(image)
58
60
  image.combine_options do |cmd|
59
61
  cmd.resize "#{d.width}x#{d.height}"
60
- cmd.background params[:bg]
62
+ cmd.background trans[:bg]
61
63
  cmd.gravity 'Center'
62
64
  cmd.extent "#{d.width}x#{d.height}"
63
65
  end
64
66
  end
65
67
 
66
68
  def process_format image
67
- image.format params[:fm]
69
+ image.format trans[:fm]
68
70
  end
69
71
 
70
72
  def process_quality image
71
- image.quality params[:q]
73
+ image.quality trans[:q]
72
74
  end
73
75
 
74
76
  def resized_dimensions image
75
- ::Presto::Dimensions.resize(image[:width], image[:height], params[:w], params[:h])
77
+ ::Presto::Dimensions.resize(image[:width], image[:height], trans[:w], trans[:h])
76
78
  end
77
79
 
78
80
  def replaced_dimensions image
79
- w = params[:w] || image[:width]
80
- h = params[:h] || image[:height]
81
+ w = trans[:w] || image[:width]
82
+ h = trans[:h] || image[:height]
81
83
  ::Presto::Dimensions.new w, h
82
84
  end
85
+
86
+ def trans
87
+ transformation
88
+ end
83
89
  end
84
90
 
85
91
  module Extension
86
- def presto params={}
87
- ::Presto::MiniMagick::Processor.new(params).process(self)
92
+ def presto transformation={}
93
+ ::Presto::MiniMagick::Processor.process(self, transformation)
88
94
  end
89
95
  end
90
96
  end
@@ -1,6 +1,5 @@
1
1
  require 'presto'
2
- require 'presto/exceptions'
3
- require 'presto/errors'
2
+ require 'veto'
4
3
 
5
4
  module Presto
6
5
  class Transformation
@@ -8,126 +7,30 @@ module Presto
8
7
  HEXDECIMAL_ALPHA_REGEX = /^#[a-fA-F0-9]{8}$/
9
8
  HEXDECIMAL_LONG_REGEX = /^#[a-fA-F0-9]{6}$/
10
9
  HEXDECIMAL_SHORT_REGEX = /^#[a-fA-F0-9]{3}$/
11
-
12
10
  FIT_TYPES = %w(clip crop scale fill max).freeze
13
11
  QUALITY_MAX = 100
14
12
  QUALITY_MIN = 1
15
13
 
16
- attr_reader :record, :errors
17
-
18
- def initialize record
19
- @record = record
20
- end
14
+ include Veto.validator
21
15
 
22
- def valid?
23
- validate
24
- errors.empty?
16
+ def self.config
17
+ Presto.configuration
25
18
  end
26
19
 
27
- def validate!
28
- raise(::Presto::InvalidTransformation, errors.full_messages.join(", ")) unless valid?
29
- true
30
- end
20
+ validates :w, :greater_than_or_equal_to => config.min_width, :less_than_or_equal_to => config.max_width, :unless => "w.nil?"
21
+ validates :h, :greater_than_or_equal_to => config.min_height, :less_than_or_equal_to => config.max_height, :unless => "h.nil?"
22
+ validates :fit, :presence => true, :inclusion => FIT_TYPES
23
+ validate :validate_bg_format, :unless => "bg.nil?"
24
+ validates :fm, :inclusion => config.extension_white_list, :unless => "fm.nil?"
25
+ validates :q, :greater_than_or_equal_to => QUALITY_MIN, :less_than_or_equal_to => QUALITY_MAX, :unless => "q.nil?"
31
26
 
32
27
  private
33
28
 
34
- def validate
35
- @errors = Errors.new
36
- validate_width_max_size! unless record.w.nil?
37
- validate_width_min_size! unless record.w.nil?
38
- validate_height_max_size! unless record.h.nil?
39
- validate_height_min_size! unless record.h.nil?
40
- validate_fit_presence!
41
- validate_fit_inclusion!
42
- validate_bg_format! unless record.bg.nil?
43
- validate_format_inclusion! unless record.fm.nil?
44
- validate_quality_max_size! unless record.q.nil?
45
- validate_quality_min_size! unless record.q.nil?
46
- end
47
-
48
- def validate_width_max_size!
49
- if record.w > max_width
50
- errors.add(:w, "must be equal to or less than #{max_width}")
51
- end
52
- end
53
-
54
- def validate_width_min_size!
55
- if record.w < min_width
56
- errors.add(:w, "must be equal to or greater than #{min_width}")
57
- end
58
- end
59
-
60
- def validate_height_max_size!
61
- if record.h > max_height
62
- errors.add(:h, "must be equal to or less than #{max_height}")
63
- end
64
- end
65
-
66
- def validate_height_min_size!
67
- if record.h < min_height
68
- errors.add(:h, "must be equal to or greater than #{min_height}")
69
- end
70
- end
71
-
72
- def validate_fit_presence!
73
- if record.fit.nil? || record.fit.empty?
74
- errors.add(:fit, "can't be blank")
75
- end
76
- end
77
-
78
- def validate_fit_inclusion!
79
- unless FIT_TYPES.include?(record.fit)
80
- errors.add(:fit, "is not valid")
81
- end
82
- end
83
-
84
- def validate_bg_format!
85
- unless record.bg =~ HEXDECIMAL_LONG_REGEX || record.bg =~ HEXDECIMAL_SHORT_REGEX || record.bg =~ HEXDECIMAL_ALPHA_REGEX
29
+ def validate_bg_format
30
+ unless entity.bg =~ HEXDECIMAL_LONG_REGEX || entity.bg =~ HEXDECIMAL_SHORT_REGEX || entity.bg =~ HEXDECIMAL_ALPHA_REGEX
86
31
  errors.add(:bg, "is not valid")
87
32
  end
88
33
  end
89
-
90
- def validate_format_inclusion!
91
- unless extension_white_list.include?(record.fm)
92
- errors.add(:fm, "is not valid")
93
- end
94
- end
95
-
96
- def validate_quality_max_size!
97
- if record.q > QUALITY_MAX
98
- errors.add(:q, "must be equal to or less than #{QUALITY_MAX}")
99
- end
100
- end
101
-
102
- def validate_quality_min_size!
103
- if record.q < QUALITY_MIN
104
- errors.add(:q, "must be equal to or greater than #{QUALITY_MIN}")
105
- end
106
- end
107
-
108
- def min_width
109
- config.min_width
110
- end
111
-
112
- def max_width
113
- config.max_width
114
- end
115
-
116
- def min_height
117
- config.min_height
118
- end
119
-
120
- def max_height
121
- config.max_height
122
- end
123
-
124
- def extension_white_list
125
- config.extension_white_list
126
- end
127
-
128
- def config
129
- Presto.configuration
130
- end
131
34
  end
132
35
  end
133
36
  end
@@ -1,33 +1,53 @@
1
1
  require 'presto/transformation/validator'
2
+ require 'veto'
2
3
 
3
4
  module Presto
4
5
  class Transformation
5
- extend Forwardable
6
+ DEFAULT_BG = "#ffffff00"
7
+ DEFAULT_FIT = 'clip'
6
8
 
7
- def_delegators :validator, :validate!, :valid?
9
+ include Veto.model(Validator)
8
10
 
9
- attr_reader :w, :h, :fit, :fm, :bg, :q
10
-
11
- def initialize args={}
12
- defaults = {:fit => 'clip', :bg => 'ffffff00'}
13
- params = defaults.merge(args)
14
-
15
- @bg = "##{params[:bg]}" if params.key?(:bg)
16
- @fit = params[:fit]
17
- @fm = params[:fm]
18
- @h = params[:h].to_i if params.key?(:h)
19
- @w = params[:w].to_i if params.key?(:w)
20
- @q = params[:q].to_i if params.key?(:q)
11
+ def initialize params={}
12
+ @params = params
21
13
  end
22
14
 
23
15
  def [] val
24
16
  send(val)
25
17
  end
26
18
 
19
+ def bg
20
+ params.key?(:bg) ? "##{params[:bg]}" : DEFAULT_BG
21
+ end
22
+
23
+ def fit
24
+ params[:fit] || DEFAULT_FIT
25
+ end
26
+
27
+ def fm
28
+ params[:fm]
29
+ end
30
+
31
+ def h
32
+ params[:h].to_i if params.key?(:h)
33
+ end
34
+
35
+ def w
36
+ params[:w].to_i if params.key?(:w)
37
+ end
38
+
39
+ def q
40
+ params[:q].to_i if params.key?(:q)
41
+ end
42
+
43
+ def empty?
44
+ params.empty?
45
+ end
46
+
27
47
  private
28
48
 
29
- def validator
30
- ::Presto::Transformation::Validator.new(self)
49
+ def params
50
+ @params
31
51
  end
32
52
  end
33
53
  end
@@ -1,3 +1,3 @@
1
1
  module Presto
2
- VERSION = "0.0.1"
2
+ VERSION = "0.6.0"
3
3
  end
data/presto.gemspec CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "veto", "~> 0.1.2"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "guard-minitest", "~> 1.0.1"
24
25
  spec.add_development_dependency "mocha", "~> 0.14.0"
25
26
  spec.add_development_dependency "mini_magick", "~> 3.6.0"
26
27
  end
@@ -5,11 +5,11 @@ describe Presto::MiniMagick::Processor do
5
5
  let(:file) { ::File.open(File.expand_path('../../fixtures/images/test.gif', __FILE__)) }
6
6
  let(:image) { ::MiniMagick::Image.open(file.path) }
7
7
 
8
- describe "#initalize" do
9
- describe "when params are not valid" do
8
+ describe "#process" do
9
+ describe "when transformation is not valid" do
10
10
  it "raises error" do
11
- e = proc{ Presto::MiniMagick::Processor.new(:fm => 'xml') }.must_raise(::Presto::InvalidTransformation)
12
- e.message.must_equal 'fm is not valid'
11
+ e = proc{ Presto::MiniMagick::Processor.new(:fm => 'xml').process(image) }.must_raise(Veto::InvalidEntity)
12
+ e.message.must_include "fm is not in set: [\\\"jpg\\\", \\\"png\\\"]\""
13
13
  end
14
14
  end
15
15
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,6 @@
1
1
  require 'minitest/autorun'
2
- require 'mocha/setup'
2
+ require 'mocha/setup'
3
+
4
+ def context *args, &block
5
+ describe *args, &block
6
+ end
@@ -11,125 +11,74 @@ describe Presto::Transformation::Validator do
11
11
  let(:transformation) { stub(:w => w, :h => h, :fit => fit, :bg => bg, :fm => fm, :q => q) }
12
12
  let(:validator){ Presto::Transformation::Validator.new(transformation) }
13
13
 
14
- describe "#validate!" do
15
- describe "when valid" do
16
- it "returns true" do
17
- validator.validate!.must_equal true
18
- end
19
- end
20
-
21
- describe "when w is nil" do
22
- let(:w) { nil }
23
-
24
- it "does not raise error" do
25
- validator.validate!.must_equal true
26
- end
27
- end
28
-
29
- describe "when w is greater than max w" do
30
- let(:w) { Presto.configuration.max_width + 1 }
31
-
32
- it "raises error" do
33
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
34
- e.message.must_match "w must be equal to or less than"
35
- end
36
- end
37
-
38
- describe "when w is less than min w" do
39
- let(:w) { Presto.configuration.min_width - 1 }
40
-
41
- it "raises error" do
42
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
43
- e.message.must_match "w must be equal to or greater than"
44
- end
45
- end
46
-
47
- describe "when h is nil" do
48
- let(:h) { nil }
49
-
50
- it "does not raise error" do
51
- validator.validate!.must_equal true
52
- end
53
- end
54
-
55
- describe "when h is greater than max h" do
56
- let(:h) { Presto.configuration.max_height + 1 }
57
-
58
- it "raises error" do
59
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
60
- e.message.must_match "h must be equal to or less than"
61
- end
62
- end
63
-
64
- describe "when h is less than min h" do
65
- let(:h) { Presto.configuration.min_height - 1 }
66
-
67
- it "raises error" do
68
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
69
- e.message.must_match "h must be equal to or greater than"
70
- end
71
- end
72
-
73
- describe "when fit is nil" do
74
- let(:fit) { nil }
75
-
76
- it "raises error" do
77
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
78
- e.message.must_match "fit can't be blank"
79
- end
80
- end
81
-
82
- describe "when fit is invalid" do
83
- let(:fit) { 'blah' }
84
-
85
- it "raises error" do
86
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
87
- e.message.must_match "fit is not valid"
88
- end
89
- end
90
-
91
- describe "when bg is wrong formal" do
92
- let(:bg) { 'fhff' }
93
-
94
- it "raises error" do
95
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
96
- e.message.must_match "bg is not valid"
97
- end
98
- end
99
-
100
- describe "when fm is invalid" do
101
- let(:fm) { 'tif' }
102
-
103
- it "raises error" do
104
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
105
- e.message.must_match "fm is not valid"
106
- end
107
- end
108
-
109
- describe "when q is nil" do
110
- let(:q) { nil }
111
-
112
- it "does not raise error" do
113
- validator.validate!.must_equal true
114
- end
115
- end
116
-
117
- describe "when q is greater than max q" do
118
- let(:q) { ::Presto::Transformation::Validator::QUALITY_MAX + 1 }
119
-
120
- it "raises error" do
121
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
122
- e.message.must_match "q must be equal to or less than #{::Presto::Transformation::Validator::QUALITY_MAX}"
123
- end
124
- end
125
-
126
- describe "when q is less than min q" do
127
- let(:q) { ::Presto::Transformation::Validator::QUALITY_MIN - 1 }
128
-
129
- it "raises error" do
130
- e = proc { validator.validate! }.must_raise(Presto::InvalidTransformation)
131
- e.message.must_match "q must be equal to or greater than #{::Presto::Transformation::Validator::QUALITY_MIN}"
132
- end
133
- end
14
+ let(:errors) { validator.valid?; validator.errors }
15
+
16
+ context 'when valid' do
17
+ it { errors.must_be_empty }
18
+ end
19
+
20
+ context 'when w is nil' do
21
+ let(:w) { nil }
22
+ it { errors.must_be_empty }
23
+ end
24
+
25
+ context "when w is greater than max w" do
26
+ let(:w) { Presto.configuration.max_width + 1 }
27
+ it { errors[:w].must_include "must be less than or equal to 2000" }
28
+ end
29
+
30
+ context "when w is greater than max w" do
31
+ let(:w) { Presto.configuration.min_width - 1 }
32
+ it { errors[:w].must_include "must be greater than or equal to 1" }
33
+ end
34
+
35
+ context 'when h is nil' do
36
+ let(:h) { nil }
37
+ it { errors.must_be_empty }
38
+ end
39
+
40
+ context 'when h is greater than max h' do
41
+ let(:h) { Presto.configuration.max_width + 1 }
42
+ it { errors[:h].must_include "must be less than or equal to 2000" }
43
+ end
44
+
45
+ context 'when h is greater than max h' do
46
+ let(:h) { Presto.configuration.min_width - 1 }
47
+ it { errors[:h].must_include "must be greater than or equal to 1" }
48
+ end
49
+
50
+ context 'when fit is nil' do
51
+ let(:fit) { nil }
52
+ it { errors[:fit].must_include "is not present" }
53
+ end
54
+
55
+ context 'when fit is invalid' do
56
+ let(:fit) { 'blah' }
57
+ it { errors[:fit].must_include "is not in set: [\"clip\", \"crop\", \"scale\", \"fill\", \"max\"]" }
58
+ end
59
+
60
+ context 'when bg is wrong format' do
61
+ let(:bg) { 'fhff' }
62
+ it { errors[:bg].must_include "is not valid" }
63
+ end
64
+
65
+ context 'when fm is invalid' do
66
+ let(:fm) { 'tif' }
67
+ it { errors[:fm].must_include "is not in set: [\"jpg\", \"png\"]" }
68
+ end
69
+
70
+ context 'when q is nil' do
71
+ let(:q) { nil }
72
+ it { errors[:q].must_be_nil }
73
+ end
74
+
75
+ context "when q is greater than max q" do
76
+ let(:q) { ::Presto::Transformation::Validator::QUALITY_MAX + 1 }
77
+ it { errors[:q].must_include "must be less than or equal to 100" }
78
+ end
79
+
80
+ context "when q is less than min q" do
81
+ let(:q) { ::Presto::Transformation::Validator::QUALITY_MIN - 1 }
82
+ it { errors[:q].must_include "must be greater than or equal to 1" }
134
83
  end
135
84
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Lott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2013-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: veto
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :development
19
+ version: 0.1.2
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: 0.1.2
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: guard-minitest
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 1.0.1
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 1.0.1
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mocha
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -146,3 +146,4 @@ test_files:
146
146
  - spec/spec_helper.rb
147
147
  - spec/transformation/validator_spec.rb
148
148
  - spec/transformation_spec.rb
149
+ has_rdoc: