Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added JSON serialization, Closes #41

  • Loading branch information...
commit ce0d02631cb5c34493df7bfb07ca7f28db7a7808 1 parent 30efbd5
@jnicklas jnicklas authored
View
1  Rakefile
@@ -29,6 +29,7 @@ $hoe = Hoe.spec 'carrierwave' do
self.extra_dev_deps << ['mongoid', '>=0.10.4']
self.extra_dev_deps << ['aws-s3', '>=0.6.2']
self.extra_dev_deps << ['timecop', '>=0.3.4']
+ self.extra_dev_deps << ['json', '>=1.1.9']
self.extra_rdoc_files << 'README.rdoc'
end
View
11 lib/carrierwave/uploader/url.rb
@@ -19,6 +19,15 @@ def url
alias_method :to_s, :url
+ ##
+ # === Returns
+ #
+ # [String] A JSON serializtion containing this uploader's URL
+ #
+ def to_json
+ { 'url' => url }.to_json
+ end
+
end # Url
end # Uploader
-end # CarrierWave
+end # CarrierWave
View
1  spec/spec_helper.rb
@@ -13,6 +13,7 @@
require 'carrierwave'
require 'timecop'
require 'time'
+require 'json'
require 'logger'
View
17 spec/uploader/url_spec.rb
@@ -63,6 +63,21 @@
end
end
+ describe '#to_json' do
+ before do
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
+ end
+
+ it "should return a hash with a blank URL" do
+ JSON.parse(@uploader.to_json)['url'].should be_nil
+ end
+
+ it "should return a hash including a cached URL" do
+ @uploader.cache!(File.open(file_path('test.jpg')))
+ JSON.parse(@uploader.to_json)['url'].should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
+ end
+ end
+
describe '#to_s' do
before do
CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
@@ -84,4 +99,4 @@
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.