Class: Defog::Proxy
- Inherits:
-
Object
- Object
- Defog::Proxy
- Defined in:
- lib/defog/proxy.rb
Instance Attribute Summary collapse
-
#fog_wrapper ⇒ Object
readonly
:nodoc:.
-
#max_cache_size ⇒ Object
readonly
Returns the value of attribute max_cache_size.
-
#persist ⇒ Object
readonly
Returns the value of attribute persist.
-
#proxy_root ⇒ Object
readonly
Returns the value of attribute proxy_root.
Instance Method Summary collapse
-
#close_proxy_file(handle) ⇒ Object
:nodoc:.
-
#file(key, mode = nil, opts = {}, &block) ⇒ Object
Proxy a remote cloud file.
-
#fog_connection ⇒ Object
Returns the underlying Fog::Storage object for the cloud connection.
-
#fog_directory ⇒ Object
Returns the Fog directory object for the root of the cloud files.
-
#initialize(opts = {}) ⇒ Proxy
constructor
Opens a
Fog
cloud storage connection to map to a corresponding proxy directory. -
#location ⇒ Object
Returns a ‘location’ handle to use in the default proxy root path, to disambiguate it from other proxies with the same provider.
-
#open_proxy_file(handle) ⇒ Object
:nodoc:.
-
#provider ⇒ Object
Returns the provider for this proxy.
Constructor Details
#initialize(opts = {}) ⇒ Proxy
Opens a Fog
cloud storage connection to map to a corresponding proxy directory. Use via, e.g.,
defog = Defog::Proxy.new(:provider => :AWS, :aws_access_key_id => access_key, ...)
The :provider
and its corresponding options must be specified as per Fog::Storage.new
. Currently, only :local
and :AWS
are supported. When using :AWS
, an additional option :bucket
must be specified; all files proxied by this instance must be in a single bucket.
By default, each proxy’s root directory is placed in a reasonable safe place, under Rails.root/tmp
if Rails is defined otherwise under Dir.tmpdir
. (More details: within that directory, the root directory is disambiguated by #provider and #location, so that multiple Defog::Proxy instances can be created without collision.)
The upshot is that if you have no special constraints you don’t need to worry about it. But if you do care, you can specify the option:
:proxy_root => "/root/for/this/proxy/files"
You can turn on persistence of local proxy files by specifying
:persist => true
The persistence behavior can be overriden on a per-file basis when opening a proxy (see Defog::Handle#open)
You can enable cache management by specifying a max cache size in bytes, e.g.
:max_cache_size => 3.gigabytes
See the README for discussion. [Number#gigabytes is defined in Rails’ ActiveSupport core extensions]
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/defog/proxy.rb', line 47 def initialize(opts={}) opts = opts.keyword_args(:provider => :required, :proxy_root => :optional, :persist => :optional, :max_cache_size => :optional, :OTHERS => :optional) @proxy_root = Pathname.new(opts.delete(:proxy_root)) if opts.proxy_root @persist = opts.delete(:persist) @max_cache_size = opts.delete(:max_cache_size) @open_proxy_paths = Set.new @fog_wrapper = FogWrapper.connect(opts) @proxy_root ||= case when defined?(Rails) then Rails.root + "tmp" else Pathname.new(Dir.tmpdir) end + "defog" + provider.to_s + location end |
Instance Attribute Details
#fog_wrapper ⇒ Object (readonly)
:nodoc:
12 13 14 |
# File 'lib/defog/proxy.rb', line 12 def fog_wrapper @fog_wrapper end |
#max_cache_size ⇒ Object (readonly)
Returns the value of attribute max_cache_size.
11 12 13 |
# File 'lib/defog/proxy.rb', line 11 def max_cache_size @max_cache_size end |
#persist ⇒ Object (readonly)
Returns the value of attribute persist.
10 11 12 |
# File 'lib/defog/proxy.rb', line 10 def persist @persist end |
#proxy_root ⇒ Object (readonly)
Returns the value of attribute proxy_root.
9 10 11 |
# File 'lib/defog/proxy.rb', line 9 def proxy_root @proxy_root end |
Instance Method Details
#close_proxy_file(handle) ⇒ Object
:nodoc:
119 120 121 |
# File 'lib/defog/proxy.rb', line 119 def close_proxy_file(handle) #:nodoc: @open_proxy_paths.delete handle.proxy_path end |
#file(key, mode = nil, opts = {}, &block) ⇒ Object
Proxy a remote cloud file. Returns or yields a Defog::Handle object that represents the file.
If a mode
is given, opens a proxy file via Defog::Handle#open (passing it the mode and other options and optional block), returning or yielding instead the Defog::File object.
Thus
proxy.file("key", mode, , &block)
is shorthand for
proxy.file("key").open(mode, , &block)
105 106 107 108 109 110 111 112 |
# File 'lib/defog/proxy.rb', line 105 def file(key, mode=nil, opts={}, &block) handle = Handle.new(self, key) case when mode then handle.open(mode, opts, &block) if mode when block then block.call(handle) else handle end end |
#fog_connection ⇒ Object
Returns the underlying Fog::Storage object for the cloud connection
83 84 85 |
# File 'lib/defog/proxy.rb', line 83 def fog_connection @fog_wrapper.fog_connection end |
#fog_directory ⇒ Object
Returns the Fog directory object for the root of the cloud files
88 89 90 |
# File 'lib/defog/proxy.rb', line 88 def fog_directory @fog_wrapper.fog_directory end |
#location ⇒ Object
Returns a ‘location’ handle to use in the default proxy root path, to disambiguate it from other proxies with the same provider. For :AWS it’s the bucket name, for :Local it’s derived from the local root path.
78 79 80 |
# File 'lib/defog/proxy.rb', line 78 def location @fog_wrapper.location end |
#open_proxy_file(handle) ⇒ Object
:nodoc:
114 115 116 117 |
# File 'lib/defog/proxy.rb', line 114 def open_proxy_file(handle) #:nodoc: manage_cache(handle) if max_cache_size @open_proxy_paths << handle.proxy_path end |
#provider ⇒ Object
Returns the provider for this proxy. I.e., :local
or :AWS
70 71 72 |
# File 'lib/defog/proxy.rb', line 70 def provider @fog_wrapper.provider end |