thumbor-versionable 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc7d72ea111430056c3d0da5c67699bada5a9ca9
4
+ data.tar.gz: e997545bd7b6bd32dea144c35ffeb4b970d82189
5
+ SHA512:
6
+ metadata.gz: bc253689155895a3540c6871ff04cb06f7f83eed48e2d2db4e6b3e3831024b32cd5590c1f9ecaa3f190efd47cc03d27d788fa162616c2f7eb0f1f3106bc51efa
7
+ data.tar.gz: 78a254349334b45a636e5ceb9e912bab6e633af274c73016546ce5ef0e6f7c366fca0cb03b4564d9ede1b8eaf6f4d9537ae2408125c440426f9dddebab87caf0
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.hound.yml ADDED
@@ -0,0 +1,3 @@
1
+ ruby:
2
+ enabled: true
3
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,96 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ - 'spec/fixtures/**/*'
5
+
6
+ Style/AlignHash:
7
+ EnforcedHashRocketStyle: separator
8
+ EnforcedColonStyle: separator
9
+
10
+ Style/CollectionMethods:
11
+ # Mapping from undesired method to desired_method
12
+ # e.g. to use `detect` over `find`:
13
+ #
14
+ # CollectionMethods:
15
+ # PreferredMethods:
16
+ # find: detect
17
+ PreferredMethods:
18
+ collect: 'map'
19
+ collect!: 'map!'
20
+ reduce: 'inject'
21
+ detect: 'find'
22
+ select: 'find_all'
23
+
24
+ Style/GuardClause:
25
+ Description: "Check for conditionals that can be replaced with guard clauses"
26
+ StyleGuide: "https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals"
27
+ Enabled: true
28
+
29
+ # I don't want to write documentation for every class
30
+ Style/Documentation:
31
+ Description: "Document classes and non-namespace modules."
32
+ Enabled: false
33
+
34
+ # I will use the %r syntax even if there's only one slash
35
+ Style/RegexpLiteral:
36
+ Description: >-
37
+ Use %r for regular expressions matching more than
38
+ `MaxSlashes` "/" characters.
39
+ StyleGuide: "https://github.com/bbatsov/ruby-style-guide#percent-r"
40
+ MaxSlashes: 0
41
+ Enabled: true
42
+
43
+ # I prefer raise to fail
44
+ Style/SignalException:
45
+ Description: "Checks for proper usage of fail and raise."
46
+ StyleGuide: "https://github.com/bbatsov/ruby-style-guide#fail-method"
47
+ Enabled: false
48
+
49
+ # Expect a final blank line
50
+ Style/TrailingBlankLines:
51
+ Description: "Checks trailing blank lines and final newline."
52
+ StyleGuide: "https://github.com/bbatsov/ruby-style-guide#newline-eof"
53
+ Enabled: true
54
+ EnforcedStyle: final_blank_line
55
+
56
+ Style/DoubleNegation:
57
+ Description: 'Checks for uses of double negation (!!).'
58
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-bang-bang'
59
+ Enabled: false
60
+
61
+ Style/ClassCheck:
62
+ Description: 'Enforces consistent use of `Object#is_a?` or `Object#kind_of?`.'
63
+ Enabled: false
64
+
65
+ Metrics/AbcSize:
66
+ Description: >-
67
+ A calculated magnitude based on number of assignments,
68
+ branches, and conditions.
69
+ Enabled: false
70
+
71
+ Metrics/LineLength:
72
+ Description: 'Limit lines to 100 characters.'
73
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
74
+ Enabled: true
75
+ Max: 100
76
+
77
+ Style/AlignHash:
78
+ Description: >-
79
+ Align the elements of a hash literal if they span more than
80
+ one line.
81
+ Enabled: false
82
+
83
+ Style/TrailingBlankLines:
84
+ Description: 'Checks trailing blank lines and final newline.'
85
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#newline-eof'
86
+ Enabled: false
87
+
88
+ Style/IndentHash:
89
+ Description: 'Checks the indentation of the first key in a hash literal.'
90
+ Enabled: false
91
+
92
+ Lint/UnusedMethodArgument:
93
+ Description: 'Checks for unused method arguments.'
94
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#underscore-unused-vars'
95
+ Enabled: false
96
+
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - ruby-head
5
+ - 2.1.0
6
+ addons:
7
+ code_climate:
8
+ repo_token: ce47d114985092505ca9f58d54401e457408a8bef35fbbef789011cade2fc9d9
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,363 @@
1
+ Mozilla Public License, version 2.0
2
+
3
+ 1. Definitions
4
+
5
+ 1.1. "Contributor"
6
+
7
+ means each individual or legal entity that creates, contributes to the
8
+ creation of, or owns Covered Software.
9
+
10
+ 1.2. "Contributor Version"
11
+
12
+ means the combination of the Contributions of others (if any) used by a
13
+ Contributor and that particular Contributor's Contribution.
14
+
15
+ 1.3. "Contribution"
16
+
17
+ means Covered Software of a particular Contributor.
18
+
19
+ 1.4. "Covered Software"
20
+
21
+ means Source Code Form to which the initial Contributor has attached the
22
+ notice in Exhibit A, the Executable Form of such Source Code Form, and
23
+ Modifications of such Source Code Form, in each case including portions
24
+ thereof.
25
+
26
+ 1.5. "Incompatible With Secondary Licenses"
27
+ means
28
+
29
+ a. that the initial Contributor has attached the notice described in
30
+ Exhibit B to the Covered Software; or
31
+
32
+ b. that the Covered Software was made available under the terms of
33
+ version 1.1 or earlier of the License, but not also under the terms of
34
+ a Secondary License.
35
+
36
+ 1.6. "Executable Form"
37
+
38
+ means any form of the work other than Source Code Form.
39
+
40
+ 1.7. "Larger Work"
41
+
42
+ means a work that combines Covered Software with other material, in a
43
+ separate file or files, that is not Covered Software.
44
+
45
+ 1.8. "License"
46
+
47
+ means this document.
48
+
49
+ 1.9. "Licensable"
50
+
51
+ means having the right to grant, to the maximum extent possible, whether
52
+ at the time of the initial grant or subsequently, any and all of the
53
+ rights conveyed by this License.
54
+
55
+ 1.10. "Modifications"
56
+
57
+ means any of the following:
58
+
59
+ a. any file in Source Code Form that results from an addition to,
60
+ deletion from, or modification of the contents of Covered Software; or
61
+
62
+ b. any new file in Source Code Form that contains any Covered Software.
63
+
64
+ 1.11. "Patent Claims" of a Contributor
65
+
66
+ means any patent claim(s), including without limitation, method,
67
+ process, and apparatus claims, in any patent Licensable by such
68
+ Contributor that would be infringed, but for the grant of the License,
69
+ by the making, using, selling, offering for sale, having made, import,
70
+ or transfer of either its Contributions or its Contributor Version.
71
+
72
+ 1.12. "Secondary License"
73
+
74
+ means either the GNU General Public License, Version 2.0, the GNU Lesser
75
+ General Public License, Version 2.1, the GNU Affero General Public
76
+ License, Version 3.0, or any later versions of those licenses.
77
+
78
+ 1.13. "Source Code Form"
79
+
80
+ means the form of the work preferred for making modifications.
81
+
82
+ 1.14. "You" (or "Your")
83
+
84
+ means an individual or a legal entity exercising rights under this
85
+ License. For legal entities, "You" includes any entity that controls, is
86
+ controlled by, or is under common control with You. For purposes of this
87
+ definition, "control" means (a) the power, direct or indirect, to cause
88
+ the direction or management of such entity, whether by contract or
89
+ otherwise, or (b) ownership of more than fifty percent (50%) of the
90
+ outstanding shares or beneficial ownership of such entity.
91
+
92
+
93
+ 2. License Grants and Conditions
94
+
95
+ 2.1. Grants
96
+
97
+ Each Contributor hereby grants You a world-wide, royalty-free,
98
+ non-exclusive license:
99
+
100
+ a. under intellectual property rights (other than patent or trademark)
101
+ Licensable by such Contributor to use, reproduce, make available,
102
+ modify, display, perform, distribute, and otherwise exploit its
103
+ Contributions, either on an unmodified basis, with Modifications, or
104
+ as part of a Larger Work; and
105
+
106
+ b. under Patent Claims of such Contributor to make, use, sell, offer for
107
+ sale, have made, import, and otherwise transfer either its
108
+ Contributions or its Contributor Version.
109
+
110
+ 2.2. Effective Date
111
+
112
+ The licenses granted in Section 2.1 with respect to any Contribution
113
+ become effective for each Contribution on the date the Contributor first
114
+ distributes such Contribution.
115
+
116
+ 2.3. Limitations on Grant Scope
117
+
118
+ The licenses granted in this Section 2 are the only rights granted under
119
+ this License. No additional rights or licenses will be implied from the
120
+ distribution or licensing of Covered Software under this License.
121
+ Notwithstanding Section 2.1(b) above, no patent license is granted by a
122
+ Contributor:
123
+
124
+ a. for any code that a Contributor has removed from Covered Software; or
125
+
126
+ b. for infringements caused by: (i) Your and any other third party's
127
+ modifications of Covered Software, or (ii) the combination of its
128
+ Contributions with other software (except as part of its Contributor
129
+ Version); or
130
+
131
+ c. under Patent Claims infringed by Covered Software in the absence of
132
+ its Contributions.
133
+
134
+ This License does not grant any rights in the trademarks, service marks,
135
+ or logos of any Contributor (except as may be necessary to comply with
136
+ the notice requirements in Section 3.4).
137
+
138
+ 2.4. Subsequent Licenses
139
+
140
+ No Contributor makes additional grants as a result of Your choice to
141
+ distribute the Covered Software under a subsequent version of this
142
+ License (see Section 10.2) or under the terms of a Secondary License (if
143
+ permitted under the terms of Section 3.3).
144
+
145
+ 2.5. Representation
146
+
147
+ Each Contributor represents that the Contributor believes its
148
+ Contributions are its original creation(s) or it has sufficient rights to
149
+ grant the rights to its Contributions conveyed by this License.
150
+
151
+ 2.6. Fair Use
152
+
153
+ This License is not intended to limit any rights You have under
154
+ applicable copyright doctrines of fair use, fair dealing, or other
155
+ equivalents.
156
+
157
+ 2.7. Conditions
158
+
159
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
160
+ Section 2.1.
161
+
162
+
163
+ 3. Responsibilities
164
+
165
+ 3.1. Distribution of Source Form
166
+
167
+ All distribution of Covered Software in Source Code Form, including any
168
+ Modifications that You create or to which You contribute, must be under
169
+ the terms of this License. You must inform recipients that the Source
170
+ Code Form of the Covered Software is governed by the terms of this
171
+ License, and how they can obtain a copy of this License. You may not
172
+ attempt to alter or restrict the recipients' rights in the Source Code
173
+ Form.
174
+
175
+ 3.2. Distribution of Executable Form
176
+
177
+ If You distribute Covered Software in Executable Form then:
178
+
179
+ a. such Covered Software must also be made available in Source Code Form,
180
+ as described in Section 3.1, and You must inform recipients of the
181
+ Executable Form how they can obtain a copy of such Source Code Form by
182
+ reasonable means in a timely manner, at a charge no more than the cost
183
+ of distribution to the recipient; and
184
+
185
+ b. You may distribute such Executable Form under the terms of this
186
+ License, or sublicense it under different terms, provided that the
187
+ license for the Executable Form does not attempt to limit or alter the
188
+ recipients' rights in the Source Code Form under this License.
189
+
190
+ 3.3. Distribution of a Larger Work
191
+
192
+ You may create and distribute a Larger Work under terms of Your choice,
193
+ provided that You also comply with the requirements of this License for
194
+ the Covered Software. If the Larger Work is a combination of Covered
195
+ Software with a work governed by one or more Secondary Licenses, and the
196
+ Covered Software is not Incompatible With Secondary Licenses, this
197
+ License permits You to additionally distribute such Covered Software
198
+ under the terms of such Secondary License(s), so that the recipient of
199
+ the Larger Work may, at their option, further distribute the Covered
200
+ Software under the terms of either this License or such Secondary
201
+ License(s).
202
+
203
+ 3.4. Notices
204
+
205
+ You may not remove or alter the substance of any license notices
206
+ (including copyright notices, patent notices, disclaimers of warranty, or
207
+ limitations of liability) contained within the Source Code Form of the
208
+ Covered Software, except that You may alter any license notices to the
209
+ extent required to remedy known factual inaccuracies.
210
+
211
+ 3.5. Application of Additional Terms
212
+
213
+ You may choose to offer, and to charge a fee for, warranty, support,
214
+ indemnity or liability obligations to one or more recipients of Covered
215
+ Software. However, You may do so only on Your own behalf, and not on
216
+ behalf of any Contributor. You must make it absolutely clear that any
217
+ such warranty, support, indemnity, or liability obligation is offered by
218
+ You alone, and You hereby agree to indemnify every Contributor for any
219
+ liability incurred by such Contributor as a result of warranty, support,
220
+ indemnity or liability terms You offer. You may include additional
221
+ disclaimers of warranty and limitations of liability specific to any
222
+ jurisdiction.
223
+
224
+ 4. Inability to Comply Due to Statute or Regulation
225
+
226
+ If it is impossible for You to comply with any of the terms of this License
227
+ with respect to some or all of the Covered Software due to statute,
228
+ judicial order, or regulation then You must: (a) comply with the terms of
229
+ this License to the maximum extent possible; and (b) describe the
230
+ limitations and the code they affect. Such description must be placed in a
231
+ text file included with all distributions of the Covered Software under
232
+ this License. Except to the extent prohibited by statute or regulation,
233
+ such description must be sufficiently detailed for a recipient of ordinary
234
+ skill to be able to understand it.
235
+
236
+ 5. Termination
237
+
238
+ 5.1. The rights granted under this License will terminate automatically if You
239
+ fail to comply with any of its terms. However, if You become compliant,
240
+ then the rights granted under this License from a particular Contributor
241
+ are reinstated (a) provisionally, unless and until such Contributor
242
+ explicitly and finally terminates Your grants, and (b) on an ongoing
243
+ basis, if such Contributor fails to notify You of the non-compliance by
244
+ some reasonable means prior to 60 days after You have come back into
245
+ compliance. Moreover, Your grants from a particular Contributor are
246
+ reinstated on an ongoing basis if such Contributor notifies You of the
247
+ non-compliance by some reasonable means, this is the first time You have
248
+ received notice of non-compliance with this License from such
249
+ Contributor, and You become compliant prior to 30 days after Your receipt
250
+ of the notice.
251
+
252
+ 5.2. If You initiate litigation against any entity by asserting a patent
253
+ infringement claim (excluding declaratory judgment actions,
254
+ counter-claims, and cross-claims) alleging that a Contributor Version
255
+ directly or indirectly infringes any patent, then the rights granted to
256
+ You by any and all Contributors for the Covered Software under Section
257
+ 2.1 of this License shall terminate.
258
+
259
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
260
+ license agreements (excluding distributors and resellers) which have been
261
+ validly granted by You or Your distributors under this License prior to
262
+ termination shall survive termination.
263
+
264
+ 6. Disclaimer of Warranty
265
+
266
+ Covered Software is provided under this License on an "as is" basis,
267
+ without warranty of any kind, either expressed, implied, or statutory,
268
+ including, without limitation, warranties that the Covered Software is free
269
+ of defects, merchantable, fit for a particular purpose or non-infringing.
270
+ The entire risk as to the quality and performance of the Covered Software
271
+ is with You. Should any Covered Software prove defective in any respect,
272
+ You (not any Contributor) assume the cost of any necessary servicing,
273
+ repair, or correction. This disclaimer of warranty constitutes an essential
274
+ part of this License. No use of any Covered Software is authorized under
275
+ this License except under this disclaimer.
276
+
277
+ 7. Limitation of Liability
278
+
279
+ Under no circumstances and under no legal theory, whether tort (including
280
+ negligence), contract, or otherwise, shall any Contributor, or anyone who
281
+ distributes Covered Software as permitted above, be liable to You for any
282
+ direct, indirect, special, incidental, or consequential damages of any
283
+ character including, without limitation, damages for lost profits, loss of
284
+ goodwill, work stoppage, computer failure or malfunction, or any and all
285
+ other commercial damages or losses, even if such party shall have been
286
+ informed of the possibility of such damages. This limitation of liability
287
+ shall not apply to liability for death or personal injury resulting from
288
+ such party's negligence to the extent applicable law prohibits such
289
+ limitation. Some jurisdictions do not allow the exclusion or limitation of
290
+ incidental or consequential damages, so this exclusion and limitation may
291
+ not apply to You.
292
+
293
+ 8. Litigation
294
+
295
+ Any litigation relating to this License may be brought only in the courts
296
+ of a jurisdiction where the defendant maintains its principal place of
297
+ business and such litigation shall be governed by laws of that
298
+ jurisdiction, without reference to its conflict-of-law provisions. Nothing
299
+ in this Section shall prevent a party's ability to bring cross-claims or
300
+ counter-claims.
301
+
302
+ 9. Miscellaneous
303
+
304
+ This License represents the complete agreement concerning the subject
305
+ matter hereof. If any provision of this License is held to be
306
+ unenforceable, such provision shall be reformed only to the extent
307
+ necessary to make it enforceable. Any law or regulation which provides that
308
+ the language of a contract shall be construed against the drafter shall not
309
+ be used to construe this License against a Contributor.
310
+
311
+
312
+ 10. Versions of the License
313
+
314
+ 10.1. New Versions
315
+
316
+ Mozilla Foundation is the license steward. Except as provided in Section
317
+ 10.3, no one other than the license steward has the right to modify or
318
+ publish new versions of this License. Each version will be given a
319
+ distinguishing version number.
320
+
321
+ 10.2. Effect of New Versions
322
+
323
+ You may distribute the Covered Software under the terms of the version
324
+ of the License under which You originally received the Covered Software,
325
+ or under the terms of any subsequent version published by the license
326
+ steward.
327
+
328
+ 10.3. Modified Versions
329
+
330
+ If you create software not governed by this License, and you want to
331
+ create a new license for such software, you may create and use a
332
+ modified version of this License if you rename the license and remove
333
+ any references to the name of the license steward (except to note that
334
+ such modified license differs from this License).
335
+
336
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary
337
+ Licenses If You choose to distribute Source Code Form that is
338
+ Incompatible With Secondary Licenses under the terms of this version of
339
+ the License, the notice described in Exhibit B of this License must be
340
+ attached.
341
+
342
+ Exhibit A - Source Code Form License Notice
343
+
344
+ This Source Code Form is subject to the
345
+ terms of the Mozilla Public License, v.
346
+ 2.0. If a copy of the MPL was not
347
+ distributed with this file, You can
348
+ obtain one at
349
+ http://mozilla.org/MPL/2.0/.
350
+
351
+ If it is not possible or desirable to put the notice in a particular file,
352
+ then You may include the notice in a location (such as a LICENSE file in a
353
+ relevant directory) where a recipient would be likely to look for such a
354
+ notice.
355
+
356
+ You may add additional accurate notices of copyright ownership.
357
+
358
+ Exhibit B - "Incompatible With Secondary Licenses" Notice
359
+
360
+ This Source Code Form is "Incompatible
361
+ With Secondary Licenses", as defined by
362
+ the Mozilla Public License, v. 2.0.
363
+
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ thumbor-versionable
2
+ ===================
3
+
4
+ [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/vjustov/thumbor-versionable?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5
+ [![Code Climate](https://codeclimate.com/github/vjustov/thumbor-versionable/badges/gpa.svg)](https://codeclimate.com/github/vjustov/thumbor-versionable)
6
+ [![Test Coverage](https://codeclimate.com/github/vjustov/thumbor-versionable/badges/coverage.svg)](https://codeclimate.com/github/vjustov/thumbor-versionable)
7
+ [![Build Status](https://travis-ci.org/vjustov/thumbor-versionable.svg?branch=master)](https://travis-ci.org/vjustov/thumbor-versionable)
8
+
9
+ A Thumbor client that lets you specify versions of given image and generates the url for them.
10
+
11
+ Often you'll want to add different versions of an image. The classic example is image thumbnails but doing that on the server is time/resource consuming, that's when [thumbor](https://github.com/thumbor/thumbor) comes in.
12
+
13
+ This gem provides a simple and extremely flexible way generate thumbor URLs for each image version in Ruby applications. It works well with Rack based web applications, such as Ruby on Rails and is a drop drop-in replacement for [carrierwave](https://github.com/carrierwaveuploader/carrierwave) when the image processing times have gone too high.
14
+
15
+
16
+ ## Installation
17
+
18
+ thumbor-versionable can be installed to use with any Ruby web framework. The first step is to install the gem:
19
+
20
+ $ gem install thumbor-versionable
21
+
22
+ Or include it in your project's Gemfile with Bundler:
23
+
24
+ ```ruby
25
+ gem 'thumbor-versionable', github: 'vjustov/thumbor-versionable'
26
+ ```
27
+
28
+ If you are using rails, that's it! if you are using anything else you will have to require the gem whenever you want to use it.
29
+
30
+ ```ruby
31
+ require 'versionable'
32
+ ```
33
+
34
+ Just in case, restart the server to apply the changes.
35
+
36
+ ## Usage
37
+
38
+ You can use thumbor-versionable in one of two ways. if you just want to generate a url of an image to use with thumbor.
39
+
40
+ ```ruby
41
+ require 'versionable'
42
+ require 'versionable/version'
43
+
44
+ Versionable.configure do
45
+ thumbor_server 'thumbor_server.example.com'
46
+ secret_key 'S3CR37_K3Y' # This is only needed if you want to sign your requests.
47
+ end
48
+
49
+ thumbor_url = Version.new(width: 45, height: 200).url
50
+ ```
51
+
52
+ But if you need many thumbnails or some kind of image manipulation on one of your models you can do it this way:
53
+
54
+ ```ruby
55
+ class Product
56
+ include Versionable
57
+
58
+ versionable :image, :external_fake_image do
59
+ version :form_thumbnail, width: 100, height: 150 do
60
+ filter :quality, 50
61
+ end
62
+ version :notification_thumbnail, width: 50, height: 0
63
+ end
64
+ end
65
+ ```
66
+
67
+ Then you can access a version by doing:
68
+
69
+ ```ruby
70
+ @product.image.form_thumbnail.url
71
+ ```
72
+
73
+ ### Calculating Metadata
74
+
75
+ You can generate a metadata url passing the :meta key with a truthy value to Version, which you could then request using Net::HTTP.
76
+
77
+ ```ruby
78
+ thumbor_url = Version.new(width: 145, height: 340, meta: true).url
79
+ ```
80
+
81
+ But when you have several versions of one image, one request each to get the metadata of them all, it's not a good idea. We formulated a way to calculate the metadata of the different versions based on the metadata of the parent:
82
+
83
+ ```ruby
84
+ metadata = @product.image.fetch_metadata
85
+ @product.image.height_from_metadata metadata
86
+ @product.image.width_from_metadata metadata
87
+
88
+ @product.image.notification_thumbnail.height
89
+ => 0
90
+ @product.image.notification_thumbnail.calculate_metadata
91
+ @product.image.notification_thumbnail.height
92
+ => 75
93
+ ```
94
+
95
+ This will only work when the main image metadata has been previously set.
96
+
97
+ ## Thumbor Options
98
+
99
+ Available arguments to a version are:
100
+
101
+ meta: bool
102
+ width: int
103
+ height: int
104
+ smart: bool
105
+ fit_in: bool
106
+
107
+ Filters can be specified in a DSL way and all are supported. You can also include the same filter more than once and they will appear in that order.
108
+
109
+ ```ruby
110
+ version :form_thumbnail, width: 100, height: 150 do
111
+ filter :colorize, [25,10,53,'79A8B2']
112
+ filter :quality, 50
113
+ filter :grayscale
114
+ filter :colorize, [35,40,50,'79A8B2']
115
+ end
116
+ ```
117
+
118
+ If you need more info on what each option does, check [thumbor's documentation](github.com/thumbor/thumbor/wiki).
119
+
120
+ ## Contributing
121
+
122
+ - [Fork it](https://github.com/vjustov/thumbor-versionable)
123
+ - Create your feature branch (git checkout -b my-new-feature)
124
+ - Commit your changes (git commit -am "Add some feature")
125
+ - Run `rake` to run all of the specs and make sure they are all green.
126
+ - Run `rubocop` and keep the cop happy.
127
+ - Push to the branch (git push origin my-new-feature)
128
+ - Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task default: :spec
@@ -0,0 +1,24 @@
1
+ module Versionable
2
+ class Configuration
3
+ def initialize(&block)
4
+ instance_eval(&block) if block_given?
5
+ end
6
+
7
+ def thumbor_server(value = nil)
8
+ if value
9
+ @server = value
10
+ else
11
+ @server ||= 'thumbor_server.example.com'
12
+ end
13
+ end
14
+
15
+ def secret_key(value = nil)
16
+ if value
17
+ @secret_key = value
18
+ else
19
+ @secret_key ||= 'S3CR37_K3Y'
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,83 @@
1
+ require 'json'
2
+ module Versionable
3
+ class Image
4
+ class InvalidMetadata < StandardError; end
5
+
6
+ attr_reader :width, :height
7
+ def initialize(model, column, accessor, &blk)
8
+ @model = model
9
+ @column = column
10
+ @accessor = accessor
11
+ @versions = {}
12
+
13
+ instance_eval(&blk) if block_given?
14
+ end
15
+
16
+ def url
17
+ blank?(model.send(column)) ? legacy_url(accessor) : model.send(column)
18
+ end
19
+
20
+ def respond_to?(method, include_private = false)
21
+ super || @versions.key?(method)
22
+ end
23
+
24
+ def to_json(_options = nil)
25
+ JSON.generate(as_json)
26
+ end
27
+
28
+ def as_json(_options = nil)
29
+ serializable_hash
30
+ end
31
+
32
+ def fetch_metadata
33
+ metadata_url = URI.parse(Versionable::Version.new(self, meta: true).url)
34
+ json_data = Net::HTTP.get(metadata_url)
35
+ blank?(json_data) ? nil : JSON.parse(Net::HTTP.get(metadata_url))
36
+ end
37
+
38
+ def height_from_metadata(hash)
39
+ @height = hash['thumbor']['source']['height']
40
+ rescue
41
+ raise InvalidMetadata,
42
+ 'Argument is not valid thumbor metadata. " +
43
+ "Use #fetch_metadata to get it.'
44
+ end
45
+
46
+ def width_from_metadata(hash)
47
+ @width = hash['thumbor']['source']['width']
48
+ rescue
49
+ raise InvalidMetadata,
50
+ 'Argument is not valid thumbor metadata. " +
51
+ "Use #fetch_metadata to get it.'
52
+ end
53
+
54
+ private
55
+
56
+ attr_reader :model, :column, :accessor, :versions
57
+
58
+ def method_missing(name, *args, &blk)
59
+ if versions.respond_to?(:key?) && versions.key?(name)
60
+ versions[name]
61
+ else
62
+ super
63
+ end
64
+ end
65
+
66
+ def version(name, options, &blk)
67
+ @versions[name] = Versionable::Version.new(self, options, &blk)
68
+ end
69
+
70
+ def serializable_hash(_options = nil)
71
+ # TODO: Add serializable_attributes to Version,
72
+ # so that we dont hardcode url as the only attribute that gets serialized.
73
+ { 'url' => url }.merge Hash[@versions.map do |name, version|
74
+ [name, { 'url' => version.url }]
75
+ end]
76
+ end
77
+
78
+ def blank?(obj)
79
+ obj.respond_to?(:empty?) ? !!obj.empty? : !obj
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,152 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'uri'
4
+
5
+ module Versionable
6
+ class Version
7
+ FIT_IN = 'fit-in'.freeze
8
+ SMART = 'smart'.freeze
9
+ UNSAFE = 'unsafe'.freeze
10
+ META = 'meta'.freeze
11
+
12
+ attr_reader :width, :height
13
+ def initialize(image, parameters, &blk)
14
+ @image = image
15
+ @height = parameters.fetch(:height) { 0 }
16
+ @width = parameters.fetch(:width) { 0 }
17
+ @fit_in = parameters.fetch(:fit_in) { false }
18
+ @smart = parameters.fetch(:smart) { false }
19
+ @meta = parameters.fetch(:meta) { false }
20
+ @filters = []
21
+
22
+ instance_eval(&blk) if block_given?
23
+ end
24
+
25
+ def url
26
+ [
27
+ server,
28
+ sign,
29
+ options_url
30
+ ].reject(&:nil?).join('/')
31
+ end
32
+
33
+ def calculate_metadata
34
+ {
35
+ width: @width = width != 0 ? width : (image.width * height) / \
36
+ image.height,
37
+ height: @height = height != 0 ? height : (image.height * width) / \
38
+ image.width
39
+ }
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :image, :fit_in, :smart, :filters
45
+
46
+ def options_url
47
+ [
48
+ meta,
49
+ crop,
50
+ fit_in,
51
+ measurements,
52
+ smart,
53
+ filters,
54
+ decoded_url
55
+ ].reject(&:nil?).join('/')
56
+ end
57
+
58
+ def image_url
59
+ image.url
60
+ end
61
+
62
+ def decoded_url
63
+ URI.decode(image_url).gsub(/[+ ]/, '%20')
64
+ # We need gsub to change '+' to ' ' when the url is decoded,
65
+ # but it doesn't, so we karate-chop 'em into place.
66
+ end
67
+
68
+ def filter(name, values = nil)
69
+ @filters << { name => values }
70
+ end
71
+
72
+ # Serves as getter and setter.
73
+ def crop(hash = {})
74
+ if hash.empty?
75
+ @crop
76
+ else
77
+ @crop = Crop.from(hash[:from]).to(hash[:to]).create!
78
+ end
79
+ end
80
+
81
+ def measurements
82
+ "#{width}x#{height}"
83
+ end
84
+
85
+ def server
86
+ Versionable.config.thumbor_server
87
+ end
88
+
89
+ def sign
90
+ key = Versionable.config.secret_key
91
+ Base64.urlsafe_encode64(OpenSSL::HMAC.digest('sha1', key, options_url))
92
+ end
93
+
94
+ def meta
95
+ @meta ? META : nil
96
+ end
97
+
98
+ def fit_in
99
+ @fit_in ? FIT_IN : nil
100
+ end
101
+
102
+ def smart
103
+ @smart ? SMART : nil
104
+ end
105
+
106
+ def filters
107
+ @filters.inject('filters') do |result, filter|
108
+ result + ":#{filter_name(filter)}(#{args_from filter_params(filter)})"
109
+ end unless @filters.empty?
110
+ end
111
+
112
+ def filter_name(filter)
113
+ filter.keys.first
114
+ end
115
+
116
+ def filter_params(filter)
117
+ filter.values.first
118
+ end
119
+
120
+ def args_from(filter_value)
121
+ filter_value.kind_of?(Array) ? filter_value.join(',') : filter_value
122
+ end
123
+
124
+ class Crop
125
+ def self.from(hash)
126
+ @x1 = hash[:x]
127
+ @y1 = hash[:y]
128
+ self
129
+ end
130
+
131
+ def self.to(hash)
132
+ @x2 = hash[:x]
133
+ @y2 = hash[:y]
134
+ self
135
+ end
136
+
137
+ def self.create!
138
+ new(@x1, @y1, @x2, @y2)
139
+ end
140
+
141
+ def initialize(x1, y1, x2, y2)
142
+ @x1, @y1, @x2, @y2 = x1, y1, x2, y2
143
+ end
144
+
145
+ def to_str
146
+ "#{@x1}x#{@y1}:#{@x2}x#{@y2}"
147
+ end
148
+ alias_method :to_s, :to_str
149
+ end
150
+ end
151
+ end
152
+
@@ -0,0 +1,29 @@
1
+ module Versionable
2
+ class << self
3
+ attr_reader :config
4
+
5
+ def version
6
+ '0.2.3'
7
+ end
8
+
9
+ def configure(&blk)
10
+ @config ||= Configuration.new(&blk)
11
+ end
12
+
13
+ def included(base)
14
+ base.extend(ClassMethods)
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ attr_reader :versions, :image
20
+
21
+ def versionable(accessor, column, &blk)
22
+ instance_eval <<-EOF
23
+ define_method(accessor) do
24
+ @#{accessor} ||= Versionable::Image.new(self, :#{column}, :#{accessor}, &blk)
25
+ end
26
+ EOF
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Versionable::Configuration do
4
+ let(:config) do
5
+ described_class.new do
6
+ thumbor_server('server_test.example.com')
7
+ secret_key('S3CR37_K3Y!!')
8
+ end
9
+ end
10
+
11
+ it 'correctly set the server' do
12
+ expect(config.thumbor_server).to eq('server_test.example.com')
13
+ end
14
+ it 'correctly set the secret_key' do
15
+ expect(config.secret_key).to eq('S3CR37_K3Y!!')
16
+ end
17
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Versionable::Image do
4
+ let(:image) do
5
+ described_class.new OpenStruct.new(
6
+ url: 'https://s3.amazonaws.com/ksr/assets/moo.png'
7
+ ), :url, :accessor do
8
+ version :form_thumbnail, width: 100, height: 150 do
9
+ filter :quality, 50
10
+ end
11
+ version :form_thumbnail_2, width: 100, height: 150
12
+ end
13
+ end
14
+
15
+ it 'should responds to :url' do
16
+ expect(image).to respond_to(:url)
17
+ expect(image.url).to eq('https://s3.amazonaws.com/ksr/assets/moo.png')
18
+ end
19
+
20
+ it 'should responds to version' do
21
+ expect(image).to respond_to(:form_thumbnail)
22
+ expect(image).to respond_to(:form_thumbnail_2)
23
+ end
24
+
25
+ it 'should be rendered as a json' do
26
+ expect(image).to respond_to(:to_json)
27
+ expect(image.to_json).to eq(
28
+ '{"url":"https://s3.amazonaws.com/ksr/assets/moo.png","form_thumbnail":{"url":"ht' \
29
+ 'tp://thumbor_server.net/JCvCnwX3k3NBcHOF_HICTy9dEk8=/100x150/filters:quality(50)' \
30
+ '/https://s3.amazonaws.com/ksr/assets/moo.png"},"form_thumbnail_2":{"url":"http:/' \
31
+ '/thumbor_server.net/pzNWZlQuWGNjwy2Ix-83Nc4gswY=/100x150/https://s3.amazonaws.co' \
32
+ 'm/ksr/assets/moo.png"}}')
33
+ end
34
+
35
+ context 'when the image is valid' do
36
+ let(:metadata) do
37
+ {
38
+ 'thumbor' => {
39
+ 'operations' => [],
40
+ 'source' => {
41
+ 'url' => 'http://google.com/favicon.ico',
42
+ 'width' => 32,
43
+ 'height' => 32
44
+ },
45
+ 'focal_points' => [{
46
+ 'origin' => 'alignment',
47
+ 'height' => 1,
48
+ 'width' => 1,
49
+ 'y' => 16.0,
50
+ 'x' => 16.0,
51
+ 'z' => 1.0
52
+ }],
53
+ 'target' => { 'width' => 32, 'height' => 32 }
54
+ }
55
+ }
56
+ end
57
+
58
+ it 'should fetch the image metadata' do
59
+ expect(image).to respond_to(:fetch_metadata)
60
+ end
61
+
62
+ it 'should be able to set height from the metadata' do
63
+ expect(image.height_from_metadata metadata).to eq(32)
64
+ end
65
+
66
+ it 'should be able to set the width from the metadata' do
67
+ expect(image.width_from_metadata metadata).to eq(32)
68
+ end
69
+ end
70
+
71
+ context 'when the image is invalid' do
72
+ let(:metadata) { '' }
73
+
74
+ it 'setting the height from the metadata should throw an exception' do
75
+ expect { image.height_from_metadata metadata }.to \
76
+ raise_exception
77
+ end
78
+ it 'setting the width from the metadata should throw an exception' do
79
+ expect { image.width_from_metadata metadata }.to \
80
+ raise_exception
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,15 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ # Dir[File.'../lib/**/*.rb'].each { |file| require file }
5
+ require 'versionable'
6
+ require 'versionable/image'
7
+ require 'versionable/version'
8
+ require 'versionable/configuration'
9
+
10
+ require 'ostruct'
11
+
12
+ RSpec.configure do |config|
13
+ config.color = true
14
+ config.formatter = :documentation # :progress, :html, :textmate
15
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Versionable::Version do
4
+ let(:version) do
5
+ described_class.new OpenStruct.new(
6
+ url: 'https://s3.amazonaws.com/ksr/assets/moo.png'
7
+ ), width: 100, height: 150
8
+ end
9
+
10
+ it 'should responds to :url' do
11
+ expect(version).to respond_to(:url)
12
+ end
13
+
14
+ it 'should correctly constructs the url' do
15
+ expect(version.url).to eq(
16
+ 'http://thumbor_server.net/pzNWZlQuWGNjwy2Ix-83Nc4gswY=/100' \
17
+ 'x150/https://s3.amazonaws.com/ksr/assets/moo.png'
18
+ )
19
+ end
20
+
21
+ context 'when the image is valid' do
22
+ it 'correctly calculate it metadata' do
23
+ expect(version.calculate_metadata).to eq(
24
+ width: 100, height: 150
25
+ )
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Versionable do
4
+
5
+ Versionable.configure do
6
+ thumbor_server 'http://thumbor_server.net'
7
+ end
8
+
9
+ class FakeClass
10
+ include Versionable
11
+
12
+ versionable :image, :external_fake_image do
13
+ version :form_thumbnail, width: 100, height: 150 do
14
+ filter :quality, 50
15
+ end
16
+ version :form_thumbnail_2, width: 100, height: 150
17
+ end
18
+ end
19
+
20
+ let(:fake_product) do
21
+ fake_product = FakeClass.new
22
+ allow(fake_product).to receive(:external_fake_image) do
23
+ 'http://google.com/favicon.ico'
24
+ end
25
+ fake_product
26
+ end
27
+
28
+ it 'adds :versionable to the class' do
29
+ expect(FakeClass).to respond_to(:versionable)
30
+ end
31
+
32
+ it 'should responds to the method specified' do
33
+ expect(fake_product).to respond_to(:image)
34
+ end
35
+ end
36
+
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'versionable'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'thumbor-versionable'
8
+ spec.version = Versionable.version
9
+ spec.authors = ['Viktor Justo']
10
+ spec.email = ['viktor@vjustov.me']
11
+ spec.description = 'A Thumbor client to specify versions of given image.'
12
+ spec.summary = 'A Thumbor client to specify versions of given image.'
13
+ spec.homepage = ''
14
+ spec.license = 'Mozilla Public License, version 2.0'
15
+
16
+ spec.files = `git ls-files`.split($RS)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+
21
+ spec.add_development_dependency 'rspec', '~> 3.1'
22
+ spec.add_development_dependency 'rake', '~> 10.4'
23
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thumbor-versionable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Justo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codeclimate-test-reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ description: A Thumbor client to specify versions of given image.
56
+ email:
57
+ - viktor@vjustov.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".hound.yml"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - lib/versionable.rb
71
+ - lib/versionable/configuration.rb
72
+ - lib/versionable/image.rb
73
+ - lib/versionable/version.rb
74
+ - spec/configuration_spec.rb
75
+ - spec/image_spec.rb
76
+ - spec/spec_helper.rb
77
+ - spec/version_spec.rb
78
+ - spec/versionable_spec.rb
79
+ - thumbor-versionable.gemspec
80
+ homepage: ''
81
+ licenses:
82
+ - Mozilla Public License, version 2.0
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.2.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A Thumbor client to specify versions of given image.
104
+ test_files:
105
+ - spec/configuration_spec.rb
106
+ - spec/image_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/version_spec.rb
109
+ - spec/versionable_spec.rb