Class: Stacktor::Swift::V1::Client

Inherits:
Core::Client show all
Defined in:
lib/stacktor/swift/v1/client.rb

Instance Method Summary collapse

Methods inherited from Core::Client

#before_request, #execute_request, #initialize, #settings, #url, #url=

Constructor Details

This class inherits a constructor from Stacktor::Core::Client

Instance Method Details

#create_object(opts) ⇒ Hash

Stores object in Swift container

Parameters:

  • the options hash

Options Hash (opts):

  • :container_name (String)
    • name of the container

  • :object_name (String)
    • name of the new object

  • :content (File, String)
    • contents of the object

  • :content_type (String)
    • content type for the object

  • :metadata (Hash)
    • metadata to store with the object

Returns:

  • Result object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stacktor/swift/v1/client.rb', line 109

def (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?Boolean

Returns:



129
130
131
# File 'lib/stacktor/swift/v1/client.rb', line 129

def has_valid_token?
  @settings[:token] && @settings[:token].valid?
end

#list_containers(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

#tokenObject



126
127
128
# File 'lib/stacktor/swift/v1/client.rb', line 126

def token
  @settings[:token]
end

#token=(val) ⇒ Object

HELPERS



123
124
125
# File 'lib/stacktor/swift/v1/client.rb', line 123

def token=(val)
  @settings[:token] = val
end