class Stacktor::Swift::V1::Client

Public Instance Methods

create_object(opts) click to toggle source

Stores object in Swift container

@param opts [Hash] the options hash @option opts [String] :container_name - name of the container @option opts [String] :object_name - name of the new object @option opts [File, String] :content - contents of the object @option opts [String] :content_type - content type for the object @option opts [Hash] :metadata - metadata to store with the object

@return [Hash] Result object

# File lib/stacktor/swift/v1/client.rb, line 66
def create_object(opts)
  ctn = opts[:container_name]
  obn = opts[:object_name]
  if opts[:content].is_a?(String)
    body = StringIO.new(opts[:content])
  else
    body = opts[:content]
  end

  headers = {}
  if opts[:content_type].nil?
    headers['X-Detect-Content-Type'] = 'true'
  else
    headers['Content-Type'] = opts[:content_type]
  end
  if !opts[:metadata].nil?
    opts[:metadata].each do |k,v|
      headers["X-Object-Meta-#{k}"] = v
    end
  end
  resp = self.execute_request(
    path: "/#{ctn}/#{obn}",
    method: "PUT",
    headers: headers,
    data: body
  )
  parse_object(resp, {'name' => obn, 'container_name' => ctn})
  resp[:object].reload if resp[:object]
  return resp
end
delete_object(opts) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 97
def delete_object(opts)
  ctn = opts[:container_name]
  obn = opts[:object_name]

  resp = self.execute_request(
    path: "/#{ctn}/#{obn}",
    method: "DELETE"
  )
  parse_object(resp, {'name' => obn, 'container_name' => ctn})
  return resp
end
get_object_content(opts, &resp_fn) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 41
def get_object_content(opts, &resp_fn)
  ctn = opts[:container_name]
  obn = opts[:object_name]

  resp = self.execute_request(
    path: "/#{ctn}/#{obn}",
    method: "GET",
    response_handler: resp_fn
  )
  parse_object(resp, {'name' => obn, 'container_name' => ctn})
  return resp
end
get_object_metadata(opts) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 109
def get_object_metadata(opts)
  ctn = opts[:container_name]
  obn = opts[:object_name]

  resp = self.execute_request(
    path: "/#{ctn}/#{obn}",
    method: "HEAD"
  )
  parse_object(resp, {'name' => obn, 'container_name' => ctn})
  return resp
end
has_valid_token?() click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 129
def has_valid_token?
  @settings[:token] && @settings[:token].valid?
end
list_containers(opts={}) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 9
def list_containers(opts={})
  path = "/"
  opts[:format] = 'json'
  resp = self.execute_request(
    path: path,
    method: "GET",
    data: opts
  )
  parse_containers(resp)
  return resp
end
list_objects(opts) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 21
def list_objects(opts)
  ctn = opts[:container_name]
  data = {}
  data[:limit] = opts[:limit] || 1000
  data[:marker] = opts[:marker] if opts[:marker]
  data[:end_marker] = opts[:end_marker] if opts[:end_marker]
  data[:prefix] = opts[:prefix] if opts[:prefix]
  data[:format] = 'json'
  data[:delimiter] = opts[:delimiter] if opts[:delimiter]
  data[:path] = opts[:path] if opts[:path]
  result = self.execute_request(
    path: "/#{ctn}",
    method: "GET",
    data: data
  )
  parse_objects(result, {'container_name' => ctn})
  return result
end
token() click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 126
def token
  @settings[:token]
end
token=(val) click to toggle source

HELPERS

# File lib/stacktor/swift/v1/client.rb, line 123
def token=(val)
  @settings[:token] = val
end

Private Instance Methods

build_headers() click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 156
def build_headers
  {
    'X-Auth-Token' => self.settings[:token].id
  }
end
parse_containers(resp) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 135
def parse_containers(resp)
  if resp[:success]
    resp[:containers] = JSON.parse(resp[:body]).collect {|c| Container.new(c, self)}
  end
end
parse_object(resp, data={}) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 141
def parse_object(resp, data={})
  r = resp[:response]
  if resp[:success]
    resp[:object] = ContainerObject.new(data, self, headers: r)
  end
end
parse_objects(resp, data={}) click to toggle source
# File lib/stacktor/swift/v1/client.rb, line 148
def parse_objects(resp, data={})
  if resp[:success]
    resp[:objects] = JSON.parse(resp[:body]).collect{|obj_data|
      ContainerObject.new(obj_data, self)
    }
  end
end