fileturn 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +107 -0
- data/Rakefile +1 -0
- data/fileturn.gemspec +27 -0
- data/lib/fileturn/exceptions.rb +30 -0
- data/lib/fileturn/http_client.rb +49 -0
- data/lib/fileturn/resources/account.rb +46 -0
- data/lib/fileturn/resources/file.rb +112 -0
- data/lib/fileturn/resources/notification.rb +16 -0
- data/lib/fileturn/resources/resource.rb +17 -0
- data/lib/fileturn/resources/upload.rb +77 -0
- data/lib/fileturn/version.rb +3 -0
- data/lib/fileturn.rb +19 -0
- data/spec/.DS_Store +0 -0
- data/spec/data/.DS_Store +0 -0
- data/spec/data/testfile.docx +0 -0
- data/spec/fileturn_spec.rb +16 -0
- data/spec/fixtures/vcr_cassettes/account_info.yml +47 -0
- data/spec/fixtures/vcr_cassettes/file_all.yml +60 -0
- data/spec/fixtures/vcr_cassettes/file_all_notifications.yml +60 -0
- data/spec/fixtures/vcr_cassettes/file_convert_no_params.yml +44 -0
- data/spec/fixtures/vcr_cassettes/file_convert_success.yml +46 -0
- data/spec/fixtures/vcr_cassettes/file_doesnt_upload_since_too_big.yml +46 -0
- data/spec/fixtures/vcr_cassettes/file_failed_.yml +133 -0
- data/spec/fixtures/vcr_cassettes/file_notifications.yml +46 -0
- data/spec/fixtures/vcr_cassettes/file_queued_.yml +133 -0
- data/spec/fixtures/vcr_cassettes/file_reload.yml +89 -0
- data/spec/fixtures/vcr_cassettes/file_success.yml +46 -0
- data/spec/fixtures/vcr_cassettes/file_success_.yml +133 -0
- data/spec/fixtures/vcr_cassettes/file_time_taken.yml +133 -0
- data/spec/fixtures/vcr_cassettes/file_unauthorized.yml +44 -0
- data/spec/fixtures/vcr_cassettes/file_upload_doc_queued.yml +739 -0
- data/spec/fixtures/vcr_cassettes/file_upload_not_enough_credits.yml +87 -0
- data/spec/fixtures/vcr_cassettes/file_upload_queued.yml +160 -0
- data/spec/fixtures/vcr_cassettes/upload_all.yml +46 -0
- data/spec/fixtures/vcr_cassettes/upload_one.yml +46 -0
- data/spec/fixtures/vcr_cassettes/upload_refetch.yml +89 -0
- data/spec/resources/account_spec.rb +31 -0
- data/spec/resources/file_spec.rb +184 -0
- data/spec/resources/upload_spec.rb +41 -0
- data/spec/spec_helper.rb +19 -0
- metadata +198 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            !binary "U0hBMQ==":
         | 
| 3 | 
            +
              metadata.gz: !binary |-
         | 
| 4 | 
            +
                NDM5ODQ3OTRjNzQ1YTczNDViZDIzNTJjNjkzZWQ0MjUyMmU3NDQxNw==
         | 
| 5 | 
            +
              data.tar.gz: !binary |-
         | 
| 6 | 
            +
                NmU0OGY3MTg4ZDQyMmUxMmY0ZTMwNGZkNDc3ZTA4NTRjN2FhYzY3ZQ==
         | 
| 7 | 
            +
            !binary "U0hBNTEy":
         | 
| 8 | 
            +
              metadata.gz: !binary |-
         | 
| 9 | 
            +
                MzBkYmNlOGY1MDY4ZTk5ZmRlNjVlZGU5M2M0NWI0YzMxNTk2OWJkYmRkZjAw
         | 
| 10 | 
            +
                OGM0MDIzNjYxNzE3MTQ0ODJhMjJmMzJiODVlOWE2NDJhMDdmNzc3MWM4OGNj
         | 
| 11 | 
            +
                MTQyOWNjZjJmM2NmOTRkMTFiNjIwN2RiOTJhYWFjNWQ5MTU0NTY=
         | 
