Skip to content

Commit 3f9d1d5

Browse files
committed
Initial tests. Size option fix. Better rails dependency support
1 parent afe2470 commit 3f9d1d5

File tree

7 files changed

+196
-9
lines changed

7 files changed

+196
-9
lines changed

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

cloudinary.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
1919
s.require_paths = ["lib"]
2020

2121
s.add_dependency "rest-client"
22+
s.add_development_dependency "rspec"
2223
end

lib/cloudinary.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require "cloudinary/uploader"
66
require "cloudinary/downloader"
77
require "cloudinary/blob"
8-
require "cloudinary/static"
8+
require "cloudinary/static" if defined?(::ActiveSupport)
99
require 'active_support'
1010
require "cloudinary/missing"
1111
require "cloudinary/carrier_wave" if defined?(::CarrierWave)

lib/cloudinary/static.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Cloudinary::Static
99
def self.discover
1010
ignore_files = Cloudinary.config.ignore_files || IGNORE_FILES
1111
relative_dirs = Cloudinary.config.statis_image_dirs || STATIC_IMAGE_DIRS
12-
dirs = relative_dirs.map{|dir| Rails.root.join(dir)}.select(&:exist?)
12+
dirs = relative_dirs.map{|dir| self.root.join(dir)}.select(&:exist?)
1313
dirs.each do
1414
|dir|
1515
dir.find do
@@ -23,7 +23,7 @@ def self.discover
2323
elsif SUPPORTED_IMAGES.none?{|pattern| pattern.is_a?(String) ? pattern == file : file.match(pattern)}
2424
next
2525
else
26-
relative_path = path.relative_path_from(Rails.root)
26+
relative_path = path.relative_path_from(self.root)
2727
public_path = path.relative_path_from(dir.dirname)
2828
yield(relative_path, public_path)
2929
end
@@ -33,12 +33,16 @@ def self.discover
3333

3434
UTC = ActiveSupport::TimeZone["UTC"]
3535

36+
def self.root
37+
defined?(Rails) ? Rails.root : Pathname.new(".")
38+
end
39+
3640
def self.metadata_file_path
37-
Rails.root.join(METADATA_FILE)
41+
self.root.join(METADATA_FILE)
3842
end
3943

4044
def self.metadata_trash_file_path
41-
Rails.root.join(METADATA_TRASH_FILE)
45+
self.root.join(METADATA_TRASH_FILE)
4246
end
4347

4448
def self.metadata(metadata_file = metadata_file_path, hash=true)
@@ -73,7 +77,7 @@ def self.sync(options={})
7377
|path, public_path|
7478
next if found_paths.include?(path)
7579
found_paths << path
76-
data = Rails.root.join(path).read(:mode=>"rb")
80+
data = self.root.join(path).read(:mode=>"rb")
7781
ext = path.extname
7882
format = ext[1..-1]
7983
md5 = Digest::MD5.hexdigest(data)

lib/cloudinary/utils.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class Cloudinary::Utils
77

88
# Warning: options are being destructively updated!
99
def self.generate_transformation_string(options={})
10+
size = options.delete(:size)
11+
options[:width], options[:height] = size.split("x") if size
1012
width = options[:width]
1113
height = options[:height]
12-
size = options.delete(:size)
13-
width, height = size.split("x") if size
1414
options.delete(:width) if width && width.to_f < 1
1515
options.delete(:height) if height && height.to_f < 1
1616

@@ -71,7 +71,7 @@ def self.cloudinary_url(source, options = {})
7171
return source
7272
end
7373
end
74-
@metadata ||= Cloudinary::Static.metadata
74+
@metadata ||= defined?(Cloudinary::Static) ? Cloudinary::Static.metadata : {}
7575
if @metadata["images/#{source}"]
7676
return source if !Cloudinary.config.static_image_support
7777
type = :asset

