cloud_tempfile 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +18 -0
  2. data/.rvmrc +2 -0
  3. data/.travis.yml +30 -0
  4. data/Appraisals +11 -0
  5. data/CHANGELOG.md +9 -0
  6. data/Gemfile +15 -0
  7. data/LICENSE +20 -0
  8. data/README.md +232 -0
  9. data/Rakefile +43 -0
  10. data/cloud_tempfile.gemspec +38 -0
  11. data/gemfiles/rails_3.1.gemfile +10 -0
  12. data/gemfiles/rails_3.2.gemfile +10 -0
  13. data/gemfiles/rails_4.0.gemfile +10 -0
  14. data/lib/cloud_tempfile/cloud_tempfile.rb +106 -0
  15. data/lib/cloud_tempfile/config.rb +206 -0
  16. data/lib/cloud_tempfile/engine.rb +53 -0
  17. data/lib/cloud_tempfile/multi_mime.rb +22 -0
  18. data/lib/cloud_tempfile/railtie.rb +11 -0
  19. data/lib/cloud_tempfile/storage.rb +151 -0
  20. data/lib/cloud_tempfile/version.rb +9 -0
  21. data/lib/cloud_tempfile.rb +11 -0
  22. data/lib/ext/fog/aws/models/storage/file.rb +19 -0
  23. data/lib/generators/cloud_tempfile/install_generator.rb +71 -0
  24. data/lib/generators/cloud_tempfile/templates/cloud_tempfile.rb +41 -0
  25. data/lib/generators/cloud_tempfile/templates/cloud_tempfile.yml +52 -0
  26. data/lib/tasks/cloud_tempfile_tasks.rake +7 -0
  27. data/spec/dummy_app/Rakefile +27 -0
  28. data/spec/fixtures/assets/TEST.doc +0 -0
  29. data/spec/fixtures/assets/TEST.docx +0 -0
  30. data/spec/fixtures/assets/TEST.pdf +0 -0
  31. data/spec/fixtures/assets/TEST.rtf +1 -0
  32. data/spec/fixtures/aws_with_yml/config/cloud_tempfile.yml +22 -0
  33. data/spec/fixtures/google_with_yml/config/.gitkeep +0 -0
  34. data/spec/fixtures/rackspace_with_yml/config/.gitkeep +0 -0
  35. data/spec/fixtures/with_invalid_yml/config/cloud_tempfile.yml +22 -0
  36. data/spec/fixtures/without_yml/public/.gitkeep +0 -0
  37. data/spec/integration/.gitkeep +0 -0
  38. data/spec/spec_helper.rb +63 -0
  39. data/spec/unit/cloud_tempfile_spec.rb +208 -0
  40. data/spec/unit/multi_mime_spec.rb +48 -0
  41. data/spec/unit/railsless_spec.rb +50 -0
  42. data/spec/unit/storage_spec.rb +260 -0
  43. metadata +189 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ pkg/*
5
+ .DS_Store
6
+ Gemfile.lock
7
+ *.gem
8
+ *.swp
9
+ coverage
10
+ coverage/*
11
+ .env
12
+ spec/dummy_app/public/*
13
+ spec/dummy_app/log/*
14
+ spec/dummy_app/tmp/*
15
+ *tmp*
16
+ .rbx
17
+ gemfiles/*.lock
18
+ .idea
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm use 1.9.3@cloud_tempfile --default
data/.travis.yml ADDED
@@ -0,0 +1,30 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - ruby-head
7
+ gemfile:
8
+ - gemfiles/rails_3.1.gemfile
9
+ - gemfiles/rails_3.2.gemfile
10
+ before_install:
11
+ - gem install bundler
12
+ env:
13
+ global:
14
+ # - FOG_DIRECTORY=asset-sync-travis
15
+ # - FOG_PROVIDER=AWS
16
+ # - secure: "dy8Fqlg3b1ZMK1BY5z6NMQLbzAVd7GWVYY0MeCQALz76zRac0z8MyU8hkv6h\nozFry7DSdbGehGT9foOnkWTwzGzf1rzdd5cmWrUPk1wDTRgMM9SrwodPj1TU\nzsq2EFx0a79vADQN8JXkpLC1YD6kEb9aWkTxrIT9KBgw+J5H32o="
17
+ # - secure: "Hmx7D7/p2LlA2ya/xBIz21s/8MLIQCjvfYB7RWBNlWk1PfqRLAz8wX6TUVWy\nfAFktMjLnpRLRYO7AgQS4jcfQ/k0HYK9IXzqXzeI00TNm0Vwp0TCXhawiOFT\nSvUMhs2/1vRfjN0HOJ75XlWRhJzV/G5rOMiafAZLsVzN/0iiB8g="
18
+ matrix:
19
+ include:
20
+ - rvm: 1.9.3
21
+ gemfile: gemfiles/rails_4.0.gemfile
22
+ - rvm: jruby-19mode
23
+ gemfile: gemfiles/rails_4.0.gemfile
24
+ - rvm: 2.0.0
25
+ gemfile: gemfiles/rails_3.2.gemfile
26
+ - rvm: 2.0.0
27
+ gemfile: gemfiles/rails_4.0.gemfile
28
+ allow_failures:
29
+ - rvm: ruby-head
30
+ - rvm: jruby-head
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise "rails-3.1" do
2
+ gem "rails", "~> 3.1.0"
3
+ end
4
+
5
+ appraise "rails-3.2" do
6
+ gem "rails", "~> 3.2.0"
7
+ end
8
+
9
+ appraise "rails-4.0" do
10
+ gem "rails", "~> 4.0.0"
11
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # RELEASE HISTORY
2
+
3
+ ## v0.0.1 / 2014-02-16
4
+
5
+ Version 0.0.1 (Kevin Bolduc)
6
+
7
+ Changes:
8
+
9
+ * Initial commit
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in cloud_tempfile.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ gem 'rcov', :platforms => :mri_18, :group => [:development, :test]
14
+ gem 'simplecov', :platforms => [:jruby, :mri_19, :ruby_19, :mri_20, :rbx], :group => [:development, :test], :require => false
15
+ gem 'jruby-openssl', :platform => :jruby
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Kevin Bolduc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,232 @@
1
+ [![Build Status](https://secure.travis-ci.org/kbolduc/cloud_tempfile.png)](http://travis-ci.org/kbolduc/cloud_tempfile)
2
+
3
+ # CloudTempfile
4
+
5
+ Tempfile creation directly on Amazon S3 which can be utilized by a Ruby on Rails application.
6
+
7
+ This was initially built and is intended to work on [Heroku](http://heroku.com) but can work on any platform.
8
+
9
+ ## Installation
10
+
11
+ Add the gem to your Gemfile
12
+
13
+ ``` ruby
14
+ gem 'cloud_tempfile'
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ ### CloudTempfile
20
+
21
+ **CloudTempfile** supports the following methods of configuration.
22
+
23
+ * [Built-in Initializer](https://github.com/kbolduc/cloud_tempfile/blob/master/lib/cloud_tempfile/engine.rb) (configured through environment variables)
24
+ * Rails Initializer
25
+ * A YAML config file
26
+
27
+
28
+ Using the **Built-in Initializer** is the default method and is supposed to be used with **environment** variables. It's the recommended approach for deployments on Heroku.
29
+
30
+ If you need more control over configuration you will want to use a **custom rails initializer**.
31
+
32
+ Configuration using a **YAML** file (a common strategy for Capistrano deployments) is also supported.
33
+
34
+ The recommend way to configure **cloud_tempfile** is by using **environment variables** however it's up to you, it will work fine if you hard code them too. The main reason is that then your access keys are not checked into version control.
35
+
36
+ ### Built-in Initializer (Environment Variables)
37
+
38
+ The Built-in Initializer will configure **CloudTempfile** based on the contents of your environment variables.
39
+
40
+ Add your configuration details to **heroku**
41
+
42
+ ``` bash
43
+ heroku config:add AWS_ACCESS_KEY_ID=xxxx
44
+ heroku config:add AWS_SECRET_ACCESS_KEY=xxxx
45
+ heroku config:add FOG_DIRECTORY=xxxx
46
+ heroku config:add FOG_PROVIDER=AWS
47
+ # and optionally:
48
+ heroku config:add FOG_REGION=eu-west-1
49
+ ```
50
+
51
+ Or add to a traditional unix system
52
+
53
+ ``` bash
54
+ export AWS_ACCESS_KEY_ID=xxxx
55
+ export AWS_SECRET_ACCESS_KEY=xxxx
56
+ export FOG_DIRECTORY=xxxx
57
+ ```
58
+
59
+ Rackspace configuration is also supported
60
+
61
+ ``` bash
62
+ heroku config:add RACKSPACE_USERNAME=xxxx
63
+ heroku config:add RACKSPACE_API_KEY=xxxx
64
+ heroku config:add FOG_DIRECTORY=xxxx
65
+ heroku config:add FOG_PROVIDER=Rackspace
66
+ ```
67
+
68
+ Google Storage Cloud configuration is supported as well
69
+ ``` bash
70
+ heroku config:add FOG_PROVIDER=Google
71
+ heroku config:add GOOGLE_STORAGE_ACCESS_KEY_ID=xxxx
72
+ heroku config:add GOOGLE_STORAGE_SECRET_ACCESS_KEY=xxxx
73
+ heroku config:add FOG_DIRECTORY=xxxx
74
+ ```
75
+
76
+ ### Custom Rails Initializer (config/initializers/cloud_tempfile.rb)
77
+
78
+ If you want to enable some of the advanced configuration options you will want to create your own initializer.
79
+
80
+ Run the included Rake task to generate a starting point.
81
+
82
+ rails g cloud_tempfile:install --provider=Rackspace
83
+ rails g cloud_tempfile:install --provider=AWS
84
+
85
+ The generator will create a Rails initializer at `config/initializers/cloud_tempfile.rb`.
86
+
87
+ ``` ruby
88
+ CloudTempfile.configure do |config|
89
+ config.fog_provider = 'AWS'
90
+ config.fog_directory = ENV['FOG_DIRECTORY']
91
+ config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
92
+ config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
93
+
94
+ # Increase upload performance by configuring your region
95
+ # config.fog_region = 'eu-west-1'
96
+ #
97
+ # Fail silently. Useful for environments such as Heroku
98
+ # config.fail_silently = true
99
+ end
100
+ ```
101
+
102
+ ### YAML (config/cloud_tempfile.yml)
103
+
104
+ Run the included Rake task to generate a starting point.
105
+
106
+ rails g cloud_tempfile:install --use-yml --provider=Rackspace
107
+ rails g cloud_tempfile:install --use-yml --provider=AWS
108
+
109
+ The generator will create a YAML file at `config/cloud_tempfile.yml`.
110
+
111
+ ``` yaml
112
+ defaults: &defaults
113
+ fog_provider: "AWS"
114
+ fog_directory: "rails-app-assets"
115
+ aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
116
+ aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
117
+ # You may need to specify what region your storage bucket is in
118
+ # fog_region: "eu-west-1"
119
+ # Fail silently. Useful for environments such as Heroku
120
+ # fail_silently: true
121
+
122
+
123
+ development:
124
+ <<: *defaults
125
+
126
+ test:
127
+ <<: *defaults
128
+
129
+ production:
130
+ <<: *defaults
131
+ ```
132
+
133
+ ### Available Configuration Options
134
+
135
+ All CloudTempfile configuration can be modified directly using environment variables with the **Built-in initializer**. e.g.
136
+
137
+ ```ruby
138
+ CloudTempfile.config.fog_provider == ENV['FOG_PROVIDER']
139
+ ```
140
+
141
+ #### CloudTempfile (optional)
142
+
143
+ * **enabled**: (`true, false`) when false, will disable CloudTempfile and local Tempfile will be created instead. **default:** `'true'` (enabled)
144
+
145
+ #### Fog (Required)
146
+ * **fog\_provider**: your storage provider *AWS* (S3) or *Rackspace* (Cloud Files) or *Google* (Google Storage)
147
+ * **fog\_directory**: your bucket name
148
+
149
+ #### Fog (Optional)
150
+
151
+ * **fog\_region**: the region your storage bucket is in e.g. *eu-west-1*
152
+
153
+ #### AWS
154
+
155
+ * **aws\_access\_key\_id**: your Amazon S3 access key
156
+ * **aws\_secret\_access\_key**: your Amazon S3 access secret
157
+
158
+ #### Rackspace
159
+
160
+ * **rackspace\_username**: your Rackspace username
161
+ * **rackspace\_api\_key**: your Rackspace API Key.
162
+
163
+ #### Google Storage
164
+ * **google\_storage\_access\_key\_id**: your Google Storage access key
165
+ * **google\_storage\_secret\_access\_key**: your Google Storage access secret
166
+
167
+ #### Rackspace (Optional)
168
+
169
+ * **rackspace\_auth\_url**: Rackspace auth URL, for Rackspace London use: `https://lon.identity.api.rackspacecloud.com/v2.0`
170
+
171
+ ## Amazon S3 Multiple Region Support
172
+
173
+ If you are using anything other than the US buckets with S3 then you'll want to set the **region**. For example with an EU bucket you could set the following environment variable.
174
+
175
+ ``` bash
176
+ heroku config:add FOG_REGION=eu-west-1
177
+ ```
178
+
179
+ Or via a custom initializer
180
+
181
+ ``` ruby
182
+ CloudTempfile.configure do |config|
183
+ # ...
184
+ config.fog_region = 'eu-west-1'
185
+ end
186
+ ```
187
+
188
+ Or via YAML
189
+
190
+ ``` yaml
191
+ production:
192
+ # ...
193
+ fog_region: 'eu-west-1'
194
+ ```
195
+
196
+ ## Fail Silently
197
+
198
+ With the `fail_silently` option enabled, when running `rake assets:precompile` CloudTempfile will never throw an error due to missing configuration variables.
199
+
200
+ With the new **user_env_compile** feature of Heroku (see above), this is no longer required or recommended. Yet was added for the following reasons:
201
+
202
+ > With Rails 3.1 on the Heroku cedar stack, the deployment process automatically runs `rake assets:precompile`. If you are using **ENV** variable style configuration. Due to the methods with which Heroku compile slugs, there will be an error raised by asset\_sync as the environment is not available. This causes heroku to install the `rails31_enable_runtime_asset_compilation` plugin which is not necessary when using **cloud_tempfile** and also massively slows down the first incoming requests to your app.
203
+
204
+ > To prevent this part of the deploy from failing (cloud_tempfile raising a config error), but carry on as normal set `fail_silently` to true in your configuration and ensure to run `heroku run rake assets:precompile` after deploy.
205
+
206
+ ## Rake Task
207
+
208
+ A rake task is included within the **cloud_tempfile** gem to perform the clean up:
209
+
210
+ ``` ruby
211
+ namespace :cloud_tempfile do
212
+ desc "Synchronize assets to S3"
213
+ task :sync => :environment do
214
+ CloudTempfile.sync
215
+ end
216
+ end
217
+ ```
218
+
219
+ ## Todo
220
+
221
+ 1. Support more cloud storage providers
222
+ 3. Better test coverage
223
+
224
+ ## Credits
225
+
226
+ Inspired by:
227
+
228
+ - [https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem](https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem)
229
+
230
+ ## License
231
+
232
+ MIT License. Copyright 2014 Kevin Bolduc.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ require 'appraisal'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ require 'bundler/gem_tasks'
9
+ require 'rspec/core/rake_task'
10
+
11
+ namespace :spec do
12
+ RSpec::Core::RakeTask.new(:unit) do |spec|
13
+ spec.pattern = 'spec/unit/*_spec.rb'
14
+ spec.rspec_opts = ['--backtrace']
15
+
16
+ rbx = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
17
+ jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
18
+ if RUBY_VERSION == '1.8.7' && !(rbx || jruby)
19
+ spec.rcov = true
20
+ spec.rcov_opts = %w{--exclude gems\/,spec\/}
21
+ end
22
+ end
23
+ RSpec::Core::RakeTask.new(:integration) do |spec|
24
+ spec.pattern = 'spec/integration/*_spec.rb'
25
+ spec.rspec_opts = ['--backtrace']
26
+ end
27
+ desc "run spec:unit and spec:integration tasks"
28
+ task :all do
29
+ Rake::Task['spec:unit'].execute
30
+
31
+ # Travis CI does not expose encrypted ENV variables for pull requests, so
32
+ # do not run integration specs
33
+ # http://about.travis-ci.org/docs/user/build-configuration/#Set-environment-variables
34
+ #
35
+ pull_request = ENV['TRAVIS_PULL_REQUEST'] == 'true'
36
+ ci_build = ENV['TRAVIS'] == 'true'
37
+ if !ci_build || (ci_build && pull_request)
38
+ Rake::Task['spec:integration'].execute
39
+ end
40
+ end
41
+ end
42
+
43
+ task :default => 'spec:all'
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "cloud_tempfile/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "cloud_tempfile"
9
+ s.version = CloudTempfile::VERSION
10
+ s.date = "2014-02-16"
11
+ s.platform = Gem::Platform::RUBY
12
+ s.authors = ["Kevin Bolduc"]
13
+ s.email = ["kevin.bolduc@gmail.com"]
14
+ s.homepage = "https://github.com/kbolduc/CloudTempfile"
15
+ s.summary = %q{Tempfile creation directly on cloud storage (S3, Google, Rackspace etc] which can be utilized in a Ruby application}
16
+ s.description = %q{Save any CloudTempfile object and it will be directly persisted to the cloud!}
17
+ s.license = 'MIT'
18
+
19
+ # rubyforge
20
+ #s.rubyforge_project = "cloud_tempfile"
21
+
22
+ s.add_dependency('fog', ">= 1.8.0")
23
+ s.add_dependency('activemodel')
24
+
25
+ s.add_development_dependency "rspec"
26
+ s.add_development_dependency "bundler"
27
+ s.add_development_dependency "jeweler"
28
+
29
+ s.add_development_dependency "cloud_tempfile"
30
+ s.add_development_dependency "appraisal"
31
+
32
+ #s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
33
+ #s.test_files = Dir["{test,spec,features}/**/*"]
34
+ s.files = `git ls-files`.split("\n")
35
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
37
+ s.require_paths = ["lib"]
38
+ end
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rcov", :platforms=>:mri_18, :group=>[:development, :test]
6
+ gem "simplecov", :platforms=>[:jruby, :mri_19, :ruby_19, :mri_20, :rbx], :group=>[:development, :test], :require=>false
7
+ gem "jruby-openssl", :platform=>:jruby
8
+ gem "rails", "~> 3.1.0"
9
+
10
+ gemspec :path=>"../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rcov", :platforms=>:mri_18, :group=>[:development, :test]
6
+ gem "simplecov", :platforms=>[:jruby, :mri_19, :ruby_19, :mri_20, :rbx], :group=>[:development, :test], :require=>false
7
+ gem "jruby-openssl", :platform=>:jruby
8
+ gem "rails", "~> 3.2.0"
9
+
10
+ gemspec :path=>"../"
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rcov", :platforms=>:mri_18, :group=>[:development, :test]
6
+ gem "simplecov", :platforms=>[:jruby, :mri_19, :ruby_19, :mri_20, :rbx], :group=>[:development, :test], :require=>false
7
+ gem "jruby-openssl", :platform=>:jruby
8
+ gem "rails", "~> 4.0.0"
9
+
10
+ gemspec :path=>"../"
@@ -0,0 +1,106 @@
1
+ # cloud_tempfile/cloud_tempfile.rb
2
+ #
3
+ # Author:: Kevin Bolduc
4
+ # Date:: 14-02-24
5
+ # Time:: 3:23 PM
6
+
7
+ module CloudTempfile
8
+
9
+ class << self
10
+ attr_accessor :file_name
11
+ attr_accessor :file_raw_data
12
+
13
+ def initialize(file_name=nil, file_raw_data=nil)
14
+ self.file_name = file_name
15
+ self.file_raw_data = file_raw_data
16
+ end
17
+
18
+
19
+ def config=(data)
20
+ @config = data
21
+ end
22
+
23
+ def config
24
+ @config ||= Config.new
25
+ @config
26
+ end
27
+
28
+ def reset_config!
29
+ remove_instance_variable :@config if defined?(@config)
30
+ end
31
+
32
+ def configure(&proc)
33
+ @config ||= Config.new
34
+ yield @config
35
+ end
36
+
37
+ def storage
38
+ @storage ||= Storage.new(self.config)
39
+ end
40
+
41
+ # This action will responsible for persisting the Tempfile
42
+ # == Options
43
+ # [:+file_name+:] The file name to use for the Tempfile
44
+ # [:+file_raw_data+:] The raw file data to be persisted
45
+ #
46
+ # @param [Hash] options The Hash of options
47
+ # @return [File]
48
+ def write(options = {})
49
+ _file_name = ((options.has_key?(:file_name))? options[:file_name] : self.file_name)
50
+ _file_raw_data = ((options.has_key?(:file_raw_data))? options[:file_raw_data] : self.file_raw_data)
51
+
52
+ return false if _file_name.nil? || _file_name.blank? || _file_raw_data.nil?
53
+
54
+ with_config do
55
+ if !(config && config.local?)
56
+ return self.storage.upload_file(_file_name, _file_raw_data)
57
+ else
58
+ return self.storage.local_file(_file_name, _file_raw_data)
59
+ end
60
+ end
61
+ end
62
+
63
+ # Delete the remote file which are expired if the "config.clean_up" is true
64
+ # and "config.clean_up_older_than" is the amount of seconds it is older than.
65
+ #
66
+ # Note: This action should be used with the "bundle exec rake cloud_tempfile:clear" rake command (cronjob)
67
+ def clear
68
+ with_config do
69
+ self.storage.delete_expired_tempfiles
70
+ end
71
+ end
72
+
73
+ def with_config(&block)
74
+ return unless CloudTempfile.enabled?
75
+
76
+ errors = config.valid? ? "" : config.errors.full_messages.join(', ')
77
+
78
+ if !(config && config.valid?)
79
+ if config.fail_silently?
80
+ self.warn(errors)
81
+ else
82
+ raise Config::Invalid.new(errors)
83
+ end
84
+ else
85
+ block.call
86
+ end
87
+ end
88
+
89
+ def warn(msg)
90
+ stderr.puts msg
91
+ end
92
+
93
+ def log(msg)
94
+ stdout.puts msg if config.log_silently?
95
+ end
96
+
97
+ def enabled?
98
+ config.enabled?
99
+ end
100
+
101
+ # Log Stub methods
102
+ def stderr ; STDERR ; end
103
+ def stdout ; STDOUT ; end
104
+
105
+ end
106
+ end