| 12 | 
            +
              data.tar.gz: !binary |-
         | 
| 13 | 
            +
                MzIzYWYyOGViYTExOTFjZjI4MmQ2YzAxMDViYWY3MjkwNmUxNDVhMzk0NTYw
         | 
| 14 | 
            +
                MjU5OGI5NmI5ZGQ0MGRkYTBkMjkzMmIzZDI2ZTE0NzBiODkzNWNlMmMwOGQ0
         | 
| 15 | 
            +
                MzdhMWJiY2RlMzAxMzExYTJmNWNiMDgwMGEyZGQ5N2ZkODQ1ZGM=
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            Copyright (c) 2013 Nisarg Shah
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            MIT License
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 6 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 7 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 8 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 9 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 10 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 11 | 
            +
            the following conditions:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 14 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 17 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 18 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 19 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 20 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 21 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 22 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,107 @@ | |
| 1 | 
            +
            # Fileturn
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Ruby Gem Support for [FileTurn](http://fileturn.net/) Api. FileTurn converts your files to different formats. 
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                gem 'fileturn'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            And then execute:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                $ bundle
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Or install it yourself as:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                $ gem install fileturn
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            ## Configuration
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            Retrieve your API Key from [Dashboard](http://fileturn.net/dashboard/api_token).
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            	FileTurn.configure(:api_key => "[YOUR KEY]")
         | 
| 24 | 
            +
            	
         | 
| 25 | 
            +
            Thats it - now you are ready to use FileTurn.
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            ## Usage
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            ### Converting your files
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            There are multiple ways to convert a file. If your file is public, we can fetch the file and you just need to specify the url. 
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            	file = FileTurn::File.convert(
         | 
| 34 | 
            +
            		:url => "http://crypto.stanford.edu/DRM2002/darknet5.doc",
         | 
| 35 | 
            +
            		:convert_to => :pdf
         | 
| 36 | 
            +
            	)
         | 
| 37 | 
            +
            	
         | 
| 38 | 
            +
            	unless file.errors
         | 
| 39 | 
            +
            		# Woot done!
         | 
| 40 | 
            +
            	end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            If your file is stored locally, you can use
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            	file = FileTurn::File.convert(
         | 
| 45 | 
            +
            		:file => File.open("localfile.doc"),
         | 
| 46 | 
            +
            		:convert_to => :txt
         | 
| 47 | 
            +
            	)
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            Thats all you have to do to convert files. Upon calling that, your file will be queued for converting. 
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            ### How will i know when my file is done?
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            #### File Object
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            There are few ways to check if your file is done. You can repeatedly call one of these:
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            	file.reload.queued? 
         | 
| 58 | 
            +
            	file.reload.success?
         | 
| 59 | 
            +
            	file.reload.failed?
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            When your file is done, the file object will store a download link
         | 
| 62 | 
            +
             | 
| 63 | 
            +
            	while(file.reload.queued?)
         | 
| 64 | 
            +
            		sleep(5000)
         | 
| 65 | 
            +
            	end
         | 
| 66 | 
            +
            	
         | 
| 67 | 
            +
            	if file.success?
         | 
| 68 | 
            +
            		download_url = file.download_url
         | 
| 69 | 
            +
            		time_taken_for_conversion = file.time_taken
         | 
| 70 | 
            +
            	end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
            In the case you get a failure, you can retrieve the details using
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            	if(file.reload.failed?)
         | 
| 75 | 
            +
            		file.notifications.last.details
         | 
| 76 | 
            +
            	end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            #### Notifications
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            You can also request notification to be sent to you whenver the status of a file changes. Go into your [Dashboard](http://fileturn.net/dashboard/notifications) and specify a notification url. 
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            We will hit this notification url with the new status of the file and the file_id. 
         | 
| 83 | 
            +
             | 
| 84 | 
            +
             | 
| 85 | 
            +
            ## Other File Options
         | 
| 86 | 
            +
             | 
| 87 | 
            +
            You can fetch information of a single file using the file_id.
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            	FileTurn::File.find(file_id)
         | 
| 90 | 
            +
             | 
| 91 | 
            +
            Or all the files
         | 
| 92 | 
            +
            	
         | 
| 93 | 
            +
            	FileTurn::File.all
         | 
| 94 | 
            +
             | 
| 95 | 
            +
             | 
| 96 | 
            +
            ## Account Details
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            You can also fetch account details like how much credit you have. 
         | 
| 99 | 
            +
             | 
| 100 | 
            +
            	FileTurn::Account.load # fetch data from server
         | 
| 101 | 
            +
            	FileTurn::Account.credits
         | 
| 102 | 
            +
             | 
| 103 | 
            +
            Or you can use
         | 
| 104 | 
            +
             | 
| 105 | 
            +
            	FileTurn::Account.load.credits
         | 
| 106 | 
            +
             | 
| 107 | 
            +
            Thanks!
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require "bundler/gem_tasks"
         | 
    
        data/fileturn.gemspec
    ADDED
    
    | @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            # coding: utf-8
         | 
| 2 | 
            +
            lib = File.expand_path('../lib', __FILE__)
         | 
| 3 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            +
            require 'fileturn/version'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = "fileturn"
         | 
| 8 | 
            +
              spec.version       = Fileturn::VERSION
         | 
| 9 | 
            +
              spec.authors       = ["Nisarg Shah"]
         | 
| 10 | 
            +
              spec.email         = ["nisargshah100@gmail.com"]
         | 
| 11 | 
            +
              spec.description   = %q{FileTurn Client}
         | 
| 12 | 
            +
              spec.summary       = %q{FileTurn Client}
         | 
| 13 | 
            +
              spec.homepage      = ""
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              spec.files         = `git ls-files`.split($/)
         | 
| 16 | 
            +
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         | 
| 17 | 
            +
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         | 
| 18 | 
            +
              spec.require_paths = ["lib"]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              spec.add_dependency "faraday"
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              spec.add_development_dependency "bundler", "~> 1.3"
         | 
| 23 | 
            +
              spec.add_development_dependency "rake"
         | 
| 24 | 
            +
              spec.add_development_dependency "rspec"
         | 
| 25 | 
            +
              spec.add_development_dependency "vcr"
         | 
| 26 | 
            +
              spec.add_development_dependency "webmock"
         | 
| 27 | 
            +
            end
         | 
| @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              
         | 
| 3 | 
            +
              class FileTurnException < Exception
         | 
| 4 | 
            +
                attr_reader :resp
         | 
| 5 | 
            +
                def initialize(resp)
         | 
| 6 | 
            +
                  @resp = resp
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def to_s
         | 
| 10 | 
            +
                  resp
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              class UnauthorizedException < FileTurnException; end;
         | 
| 15 | 
            +
              class InternalServerException < FileTurnException; end;
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              class UnprocessableEntityException < FileTurnException
         | 
| 18 | 
            +
                attr_reader :parsed_response
         | 
| 19 | 
            +
                
         | 
| 20 | 
            +
                def initialize(resp)
         | 
| 21 | 
            +
                  super(resp)
         | 
| 22 | 
            +
                  @parsed_response = OpenStruct.new(JSON.parse(resp.body))
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def to_s
         | 
| 26 | 
            +
                  @parsed_response.to_s
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            end
         | 
| @@ -0,0 +1,49 @@ | |
| 1 | 
            +
            require 'faraday'
         | 
| 2 | 
            +
            require 'json'
         | 
| 3 | 
            +
            require 'ostruct'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module FileTurn
         | 
| 6 | 
            +
              class HttpClient
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def self.conn
         | 
| 9 | 
            +
                  @conn ||= Faraday.new(:url => 'http://fileturn.net/')
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def self.get(url, params, status_codes, &block)
         | 
| 13 | 
            +
                  make_call(:get, url, params, status_codes, &block)
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def self.post(url, params, status_codes, &block)
         | 
| 17 | 
            +
                  make_call(:post , url, params, status_codes, &block)
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| 21 | 
            +
                def self.make_call(type, url, params, status_codes, &block)
         | 
| 22 | 
            +
                  params = setup_params(params)
         | 
| 23 | 
            +
                  resp = conn.send(type, "api/#{url}") { |req| req.params = params }
         | 
| 24 | 
            +
                  if Array(status_codes).include?(resp.status)
         | 
| 25 | 
            +
                    block.call(JSON.parse(resp.body))
         | 
| 26 | 
            +
                  else
         | 
| 27 | 
            +
                    handle_error(resp)
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                def self.setup_params(params)
         | 
| 32 | 
            +
                  params['token'] = FileTurn.api_key
         | 
| 33 | 
            +
                  params
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def self.handle_error(resp)
         | 
| 37 | 
            +
                  case resp.status
         | 
| 38 | 
            +
                  when 401
         | 
| 39 | 
            +
                    raise FileTurn::UnauthorizedException.new(resp)
         | 
| 40 | 
            +
                  when 500
         | 
| 41 | 
            +
                    raise FileTurn::InternalServerException.new(resp)
         | 
| 42 | 
            +
                  when 422
         | 
| 43 | 
            +
                    OpenStruct.new(JSON.parse(resp.body))
         | 
| 44 | 
            +
                  else
         | 
| 45 | 
            +
                    nil
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
            end
         | 
| @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              class Account < Resource
         | 
| 3 | 
            +
               
         | 
| 4 | 
            +
                class << self
         | 
| 5 | 
            +
                  @account = OpenStruct.new()
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  def load
         | 
| 8 | 
            +
                    conn.get("/users.json", {}, 200) do |params|
         | 
| 9 | 
            +
                      params['created_at'] = DateTime.parse(params['created_at'])
         | 
| 10 | 
            +
                      @account = OpenStruct.new(params)
         | 
| 11 | 
            +
                      self
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  def load_only_if_not_loaded
         | 
| 16 | 
            +
                    load if @account.nil? || @account.id.nil?
         | 
| 17 | 
            +
                    self
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                  def id
         | 
| 21 | 
            +
                    @account.id
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                  def credits
         | 
| 25 | 
            +
                    @account.credits
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def created_at
         | 
| 29 | 
            +
                    @account.created_at
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  def notification_url
         | 
| 33 | 
            +
                    @account.notification_url
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  def time_zone
         | 
| 37 | 
            +
                    @account.time_zone
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def max_file_size_in_bytes
         | 
| 41 | 
            +
                    @account.max_file_size_in_bytes
         | 
| 42 | 
            +
                  end
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
            end
         | 
| @@ -0,0 +1,112 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              class File < Resource
         | 
| 3 | 
            +
               
         | 
| 4 | 
            +
                # class
         | 
| 5 | 
            +
                class << self
         | 
| 6 | 
            +
                  
         | 
| 7 | 
            +
                  def find(id, &block)
         | 
| 8 | 
            +
                    conn.get("/files/#{id}.json", {}, 200) do |params|
         | 
| 9 | 
            +
                      block ? block.call(params) : File.new(params)
         | 
| 10 | 
            +
                    end
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  def all
         | 
| 14 | 
            +
                    conn.get("files.json", {}, 200) do |params|
         | 
| 15 | 
            +
                      files = Array(params['files']).map { |p| File.new(p) }
         | 
| 16 | 
            +
                      OpenStruct.new(
         | 
| 17 | 
            +
                        :files => files, 
         | 
| 18 | 
            +
                        :total_files => params['total_files'],
         | 
| 19 | 
            +
                        :current_page => params['current_page'],
         | 
| 20 | 
            +
                        :per_page => params['per_page'],
         | 
| 21 | 
            +
                        :total_pages => params['total_pages']
         | 
| 22 | 
            +
                      )
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  def convert(params)
         | 
| 27 | 
            +
                    file = params['file'] || params[:file]
         | 
| 28 | 
            +
                    return upload_file(file, params) unless file.nil?
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                    conn.post("files.json", params, 201) do |params|
         | 
| 31 | 
            +
                      File.new(params)
         | 
| 32 | 
            +
                    end
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                private
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  def upload_file(file, params)
         | 
| 38 | 
            +
                    evaluate_file_size = FileTurn::Upload.send(:evaluate_file_size, file)
         | 
| 39 | 
            +
                    return evaluate_file_size unless evaluate_file_size.nil?
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    signed_params = FileTurn::Upload.send(:signed_upload_url, file)
         | 
| 42 | 
            +
                    return signed_params if signed_params.errors
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                    faraday = Faraday.new(:url => signed_params.url) do |conn| 
         | 
| 45 | 
            +
                      conn.request :multipart
         | 
| 46 | 
            +
                      conn.adapter :net_http
         | 
| 47 | 
            +
                    end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                    response = faraday.post '/', {
         | 
| 50 | 
            +
                      :policy => signed_params.policy,
         | 
| 51 | 
            +
                      :signature => signed_params.signature,
         | 
| 52 | 
            +
                      'AWSAccessKeyId' => signed_params.aws_access_key_id,
         | 
| 53 | 
            +
                      :key => signed_params.key,
         | 
| 54 | 
            +
                      :file => Faraday::UploadIO.new(file.path, '')
         | 
| 55 | 
            +
                    }
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    if response.status == 204
         | 
| 58 | 
            +
                      params[:file] = params['file'] = nil
         | 
| 59 | 
            +
                      params['url'] = "#{signed_params.url}/#{signed_params.key}"
         | 
| 60 | 
            +
                      params['file_type'] = signed_params.file_type
         | 
| 61 | 
            +
                      params['upload_id'] = signed_params.id
         | 
| 62 | 
            +
                      convert(params)
         | 
| 63 | 
            +
                    end
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                #vars
         | 
| 69 | 
            +
                attr_accessor :id, :url, :convert_to, :created_at, :status,
         | 
| 70 | 
            +
                              :download_url, :notifications, :params
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                # instance
         | 
| 73 | 
            +
                def initialize(params={})
         | 
| 74 | 
            +
                  parse_json_params(params)
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                def success?
         | 
| 78 | 
            +
                  status == 'processed'
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                def failed?
         | 
| 82 | 
            +
                  status == 'failed'
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                def queued?
         | 
| 86 | 
            +
                  status == 'queued'
         | 
| 87 | 
            +
                end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                def reload
         | 
| 90 | 
            +
                  File.find(id) { |params| parse_json_params(params) }
         | 
| 91 | 
            +
                  self
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                def time_taken
         | 
| 95 | 
            +
                  notifications.last && notifications.last.time_taken
         | 
| 96 | 
            +
                end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            private
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                def parse_json_params(params)
         | 
| 101 | 
            +
                  @params = params
         | 
| 102 | 
            +
                  @id = params['id']
         | 
| 103 | 
            +
                  @url = params['url']
         | 
| 104 | 
            +
                  @convert_to = params['convert_to']
         | 
| 105 | 
            +
                  @created_at = DateTime.parse(params['created_at'])
         | 
| 106 | 
            +
                  @status = params['status']
         | 
| 107 | 
            +
                  @download_url = params['download_url']
         | 
| 108 | 
            +
                  @notifications = Array(params['notifications']).map { |p| Notification.new(p) }
         | 
| 109 | 
            +
                end
         | 
| 110 | 
            +
             | 
| 111 | 
            +
              end
         | 
| 112 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              class Notification < Resource
         | 
| 3 | 
            +
                attr_reader :id, :details, :parsed_time, :status, :time_taken,
         | 
| 4 | 
            +
                            :file_id, :params
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                def initialize(params)
         | 
| 7 | 
            +
                  @params = params
         | 
| 8 | 
            +
                  @id = params['id']
         | 
| 9 | 
            +
                  @details = params['details']
         | 
| 10 | 
            +
                  @parsed_time = DateTime.parse(params['parsed_time']) if params['parsed_time']
         | 
| 11 | 
            +
                  @status = params['status']
         | 
| 12 | 
            +
                  @time_taken = params['time_taken'].to_f if params['time_taken']
         | 
| 13 | 
            +
                  @file_id = params['doc_id']
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              class Upload < Resource
         | 
| 3 | 
            +
               
         | 
| 4 | 
            +
                # class
         | 
| 5 | 
            +
                class << self
         | 
| 6 | 
            +
                  
         | 
| 7 | 
            +
                  def find(id, &block)
         | 
| 8 | 
            +
                    conn.get("/uploads/#{id}.json", {}, 200) do |params|
         | 
| 9 | 
            +
                      block ? block.call(params) : Upload.new(params)
         | 
| 10 | 
            +
                    end
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  def all
         | 
| 14 | 
            +
                    conn.get("uploads.json", {}, 200) do |params|
         | 
| 15 | 
            +
                      uploads = Array(params['uploads']).map { |p| Upload.new(p) }
         | 
| 16 | 
            +
                      OpenStruct.new(
         | 
| 17 | 
            +
                        :uploads => uploads, 
         | 
| 18 | 
            +
                        :total_uploads => params['total_uploads'],
         | 
| 19 | 
            +
                        :current_page => params['current_page'],
         | 
| 20 | 
            +
                        :per_page => params['per_page'],
         | 
| 21 | 
            +
                        :total_pages => params['total_pages']
         | 
| 22 | 
            +
                      )
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                private
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def signed_upload_url(file)
         | 
| 29 | 
            +
                    file_type = file.class.extname(file.path).gsub('.', '')
         | 
| 30 | 
            +
                    file_type = 'unknown' if file_type == ''
         | 
| 31 | 
            +
                    file_size = file.class.size(file.path)
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                    conn.post('files/upload.json', { content_type: file_type, content_length: file_size }, 201) do |params|
         | 
| 34 | 
            +
                      params['file_type'] = file_type
         | 
| 35 | 
            +
                      params['file_size'] = file_size
         | 
| 36 | 
            +
                      OpenStruct.new(params)
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def evaluate_file_size(file)
         | 
| 41 | 
            +
                    max_size = Account.load_only_if_not_loaded.max_file_size_in_bytes
         | 
| 42 | 
            +
                    if file.size > max_size
         | 
| 43 | 
            +
                      OpenStruct.new(:errors => {"file_size"=>["is too big"]})
         | 
| 44 | 
            +
                    end
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                #vars
         | 
| 50 | 
            +
                attr_accessor :id, :url, :policy, :signature, :aws_access_key_id, :key,
         | 
| 51 | 
            +
                              :params
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                # instance
         | 
| 54 | 
            +
                def initialize(params={})
         | 
| 55 | 
            +
                  parse_json_params(params)
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                def reload
         | 
| 59 | 
            +
                  Upload.find(id) { |params| parse_json_params(params) }
         | 
| 60 | 
            +
                  self
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
            private
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                def parse_json_params(params)
         | 
| 66 | 
            +
                  @params = params
         | 
| 67 | 
            +
                  @id = params['id']
         | 
| 68 | 
            +
                  @url = params['url']
         | 
| 69 | 
            +
                  @policy = params['policy']
         | 
| 70 | 
            +
                  @signature = params['signature']
         | 
| 71 | 
            +
                  @aws_access_key_id = params['aws_access_key_id']
         | 
| 72 | 
            +
                  @key = params['key']
         | 
| 73 | 
            +
                  @url = params['url']
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
            end
         | 
    
        data/lib/fileturn.rb
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            module FileTurn
         | 
| 2 | 
            +
              extend self
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              attr_reader :api_key
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              def configure(params={})
         | 
| 7 | 
            +
                raise ArgumentError, "missing params (api_key)" if params[:api_key].nil?
         | 
| 8 | 
            +
                @api_key = params[:api_key]
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            require 'date'
         | 
| 13 | 
            +
            require "fileturn/exceptions"
         | 
| 14 | 
            +
            require "fileturn/http_client"
         | 
| 15 | 
            +
            require "fileturn/resources/resource"
         | 
| 16 | 
            +
            require "fileturn/resources/account"
         | 
| 17 | 
            +
            require "fileturn/resources/upload"
         | 
| 18 | 
            +
            require "fileturn/resources/notification"
         | 
| 19 | 
            +
            require "fileturn/resources/file"
         | 
    
        data/spec/.DS_Store
    ADDED
    
    | Binary file | 
    
        data/spec/data/.DS_Store
    ADDED
    
    | Binary file | 
| Binary file | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe FileTurn do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              describe '#configure' do
         | 
| 6 | 
            +
                it 'sets up api key' do
         | 
| 7 | 
            +
                  FileTurn.configure(:api_key => "123")
         | 
| 8 | 
            +
                  FileTurn.api_key.should == "123"
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                it 'raises exception if not api key is passed' do
         | 
| 12 | 
            +
                  expect { FileTurn.configure({}) }.to raise_error
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,47 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            http_interactions:
         | 
| 3 | 
            +
            - request:
         | 
| 4 | 
            +
                method: get
         | 
| 5 | 
            +
                uri: http://localhost:3000/api/users.json?token=67sFfjudMpXxJeioQcPSppYkoVdQD1oetqPWzMAh
         | 
| 6 | 
            +
                body:
         | 
| 7 | 
            +
                  encoding: US-ASCII
         | 
| 8 | 
            +
                  string: ''
         | 
| 9 | 
            +
                headers:
         | 
| 10 | 
            +
                  User-Agent:
         | 
| 11 | 
            +
                  - Faraday v0.8.7
         | 
| 12 | 
            +
                  Accept-Encoding:
         | 
| 13 | 
            +
                  - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
         | 
| 14 | 
            +
                  Accept:
         | 
| 15 | 
            +
                  - ! '*/*'
         | 
| 16 | 
            +
              response:
         | 
| 17 | 
            +
                status:
         | 
| 18 | 
            +
                  code: 200
         | 
| 19 | 
            +
                  message: ! 'OK '
         | 
| 20 | 
            +
                headers:
         | 
| 21 | 
            +
                  Content-Type:
         | 
| 22 | 
            +
                  - application/json; charset=utf-8
         | 
| 23 | 
            +
                  X-Ua-Compatible:
         | 
| 24 | 
            +
                  - IE=Edge
         | 
| 25 | 
            +
                  Etag:
         | 
| 26 | 
            +
                  - ! '"108135fed6135342e058ab4865b5301f"'
         | 
| 27 | 
            +
                  Cache-Control:
         | 
| 28 | 
            +
                  - max-age=0, private, must-revalidate
         | 
| 29 | 
            +
                  X-Request-Id:
         | 
| 30 | 
            +
                  - 3f36878be082fdb201b2015c688aa367
         | 
| 31 | 
            +
                  X-Runtime:
         | 
| 32 | 
            +
                  - '0.112629'
         | 
| 33 | 
            +
                  Server:
         | 
| 34 | 
            +
                  - WEBrick/1.3.1 (Ruby/1.9.3/2012-11-10)
         | 
| 35 | 
            +
                  Date:
         | 
| 36 | 
            +
                  - Sun, 07 Jul 2013 22:46:58 GMT
         | 
| 37 | 
            +
                  Content-Length:
         | 
| 38 | 
            +
                  - '177'
         | 
| 39 | 
            +
                  Connection:
         | 
| 40 | 
            +
                  - Keep-Alive
         | 
| 41 | 
            +
                body:
         | 
| 42 | 
            +
                  encoding: US-ASCII
         | 
| 43 | 
            +
                  string: ! '{"id":8,"created_at":"2013-07-05T22:14:10Z","credits":27836,"notification_url":"http://localhost:3000","time_zone":"Eastern
         | 
| 44 | 
            +
                    Time (US & Canada)","max_file_size_in_bytes":1048576}'
         | 
| 45 | 
            +
                http_version: 
         | 
| 46 | 
            +
              recorded_at: Sun, 07 Jul 2013 22:46:58 GMT
         | 
| 47 | 
            +
            recorded_with: VCR 2.4.0
         |