spec/spec_helper.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
4+
# loaded once.
5+
#
6+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7+
RSpec.configure do |config|
8+
config.treat_symbols_as_metadata_keys_with_true_values = true
9+
config.run_all_when_everything_filtered = true
10+
config.filter_run :focus
11+
end

spec/utils_spec.rb

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
require 'spec_helper'
2+
require 'cloudinary'
3+
4+
describe Cloudinary::Utils do
5+
before(:each) do
6+
Cloudinary.config do
7+
|config|
8+
config.cloud_name = "test123"
9+
config.secure_distribution = nil
10+
config.private_cdn = false
11+
end
12+
end
13+
14+
it "should use cloud_name from config" do
15+
result = Cloudinary::Utils.cloudinary_url("test")
16+
result.should == "http://res.cloudinary.com/test123/image/upload/test"
17+
end
18+
19+
it "should allow overriding cloud_name in options" do
20+
options = {:cloud_name=>"test321"}
21+
result = Cloudinary::Utils.cloudinary_url("test", options)
22+
options.should == {}
23+
result.should == "http://res.cloudinary.com/test321/image/upload/test"
24+
end
25+
26+
it "should use default secure distribution if secure=true" do
27+
options = {:secure=>true}
28+
result = Cloudinary::Utils.cloudinary_url("test", options)
29+
options.should == {}
30+
result.should == "https://d3jpl91pxevbkh.cloudfront.net/test123/image/upload/test"
31+
end
32+
33+
it "should allow overriding secure distribution if secure=true" do
34+
options = {:secure=>true, :secure_distribution=>"something.else.com"}
35+
result = Cloudinary::Utils.cloudinary_url("test", options)
36+
options.should == {}
37+
result.should == "https://something.else.com/test123/image/upload/test"
38+
end
39+
40+
it "should take secure distribution from config if secure=true" do
41+
Cloudinary.config.secure_distribution = "config.secure.distribution.com"
42+
options = {:secure=>true}
43+
result = Cloudinary::Utils.cloudinary_url("test", options)
44+
options.should == {}
45+
result.should == "https://config.secure.distribution.com/test123/image/upload/test"
46+
end
47+
48+
it "should raise exception if secure is given with private_cdn and no secure_distribution" do
49+
Cloudinary.config.private_cdn = true
50+
lambda {Cloudinary::Utils.cloudinary_url("test", :secure=>true)}.should raise_error
51+
end
52+
53+
it "should use format from options" do
54+
options = {:format=>:jpg}
55+
result = Cloudinary::Utils.cloudinary_url("test", options)
56+
options.should == {}
57+
result.should == "http://res.cloudinary.com/test123/image/upload/test.jpg"
58+
end
59+
60+
it "should use width and height from options only if crop is given" do
61+
options = {:width=>100, :height=>100}
62+
result = Cloudinary::Utils.cloudinary_url("test", options)
63+
result.should == "http://res.cloudinary.com/test123/image/upload/test"
64+
options.should == {:width=>100, :height=>100}
65+
options = {:width=>100, :height=>100, :crop=>:crop}
66+
result = Cloudinary::Utils.cloudinary_url("test", options)
67+
options.should == {:width=>100, :height=>100}
68+
result.should == "http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/test"
69+
end
70+
71+
it "should use x, y, radius, prefix, gravity and quality from options" do
72+
options = {:x=>1, :y=>2, :radius=>3, :gravity=>:center, :quality=>0.4, :prefix=>"a"}
73+
result = Cloudinary::Utils.cloudinary_url("test", options)
74+
options.should == {}
75+
result.should == "http://res.cloudinary.com/test123/image/upload/g_center,p_a,q_0.4,r_3,x_1,y_2/test"
76+
end
77+
78+
it "should support named tranformation" do
79+
options = {:transformation=>"blip"}
80+
result = Cloudinary::Utils.cloudinary_url("test", options)
81+
options.should == {}
82+
result.should == "http://res.cloudinary.com/test123/image/upload/t_blip/test"
83+
end
84+
85+
it "should support array of named tranformations" do
86+
options = {:transformation=>["blip", "blop"]}
87+
result = Cloudinary::Utils.cloudinary_url("test", options)
88+
options.should == {}
89+
result.should == "http://res.cloudinary.com/test123/image/upload/t_blip.blop/test"
90+
end
91+
92+
it "should support base tranformation" do
93+
options = {:transformation=>{:x=>100, :y=>100, :crop=>:fill}, :crop=>:crop, :width=>100}
94+
result = Cloudinary::Utils.cloudinary_url("test", options)
95+
options.should == {:width=>100}
96+
result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,x_100,y_100/c_crop,w_100/test"
97+
end
98+
99+
it "should support array of base tranformations" do
100+
options = {:transformation=>[{:x=>100, :y=>100, :width=>200, :crop=>:fill}, {:radius=>10}], :crop=>:crop, :width=>100}
101+
result = Cloudinary::Utils.cloudinary_url("test", options)
102+
options.should == {:width=>100}
103+
result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,w_200,x_100,y_100/r_10/c_crop,w_100/test"
104+
end
105+
106+
it "should not include empty tranformations" do
107+
options = {:transformation=>[{}, {:x=>100, :y=>100, :crop=>:fill}, {}]}
108+
result = Cloudinary::Utils.cloudinary_url("test", options)
109+
options.should == {}
110+
result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,x_100,y_100/test"
111+
end
112+
113+
it "should support size" do
114+
options = {:size=>"10x10", :crop=>:crop}
115+
result = Cloudinary::Utils.cloudinary_url("test", options)
116+
options.should == {:width=>"10", :height=>"10"}
117+
result.should == "http://res.cloudinary.com/test123/image/upload/c_crop,h_10,w_10/test"
118+
end
119+
120+
it "should use type from options" do
121+
options = {:type=>:facebook}
122+
result = Cloudinary::Utils.cloudinary_url("test", options)
123+
options.should == {}
124+
result.should == "http://res.cloudinary.com/test123/image/facebook/test"
125+
end
126+
127+
it "should use resource_type from options" do
128+
options = {:resource_type=>:raw}
129+
result = Cloudinary::Utils.cloudinary_url("test", options)
130+
options.should == {}
131+
result.should == "http://res.cloudinary.com/test123/raw/upload/test"
132+
end
133+
134+
it "should ignore http links only if type is not given or is asset" do
135+
options = {:type=>nil}
136+
result = Cloudinary::Utils.cloudinary_url("http://test", options)
137+
options.should == {}
138+
result.should == "http://test"
139+
options = {:type=>:asset}
140+
result = Cloudinary::Utils.cloudinary_url("http://test", options)
141+
options.should == {}
142+
result.should == "http://test"
143+
options = {:type=>:fetch}
144+
result = Cloudinary::Utils.cloudinary_url("http://test", options)
145+
options.should == {}
146+
result.should == "http://res.cloudinary.com/test123/image/fetch/http://test"
147+
end
148+
149+
it "should use allow absolute links to /images" do
150+
options = {}
151+
result = Cloudinary::Utils.cloudinary_url("/images/test", options)
152+
options.should == {}
153+
result.should == "http://res.cloudinary.com/test123/image/upload/test"
154+
end
155+
156+
it "should use ignore absolute links not to /images" do
157+
options = {}
158+
result = Cloudinary::Utils.cloudinary_url("/js/test", options)
159+
options.should == {}
160+
result.should == "/js/test"
161+
end
162+
163+
it "should escape fetch urls" do
164+
options = {:type=>:fetch}
165+
result = Cloudinary::Utils.cloudinary_url("http://blah.com/hello?a=b", options)
166+
options.should == {}
167+
result.should == "http://res.cloudinary.com/test123/image/fetch/http://blah.com/hello%3Fa%3Db"
168+
end
169+
end

0 commit comments

Comments
 (0)