Permalink
Browse files

Make gem usable and write tests

  • Loading branch information...
1 parent efdbb9b commit 873aa4af0f627cb689d21db0df1287b246e5ec7d @janko-m committed Sep 3, 2012
Showing with 27,501 additions and 105 deletions.
  1. +3 −13 .gitignore
  2. +4 −0 Gemfile
  3. +5 −0 Rakefile
  4. +2 −2 lib/paperclip-dropbox.rb
  5. +2 −0 lib/paperclip/dropbox.rb
  6. +9 −0 lib/paperclip/dropbox/railtie.rb
  7. +38 −0 lib/paperclip/dropbox/rake.rb
  8. +9 −0 lib/paperclip/dropbox/tasks/authorize.rake
  9. +43 −63 lib/paperclip/storage/dropbox.rb
  10. +0 −11 lib/paperclip/storage/dropbox/railtie.rb
  11. +0 −16 lib/tasks/paperclip-dropbox.rake
  12. +9 −0 paperclip-dropbox.gemspec
  13. +5 −0 spec/dropbox.yml
  14. BIN spec/files/another_photo.jpg
  15. BIN spec/files/photo.jpg
  16. +251 −0 spec/paperclip/storage/dropbox_spec.rb
  17. +64 −0 spec/spec_helper.rb
  18. +1,584 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/CUD/create/puts_the_file_on_Dropbox.yml
  19. +1,584 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/CUD/create/raises_an_exception_on_same_filenames.yml
  20. +1,576 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/CUD/destroy/deletes_the_uploaded_file.yml
  21. +1,533 −0 ...es/Paperclip_Storage_Dropbox/CUD/destroy/doesn_t_raise_errors_if_there_are_no_files_to_delete.yml
  22. +3,157 −0 ...r_cassettes/Paperclip_Storage_Dropbox/CUD/update/deletes_the_old_file_and_uploads_the_new_one.yml
  23. +1,576 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/CUD/update/deletes_the_old_file_if_set_to_nil.yml
  24. +2,064 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/_url/accepts_the_download_option.yml
  25. +2,060 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/_url/is_valid.yml
  26. +46 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/arguments/dropbox_credentials/accepts_a_hash.yml
  27. +46 −0 .../vcr_cassettes/Paperclip_Storage_Dropbox/arguments/dropbox_credentials/accepts_a_path_to_file.yml
  28. +46 −0 spec/vcr_cassettes/Paperclip_Storage_Dropbox/arguments/dropbox_credentials/accepts_an_open_file.yml
  29. +46 −0 ...vcr_cassettes/Paperclip_Storage_Dropbox/arguments/dropbox_credentials/recognizes_environments.yml
  30. +2,357 −0 ...ttes/Paperclip_Storage_Dropbox/arguments/dropbox_options/path/doesn_t_duplicate_the_extension.yml
  31. +2,357 −0 ...es/Paperclip_Storage_Dropbox/arguments/dropbox_options/path/has_the_original_filename_default.yml
  32. +2,400 −0 ...ions/path/puts_the_instance_in_the_scope_passes_the_style_and_appends_the_extension_and_style.yml
  33. +4,625 −0 ...aperclip_Storage_Dropbox/arguments/dropbox_options/unique_filename/makes_the_file_path_unique.yml
