diff --git a/Gemfile b/Gemfile index 2844b87..14ce656 100644 --- a/Gemfile +++ b/Gemfile @@ -15,9 +15,10 @@ group :assets do gem 'bootswatch-rails' end -group :development, :test do +group :test do gem 'database_cleaner' gem 'factory_girl_rails' + gem 'fakeweb' gem 'guard-test' gem 'ruby-prof' gem 'shoulda-context' @@ -28,11 +29,14 @@ group :development do gem 'capistrano' gem 'capistrano-unicorn', :require => false gem 'rvm-capistrano' - gem "better_errors" gem 'debugger' end +group :development, :test do + gem 'rb-inotify', '~> 0.9' +end + gem 'devise' gem 'haml' gem 'sass' diff --git a/Gemfile.lock b/Gemfile.lock index f7ea30c..3c77251 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,6 +108,8 @@ GEM factory_girl_rails (4.2.1) factory_girl (~> 4.2.0) railties (>= 3.0.0) + fakeweb (1.3.0) + ffi (1.4.0) fssm (0.2.10) guard (1.6.2) listen (>= 0.6.0) @@ -207,6 +209,8 @@ GEM thor (>= 0.14.6, < 2.0) raindrops (0.10.0) rake (10.0.4) + rb-inotify (0.9.0) + ffi (>= 0.5.0) rdoc (3.12.2) json (~> 1.4) redis (3.0.3) @@ -297,6 +301,7 @@ DEPENDENCIES debugger devise factory_girl_rails + fakeweb guard-test haml haml-rails @@ -308,6 +313,7 @@ DEPENDENCIES oj rabl rails (~> 3.2.8) + rb-inotify (~> 0.9) ruby-prof rvm-capistrano sass diff --git a/app/services/creator/drop_box_document.rb b/app/services/creator/drop_box_document.rb new file mode 100644 index 0000000..1795cf3 --- /dev/null +++ b/app/services/creator/drop_box_document.rb @@ -0,0 +1,23 @@ +module Creator + class DropBoxDocument + def self.can_create?(document_params) + document_params.has_key? :files + end + + def initialize(document_params) + @document_params = document_params + end + + def document_links + @document_params[:files] + end + + def valid? + true + end + + def save + Resque.enqueue(DropBoxCreatorTask, document_links) + end + end +end diff --git a/app/services/creator/null_document.rb b/app/services/creator/null_document.rb new file mode 100644 index 0000000..990a525 --- /dev/null +++ b/app/services/creator/null_document.rb @@ -0,0 +1,18 @@ +module Creator + class NullDocument + def self.can_create?(document_params) + true + end + + def initialize(document_params) + end + + def valid? + false + end + + def save + false + end + end +end diff --git a/app/services/creator/single_document.rb b/app/services/creator/single_document.rb new file mode 100644 index 0000000..1b00fce --- /dev/null +++ b/app/services/creator/single_document.rb @@ -0,0 +1,25 @@ +module Creator + class SingleDocument + def self.can_create?(document_params) + document_params.has_key? :file + end + + def initialize(document_params) + file = document_params.delete(:file) + @document = Document.new document_params + if file + @document.original_filename = file.original_filename + @document.file = file.path + end + end + + def valid? + @document.valid? + end + + def save + @document.save + @document.enqueue_process + end + end +end diff --git a/app/services/documents_creator.rb b/app/services/documents_creator.rb new file mode 100644 index 0000000..5faed52 --- /dev/null +++ b/app/services/documents_creator.rb @@ -0,0 +1,26 @@ +class DocumentsCreator + STRATEGIES = %w(SingleDocument DropBoxDocument NullDocument) + def initialize(document_params) + @document_params = document_params + @strategy = pick_strategy + end + + def valid? + @strategy.valid? + end + + def save + @strategy.save + end + +private + + def creator(klass) + "Creator::#{klass}".constantize + end + + def pick_strategy + strategy_klass = STRATEGIES.find { |klass| creator(klass).can_create?(@document_params) } + creator(strategy_klass).new @document_params + end +end diff --git a/app/services/drop_box_creator_task.rb b/app/services/drop_box_creator_task.rb new file mode 100644 index 0000000..415a7de --- /dev/null +++ b/app/services/drop_box_creator_task.rb @@ -0,0 +1,7 @@ +class DropBoxCreatorTask + @queue = :misc + + def self.perform(documents_list) + raise "Not implemented yet" + end +end diff --git a/app/services/file_downloader.rb b/app/services/file_downloader.rb new file mode 100644 index 0000000..7e60d25 --- /dev/null +++ b/app/services/file_downloader.rb @@ -0,0 +1,31 @@ +require 'uri' +require 'net/http' + +class FileDownloader + attr_accessor :file + + def initialize(url) + @uri = URI(url) + end + + def download + @file = Tempfile.new(filename, encoding: "ascii-8bit") + Net::HTTP.start(host) do |http| + resp = http.get(path) + @file.write(resp.body) + end + @file + end + + def filename + @filename ||= @uri.path.split("/").last + end + + def path + @uri.path + end + + def host + @uri.host + end +end diff --git a/app/views/documents/new.html.haml b/app/views/documents/new.html.haml index 687248f..cb67d65 100644 --- a/app/views/documents/new.html.haml +++ b/app/views/documents/new.html.haml @@ -17,5 +17,11 @@ = f.input :description = f.input :category, :collection => %w(Alegato Articulo Fundamentos Pericia Sentencia Testimonio Veredicto otro) = f.input :file, :as => :file + .input.string.optional + %label.string.optional + Or choose some files from your dropbox + %input.string.optional{name: "document[selected-files]", style: "visibility: hidden;", type: "dropbox-chooser", "data-multiselect" => true, "data-link-type" => "direct"} = f.input :public, :as => :checked_boolean = f.button :submit + +%script#dropboxjs{"data-app-key" => "throly54gy6mhm9", :src => "https://www.dropbox.com/static/api/1/dropins.js", :type => "text/javascript"} diff --git a/test/services/documents_creator_test.rb b/test/services/documents_creator_test.rb new file mode 100644 index 0000000..033b0cb --- /dev/null +++ b/test/services/documents_creator_test.rb @@ -0,0 +1,70 @@ +require 'test_helper' + +class DocumentsCreatorTest < ActiveSupport::TestCase + setup do + temp_file = Tempfile.new "document" + temp_file.write("document content") + uploaded_file = Rack::Test::UploadedFile.new(temp_file.path, "text/plain") + + @normal_document_params = { + title: "Title", + description: "desciption", + file: uploaded_file + } + @invalid_params = { + title: "Title", + description: "desciption" + } + @multiple_documents_params = { + files: [ + "https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2011.pdf", + "https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2012.pdf" + ] + } + @single_document_params = { + files: [ + "https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2011.pdf" + ] + } + end + + should "Create a document uploading the file" do + documents_creator = DocumentsCreator.new @normal_document_params + + assert documents_creator.valid? + assert_difference("Document.count", 1) do + documents_creator.save + end + end + + should "Not create a document with invalid params" do + documents_creator = DocumentsCreator.new @invalid_params + + assert_equal false, documents_creator.valid? + assert_equal false, documents_creator.save + end + + should "Create a single dropbox document with valid params" do + documents_creator = DocumentsCreator.new @single_document_params + + assert documents_creator.valid? + assert_difference(Document.count, 1) do + documents_creator.save + end + end + + #should "Create a group of documents" do + #documents_creator = DocumentsCreator.new @multiple_documents_params + #assert documents_creator.valid? + #assert_difference(Document.count, 2) do + #documents_creator.save + #end + #end + + #should "Not create document with invalid params" do + #documents_creator = DocumentsCreator.new @invalid_params + #assert_equal documents_creator.valid?, false + #assert_equal documents_creator.save, false + #assert_instance_of documents_creator.errors, ActiveModel::Errors + #end +end diff --git a/test/services/file_downloader_test.rb b/test/services/file_downloader_test.rb new file mode 100644 index 0000000..dd88af7 --- /dev/null +++ b/test/services/file_downloader_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class FileDownladerTest < ActiveSupport::TestCase + setup do + @link = "http://examle.org/condenas_jujuy.pdf" + FakeWeb.register_uri = :get, @link, { + body: File.open("test/support/condenas_jujuy.pdf").read, + content_lenth: 1000, + content_type: "application/pdf" + } + end + + should "provide filename" do + file_dowloader = FileDownloader.new @link + assert_equal file_dowloader.filename, "condenas_jujuy.pdf" + end + + should "Download a file" do + file_dowloader = FileDownloader.new @link + assert_instance_of Tempfile, file_dowloader.download + end +end diff --git a/test/support/condenas_jujuy.pdf b/test/support/condenas_jujuy.pdf new file mode 100644 index 0000000..40a3844 Binary files /dev/null and b/test/support/condenas_jujuy.pdf differ