class SwiftStorage::Node

Attributes

name[RW]
parent[RW]

Public Class Methods

new(parent, name=nil) click to toggle source
# File lib/swift_storage/node.rb, line 7
def initialize(parent, name=nil)
  @parent = parent
  @name = name
end

Private Class Methods

header_attributes(*args) click to toggle source
# File lib/swift_storage/node.rb, line 108
def self.header_attributes(*args)
  args.each do |a|
    define_method(a.to_sym) do
      headers[a]
    end
  end
end
header_name() click to toggle source
# File lib/swift_storage/node.rb, line 103
def self.header_name
  self.name.split(':').last
end
parent_node(name) click to toggle source
# File lib/swift_storage/node.rb, line 116
def self.parent_node(name)
  define_method(name.to_sym) do
    parent
  end
end

Public Instance Methods

clear_cache() click to toggle source
# File lib/swift_storage/node.rb, line 79
def clear_cache
  @headers = nil
  @metadata = nil
end
delete() click to toggle source
# File lib/swift_storage/node.rb, line 91
def delete
  request(relative_path, :method => :delete)
end
delete_if_exists() click to toggle source
# File lib/swift_storage/node.rb, line 95
def delete_if_exists
  delete
rescue SwiftStorage::Errors::NotFoundError
  false
end
exists?() click to toggle source
# File lib/swift_storage/node.rb, line 84
def exists?
  request(relative_path, :method => :head)
  true
rescue SwiftStorage::Errors::NotFoundError
  false
end
get_lines(path, prefix: nil) click to toggle source
# File lib/swift_storage/node.rb, line 32
def get_lines(path, prefix: nil)
  headers = {'Accept' => 'text/plain'}
  response = request(path, :headers => headers, :params => {:prefix => prefix})
  body = response.body
  if body.nil? || body.empty?
    []
  else
    body.lines.map(&:strip)
  end
end
headers() click to toggle source
# File lib/swift_storage/node.rb, line 51
def headers
  return @headers if @headers

  response ||= request(relative_path, :method => :head)

  h = {}
  metadata = h[:metadata] = {}
  response.to_hash.each_pair do |k,v|
    if k =~ /^X-#{self.class.header_name}-Meta-?(.+)/i
      k = $1
      t = metadata
    elsif k =~ /^X-#{self.class.header_name}-?(.+)/i
      k = $1
      t = h
    else
      t = h
    end
    k = k.downcase.gsub(/\W/, '_')
    v = v.first if v.respond_to?(:to_ary)
    t[k] = v
  end
  @headers = struct(h)
end
metadata() click to toggle source
# File lib/swift_storage/node.rb, line 75
def metadata
  @metadata ||= struct(headers.metadata)
end
relative_path() click to toggle source
# File lib/swift_storage/node.rb, line 47
def relative_path
  raise NotImplemented
end
request(*args) click to toggle source
# File lib/swift_storage/node.rb, line 12
def request(*args)
  service.request(*args)
end
service() click to toggle source

Returns the service for this node

@return [SwiftStorage::Service]

The service this node is bound to
# File lib/swift_storage/node.rb, line 21
def service
  unless defined?(@service)
    p = parent
    while p && !(SwiftStorage::Service === p)
       p = p.parent
    end
    @service = p
  end
  @service
end
to_s() click to toggle source
# File lib/swift_storage/node.rb, line 43
def to_s
  "#<#{self.class.name} #{name}>"
end

Private Instance Methods

merge_metadata(headers, metadata) click to toggle source
# File lib/swift_storage/node.rb, line 122
def merge_metadata(headers, metadata)
  return if metadata.nil?
  metadata.each do |k,v|
    sanitized_key = k.to_s.gsub(/\W/, '-')
    sanitized_key = sanitized_key.split('-').reject{|o| o.nil? || o.empty?}
    full_key = ['X', self.class.header_name, 'Meta'] + sanitized_key
    full_key = full_key.map(&:capitalize).join('-')
    headers[full_key] = v.to_s
  end
end