View
@@ -1,17 +1,7 @@
-*.gem
-*.rbc
.bundle
-.config
-.yardoc
Gemfile.lock
-InstalledFiles
-_yardoc
-coverage
doc/
-lib/bundler/man
-pkg
-rdoc
-spec/reports
-test/tmp
-test/version_tmp
+pkg/
tmp
+.rspec
+*.DS_Store
View
@@ -1,3 +1,7 @@
source :rubygems
gemspec
+
+group :test do
+ gem "debugger"
+end
View
@@ -1,2 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+require "paperclip/dropbox/rake"
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+task default: :spec
View
@@ -1,2 +1,2 @@
-require "paperclip/storage/dropbox"
-require "paperclip/storage/dropbox/railtie" if defined?(Rails)
+require "paperclip"
+require "paperclip/dropbox"
View
@@ -0,0 +1,2 @@
+require "paperclip/storage/dropbox"
+require "paperclip/dropbox/railtie" if defined?(Rails)
@@ -0,0 +1,9 @@
+module Paperclip
+ module Dropbox
+ class Railtie < Rails::Railtie
+ rake_tasks do
+ load "paperclip/dropbox/tasks/authorize.rake"
+ end
+ end
+ end
+end
@@ -0,0 +1,38 @@
+require "dropbox_sdk"
+
+module Paperclip
+ module Dropbox
+ module Rake
+ extend self
+
+ def authorize(app_key, app_secret)
+ session = create_new_session(app_key, app_secret)
+ puts "Visit this URL: #{session.get_authorize_url}"
+ print "And after you approved the authorization confirm it here (y/n): "
+ assert_answer!
+ session.get_access_token
+ dropbox_client = DropboxClient.new(session, "dropbox")
+ account_info = dropbox_client.account_info
+
+ puts <<-MESSAGE
+Authorization was successful. Here you go:
+
+access_token: #{session.access_token.key}
+access_token_secret: #{session.access_token.secret}
+user_id: #{account_info["uid"]}
+ MESSAGE
+ end
+
+ def create_new_session(app_key, app_secret)
+ DropboxSession.new(app_key, app_secret)
+ end
+
+ def assert_answer!
+ answer = STDIN.gets.strip
+ exit if answer == "n"
+ end
+ end
+ end
+end
+
+load "paperclip/dropbox/tasks/authorize.rake"
@@ -0,0 +1,9 @@
+require "rake"
+require "paperclip/dropbox/rake" unless defined?(Paperclip::Dropbox::Rake)
+
+namespace :dropbox do
+ desc "Obtains your credentials"
+ task :authorize do
+ Paperclip::Dropbox::Rake.authorize(ENV["APP_KEY"], ENV["APP_SECRET"])
+ end
+end
@@ -9,22 +9,21 @@ module Storage
module Dropbox
def self.extended(base)
base.instance_eval do
- @dropbox_settings = parse_settings(@options[:dropbox_settings] || {})
- @dropbox_settings.update(@options[:dropbox_options] || {})
-
+ @dropbox_credentials = parse_credentials(@options[:dropbox_credentials] || {})
+ @dropbox_options = @options[:dropbox_options] || {}
+ environment = defined?(Rails) ? Rails.env : @dropbox_options[:environment].to_s
+ @dropbox_credentials = (@dropbox_credentials[environment] || @dropbox_credentials).symbolize_keys
end
end
def flush_writes
@queued_for_write.each do |style, file|
unless exists?(style)
dropbox_client.put_file(path(style), file.read)
- instance_write(:url, dropbox_client.media(path(style))["url"])
else
- raise FileExists, "\"#{path(style)}\" already exists on Dropbox"
+ raise FileExists, "file \"#{path(style)}\" already exists on Dropbox"
end
end
-
after_flush_writes
@queued_for_write = {}
end
@@ -37,33 +36,28 @@ def flush_deletes
end
def exists?(style)
- !!url(style)
+ !!dropbox_client.media(path(style))
+ rescue DropboxError
+ false
end
def url(*args)
style = args.first.is_a?(Symbol) ? args.first : default_style
- style_suffix = (style != default_style ? "_#{style}" : "")
-
options = args.last.is_a?(Hash) ? args.last : {}
query = options[:download] ? "?dl=1" : ""
- instance_read(:url).sub(/(?=\.\w{3,4}$)/, style_suffix) + query
-
- rescue DropboxError
- nil
+ File.join("http://dl.dropbox.com/u/#{user_id}", path_for_url(style) + query)
end
def path(style)
- extension = original_filename[/\.\w{3,4}$/]
- result = file_path
- file_path.scan(/\<\w+\>/).each do |keyword|
- result.sub!(keyword, dropbox_keywords[keyword].call(style).to_s)
- end
- style_suffix = (style != default_style ? "_#{style}" : "")
- result = "#{result}#{style_suffix}#{extension}"
+ File.join("Public", path_for_url(style))
+ end
- rescue
- nil
+ def path_for_url(style)
+ result = instance.instance_exec(style, &file_path)
+ result += extension if result !~ /\.\w{3,4}$/
+ style_suffix = (style != default_style ? "_#{style}" : "")
+ result.sub(extension, "#{style_suffix}#{extension}")
end
def copy_to_local_file(style, destination_path)
@@ -74,63 +68,49 @@ def copy_to_local_file(style, destination_path)
private
- def file_path
- return @dropbox_settings[:path] if @dropbox_settings[:path]
-
- if @dropbox_settings[:unique_filename]
- "<model_name>_<record_id>_<attachment_name>"
- else
- "<filename>"
- end
+ def extension
+ original_filename[/\.\w{3,4}$/]
end
- def parse_settings(settings)
- settings = settings.respond_to?(:call) ? settings.call : settings
- settings = get_settings(settings).stringify_keys
- environment = defined?(Rails) ? Rails.env : @dropbox_settings[:environment].to_s
- (settings[environment] || settings).symbolize_keys
+ def user_id
+ @dropbox_credentials[:user_id]
end
- def get_settings(settings)
- case settings
- when File
- YAML.load(ERB.new(File.read(settings.path)).result)
- when String, Pathname
- YAML.load(ERB.new(File.read(settings)).result)
- when Hash
- settings
+ def file_path
+ return @dropbox_options[:path] if @dropbox_options[:path]
+
+ if @dropbox_options[:unique_filename]
+ eval %(proc { |style| "\#{self.class.model_name.underscore}_\#{id}_\#{#{name}.name}" })
else
- raise ArgumentError, "settings are not a path, file, or hash."
+ eval %(proc { |style| #{name}.original_filename })
end
end
def dropbox_client
@dropbox_client ||= begin
- session = DropboxSession.new(@dropbox_settings[:app_key], @dropbox_settings[:app_secret])
- session.set_access_token(@dropbox_settings[:access_token], @dropbox_settings[:access_token_secret])
- DropboxClient.new(session, @dropbox_settings[:access_type] || :app_folder)
+ session = DropboxSession.new(@dropbox_credentials[:app_key], @dropbox_credentials[:app_secret])
+ session.set_access_token(@dropbox_credentials[:access_token], @dropbox_credentials[:access_token_secret])
+ DropboxClient.new(session, "dropbox")
end
end
- def dropbox_keywords
- @dropbox_keywords ||= begin
- hash = Hash.new do |hash, key|
- if key =~ /^\<record_.+\>$/
- attribute = key.match(/^\<record_(.+)\>$/)[1]
- hash[key] = lambda { |style| instance.send(attribute) }
- end
+ def parse_credentials(credentials)
+ result =
+ case credentials
+ when File
+ YAML.load(ERB.new(File.read(credentials.path)).result)
+ when String, Pathname
+ YAML.load(ERB.new(File.read(credentials)).result)
+ when Hash
+ credentials
+ else
+ raise ArgumentError, ":dropbox_credentials are not a path, file, nor a hash"
end
- hash.update(
- "<model_name>" => lambda { |style| instance.class.table_name.singularize },
- "<table_name>" => lambda { |style| instance.class.table_name },
- "<filename>" => lambda { |style| original_filename.match(/\.\w{3,4}$/).pre_match },
- "<attachment_name>" => lambda { |style| name },
- "<style>" => lambda { |style| style }
- )
- end
+
+ result.stringify_keys
end
- class FileExists < RuntimeError
+ class FileExists < ArgumentError
end
end
end
@@ -1,11 +0,0 @@
-module Paperclip
- module Storage
- module Dropbox
- class Railtie < Rails::Railtie
- rake_tasks do
- load "tasks/paperclip-dropbox.rake"
- end
- end
- end
- end
-end
@@ -1,16 +0,0 @@
-require 'rake'
-require 'dropbox_sdk'
-
-namespace :dropbox do
- desc "Obtain your access token"
- task :authorize do
- session = DropboxSession.new(ENV["APP_KEY"], ENV["APP_SECRET"])
- puts "Visit this URL: #{session.get_authorize_url}"
- print "And after you approved the authorization confirm it here (y/n): "
- answer = STDIN.gets.strip
- exit if answer == "n"
- session.get_access_token
- puts "Access token: #{session.access_token.key}"
- puts "Access token secret: #{session.access_token.secret}"
- end
-end
@@ -15,5 +15,14 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
+ gem.add_dependency "paperclip", "~> 3.1"
gem.add_dependency "dropbox-sdk", "~> 1.3"
+
+ gem.add_development_dependency "rake", "~> 0.9"
+ gem.add_development_dependency "rspec", "~> 2.11"
+ gem.add_development_dependency "vcr", "~> 2.2"
+ gem.add_development_dependency "fakeweb", "~> 1.3"
+ gem.add_development_dependency "activerecord", "~> 3.2"
+ gem.add_development_dependency "rack-test", "~> 0.6"
+ gem.add_development_dependency "sqlite3", "~> 1.3"
end
View
@@ -0,0 +1,5 @@
+app_key: <%= ENV["DROPBOX_APP_KEY"] %>
+app_secret: <%= ENV["DROPBOX_APP_SECRET"] %>
+access_token: <%= ENV["DROPBOX_ACCESS_TOKEN"] %>
+access_token_secret: <%= ENV["DROPBOX_ACCESS_TOKEN_SECRET"] %>
+user_id: 16783504
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit 873aa4a

Please sign in to comment.