Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 8333284014c40134f5e9d320966386e26735c114 @aubreyrhodes committed Nov 21, 2011
@@ -0,0 +1,7 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+.rvmrc*
+coverage/
+coverage.data
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in scribd-carrierwave.gemspec
+gemspec
@@ -0,0 +1,23 @@
+require 'bundler/gem_tasks'
+require 'rake'
+require 'rspec/core/rake_task'
+require 'cover_me'
+
+desc "Run all specs"
+RSpec::Core::RakeTask.new('spec')
+
+task default: 'spec'
+
+namespace :cover_me do
+
+ desc "Generates and opens code coverage report."
+ task :report do
+ require 'cover_me'
+ CoverMe.complete!
+ end
+
+end
+
+task :spec do
+ Rake::Task['cover_me:report'].invoke
+end
@@ -0,0 +1,83 @@
+require File.join(File.dirname(__FILE__), './scribd-carrierwave/version')
+require File.join(File.dirname(__FILE__), './scribd-carrierwave/config')
+require 'carrierwave'
+require 'rscribd'
+
+module ScribdCarrierWave
+ class << self
+ def included(base)
+ base.extend ClassMethods
+ end
+
+ def upload uploader
+ args = { file: uploader.url, access: 'private' }
+ scribd_user.upload(args)
+ end
+
+ def destroy uploader
+ document = scribd_user.find_document(uploader.ipaper_id) rescue nil
+ document.destroy if !document.nil?
+ end
+
+ module ClassMethods
+ def has_ipaper
+ include InstanceMethods
+ after :store, :upload_to_scribd
+ before :remove, :delete_from_scribd
+ end
+ end
+
+ module InstanceMethods
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def upload_to_scribd files
+ res = ScribdCarrierWave::upload(self)
+ set_params res
+ end
+
+ def delete_from_scribd
+ ScribdCarrierWave::destroy(self)
+ end
+
+ def display_ipaper(options = {})
+ id = options.delete(:id)
+ <<-END
+ <script type="text/javascript" src="http://www.scribd.com/javascripts/view.js"></script>
+ <div id="embedded_flash#{id}">#{options.delete(:alt)}</div>
+ <script type="text/javascript">
+ var scribd_doc = scribd.Document.getDoc(#{ipaper_id}, '#{ipaper_access_key}');
+ scribd_doc.addParam("hide_disabled_buttons", true)
+ scribd_doc.write("embedded_flash#{id}");
+ </script>
+ END
+ end
+
+ def ipaper_id
+ self.model.send("#{self.mounted_as.to_s}_ipaper_id")
+ end
+
+ def ipaper_access_key
+ self.model.send("#{self.mounted_as.to_s}_ipaper_access_key")
+ end
+
+ private
+
+ def set_params res
+ self.model.update_attributes({"#{self.mounted_as}_ipaper_id" => res.doc_id,
+ "#{self.mounted_as}_ipaper_access_key" => res.access_key})
+ end
+ end
+
+ private
+
+ def scribd_user
+ Scribd::API.instance.key = ScribdCarrierWave.config.key
+ Scribd::API.instance.secret = ScribdCarrierWave.config.secret
+ @scribd_user = Scribd::User.login(ScribdCarrierWave.config.username, ScribdCarrierWave.config.password)
+ end
+ end
+end
+
+CarrierWave::Uploader::Base.send(:include, ScribdCarrierWave) if Object.const_defined?("CarrierWave")
@@ -0,0 +1,8 @@
+module ScribdCarrierWave
+ class << self
+ def config(&block)
+ yield configatron.cover_me if block_given?
+ configatron.cover_me
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Scribd
+ module Carrierwave
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "scribd-carrierwave/version"
+
+Gem::Specification.new do |s|
+ s.name = "scribd-carrierwave"
+ s.version = Scribd::Carrierwave::VERSION
+ s.authors = ["Aubrey Rhodes"]
+ s.email = ["aubrey.c.rhodes@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "scribd-carrierwave"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "mocha"
+ s.add_development_dependency "cover_me"
+ s.add_runtime_dependency "rscribd"
+ s.add_runtime_dependency "carrierwave"
+ s.add_runtime_dependency "configatron"
+end
@@ -0,0 +1,92 @@
+require File.join(File.dirname(__FILE__), %w[spec_helper])
+
+describe ScribdCarrierWave do
+ context "module methods" do
+ before(:each) do
+ @uploader = mock
+ @scribd_user_mock = mock
+ Scribd::User.stubs(:login).returns(@scribd_user_mock)
+ end
+
+ describe "upload" do
+ it "calls rscribd.upload with the correct arguments" do
+ @uploader.stubs(:url).returns('test_url')
+ @scribd_user_mock.expects(:upload).with(has_entries(file: 'test_url', access: 'private'))
+ ScribdCarrierWave::upload @uploader
+ end
+ end
+
+ describe "destroy" do
+ it "gets the correct document to destroy and calls destroy" do
+ @uploader.stubs(:ipaper_id).returns('test_id')
+ document = mock
+ document.expects(:destroy)
+ @scribd_user_mock.expects(:find_document).with('test_id').returns document
+ ScribdCarrierWave::destroy @uploader
+ end
+ end
+ end
+
+ context "instance methods" do
+ before(:each) do
+ @uploader = CarrierWave::Uploader::Base.new
+ @uploader.class_eval{has_ipaper}
+ @model = mock
+ @uploader.stubs(:model).returns(@model)
+ @mounted_as = :test_model
+ @uploader.stubs(:mounted_as).returns(@mounted_as)
+ end
+
+ describe "upload_to_scribd" do
+ it "calls upload" do
+ res = mock
+ res.stubs(:doc_id).returns("test_id")
+ res.stubs(:access_key).returns("test_access_key")
+ @model.expects(:update_attributes).with(has_entries('test_model_ipaper_id' => 'test_id',
+ 'test_model_ipaper_access_key' => 'test_access_key'))
+ .returns true
+ ScribdCarrierWave.expects(:upload).with(@uploader).returns(res)
+ @uploader.upload_to_scribd nil
+ end
+ end
+
+ describe "delete_from_scribd" do
+ it "calls destroy" do
+ ScribdCarrierWave.expects(:destroy).with(@uploader)
+ @uploader.delete_from_scribd
+ end
+ end
+
+ describe "display_ipaper" do
+ before(:each) do
+ @uploader.stubs(:ipaper_id).returns('test_id')
+ @uploader.stubs(:ipaper_access_key).returns('test_access_key')
+ end
+
+ it "returns an html string with the ipaper_id and ipaper_access_key included" do
+ html = @uploader.display_ipaper
+ html.should match /test_id/
+ html.should match /test_access_key/
+ end
+
+ it "sets the correct html id attributes" do
+ html = @uploader.display_ipaper({id: 'test_id'})
+ html.should match /id="embedded_flashtest_id"/
+ end
+ end
+
+ describe "ipaper_id" do
+ it "should return the value of the model's attribute" do
+ @model.expects(:test_model_ipaper_id)
+ @uploader.ipaper_id
+ end
+ end
+
+ describe "ipaper_access_key" do
+ it "should return the value of the model's attribute" do
+ @model.expects(:test_model_ipaper_access_key)
+ @uploader.ipaper_access_key
+ end
+ end
+ end
+end
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'cover_me'
+require File.join(File.dirname(__FILE__), '../lib/scribd-carrierwave.rb')
+require 'mocha'
+
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
+
+CoverMe.config do |c|
+ c.project.root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+ c.file_pattern = [
+ /(#{c.project.root}.+\.rb)/i
+ ]
+end

2 comments on commit 8333284

I think of the stubbing and mocking in the tests above as BDD.

I'm not a big fan of BDD though I've not practiced it enough to properly criticize or pass judgement. I'd love to hear your impetus and thoughts on using it vs. TDD.

The one plus that comes to mind is it plays nicely with external resources?

Owner

aubreyrhodes replied Nov 23, 2011

To be honest, I probably don't follow a testing methodology as closely as I could. I like to use the mocking because it helps to decouple the code that I'm testing so that I can try to make my unit tests as granular as possible.

Please sign in to comment.