Skip to content

Commit

Permalink
Added JSON serialization, Closes carrierwaveuploader#41
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Feb 18, 2010
1 parent a370eba commit 4f271aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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

Expand Down
11 changes: 10 additions & 1 deletion lib/carrierwave/uploader/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'carrierwave'
require 'timecop'
require 'time'
require 'json'

require 'logger'

Expand Down
17 changes: 16 additions & 1 deletion spec/uploader/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -84,4 +99,4 @@
end
end

end
end

0 comments on commit 4f271aa

Please sign in to comment.