class Dragonfly::FogDataStore

Constants

REGIONS
VERSION

Attributes

api_key[RW]
container[RW]
region[RW]
storage_headers[RW]
url_host[RW]
url_scheme[RW]
username[RW]

Public Class Methods

new(opts={}) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 14
def initialize(opts={})
  @container = opts[:container]
  @username = opts[:username]
  @api_key = opts[:api_key]
  @region = opts[:region]
  @storage_headers = opts[:storage_headers] || {}

  @url_scheme = opts[:url_scheme] || 'http'
  @url_host = opts[:url_host]
end

Public Instance Methods

container_exists?() click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 76
def container_exists?
  rescuing_socket_errors{ storage.get_container(container) }
  true
rescue Fog::Storage::Rackspace::NotFound
  nil
rescue Excon::Errors::NotFound => e
  false
end
destroy(uid) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 56
def destroy(uid)
  rescuing_socket_errors{ storage.delete_object(container, full_path(uid)) }
rescue Fog::Storage::Rackspace::NotFound
  nil
rescue Excon::Errors::NotFound, Excon::Errors::Conflict => e
  Dragonfly.warn("#{self.class.name} destroy error: #{e}")
end
read(uid) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 45
def read(uid)
  ensure_configured

  response = rescuing_socket_errors{ storage.get_object(container, full_path(uid)) }
  [response.body, headers_to_meta(response.headers)]
rescue Fog::Storage::Rackspace::NotFound
  nil
rescue Excon::Errors::NotFound => e
  nil
end
storage() click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 64
def storage
  @storage ||= begin
    storage = Fog::Storage.new({
      provider: 'Rackspace',
      rackspace_username: username,
      rackspace_api_key: api_key,
      rackspace_region: region
    })
    storage
  end
end
write(content, opts={}) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 28
def write(content, opts={})
  ensure_configured
  ensure_container_initialized

  headers = {'Content-Type' => content.mime_type}
  headers.merge!(opts[:headers]) if opts[:headers]
  uid = opts[:path] || generate_uid(content.name || 'file')

  rescuing_socket_errors do
    content.file do |f|
      storage.put_object(container, full_path(uid), f, full_storage_headers(headers, content.meta))
    end
  end

  uid
end

Private Instance Methods

ensure_configured() click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 87
def ensure_configured
  unless @configured
    [:container, :username, :api_key, :container].each do |attr|
      raise NotConfigured, "You need to configure #{self.class.name} with #{attr}" if send(attr).nil?
    end
    @configured = true
  end
end
ensure_container_initialized() click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 96
def ensure_container_initialized
  unless @container_initialized
    rescuing_socket_errors{ storage.put_container(container) } unless container_exists?
    @container_initialized = true
  end
end
full_path(uid) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 113
def full_path(uid)
  File.join *[uid].compact
end
full_storage_headers(headers, meta) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 117
def full_storage_headers(headers, meta)
  storage_headers.merge(meta_to_headers(meta)).merge(headers)
end
generate_uid(name) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 109
def generate_uid(name)
  "#{Time.now.strftime '%Y/%m/%d/%H/%M/%S'}/#{rand(1000)}/#{name.gsub(/[^\w.]+/, '_')}"
end
get_region() click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 103
def get_region
  reg = region || :ord
  raise "Invalid region #{reg} - should be one of #{REGIONS.join(', ')}" unless REGIONS.include?(reg)
  reg
end
headers_to_meta(headers) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 121
def headers_to_meta(headers)
  json = headers['X-Object-Meta']
  if json && !json.empty?
    Serializer.json_decode(json)
  end
end
meta_to_headers(meta) click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 128
def meta_to_headers(meta)
  {'X-Object-Meta' => Serializer.json_encode(meta)}
end
rescuing_socket_errors() { || ... } click to toggle source
# File lib/dragonfly/fog_data_store.rb, line 132
def rescuing_socket_errors(&block)
  yield
rescue Excon::Errors::SocketError => e
  storage.reload
  yield
end