Class: Kvpbase

Inherits:
Object
  • Object
show all
Defined in:
lib/kvpbase.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_url, user_guid, api_key, enable_ssl, debug) ⇒ Kvpbase



16
17
18
19
20
21
22
# File 'lib/kvpbase.rb', line 16

def initialize(endpoint_url, user_guid, api_key, enable_ssl, debug)
  @endpoint_url = endpoint_url
  @user_guid = user_guid
  @api_key = api_key
  @enable_ssl = enable_ssl
  @debug = debug
end

Instance Method Details

#container_exists(container_path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kvpbase.rb', line 113

def container_exists(container_path)
  url = @endpoint_url + @user_guid + "/" + container_path + "?container=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Head.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "container_exists response " + response.code
  return response    
end

#create_container(container_path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/kvpbase.rb', line 95

def create_container(container_path)
  url = @endpoint_url + @user_guid + "/" + container_path + "?container=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Put.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "create_container response " + response.code + ": " + response.body
  return response      
end

#create_obj(filename, content_type, data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kvpbase.rb', line 39

def create_obj(filename, content_type, data)
  url = @endpoint_url + @user_guid + "/" + filename 
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key, "content-type" => content_type }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Put.new(uri.request_uri)
  request.body = data
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "create_obj response " + response.code + ": " + response.body
  return response    
end

#create_obj_without_name(container_path, content_type, data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kvpbase.rb', line 58

def create_obj_without_name(container_path, content_type, data)
  url = @endpoint_url + @user_guid + "/" + container_path 
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key, "content-type" => content_type }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = data
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "create_obj_without_name response " + response.code + ": " + response.body
  return response    
end

#del_container(container_path, recursive) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/kvpbase.rb', line 209

def del_container(container_path, recursive)
  url = @endpoint_url + @user_guid + "/" + container_path + "?container=true"
  if recursive
 url = url + "&recursive=true"
  end
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Delete.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "del_container response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end

#del_obj(filename) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/kvpbase.rb', line 189

def del_obj(filename)
  url = @endpoint_url + @user_guid + "/" + filename 
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Delete.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "del_obj response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end

#get_container(container_path) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/kvpbase.rb', line 169

def get_container(container_path)
  url = @endpoint_url + @user_guid + "/" + container_path + "?container=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "get_container response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end

#get_obj(filename) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kvpbase.rb', line 131

def get_obj(filename)
  url = @endpoint_url + @user_guid + "/" + filename
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "get_obj response " + response.code + ": " + response.body
  # JSON.parse(response.body)
  return response
end

#get_obj_metadata(filename) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kvpbase.rb', line 150

def (filename)
  url = @endpoint_url + @user_guid + "/" + filename + "?metadata=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "get_obj_metadata response " + response.code + ": " + response.body
  # JSON.parse(response.body)
  return response
end

#log(msg) ⇒ Object

internal log method adds conditional in front of puts



25
26
27
28
29
# File 'lib/kvpbase.rb', line 25

def log(msg)
  if @debug 
    puts msg
  end
end

#loopbackObject



31
32
33
34
35
36
37
# File 'lib/kvpbase.rb', line 31

def loopback
  url = @endpoint_url + "loopback"
  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)
  self.log "loopback response " + response.code + ": " + response.body
  return response.body
end

#move_container(from_container, move_from, to_container, move_to) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/kvpbase.rb', line 257

def move_container(from_container, move_from, to_container, move_to)
  url = @endpoint_url + @user_guid + "/move?container=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = { from_container: from_container, 
           move_from: move_from,
           to_container: to_container,
           move_to: move_to }.to_json

  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "move_container response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end

#move_obj(from_container, move_from, to_container, move_to) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/kvpbase.rb', line 232

def move_obj(from_container, move_from, to_container, move_to)
  url = @endpoint_url + @user_guid + "/move"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = { from_container: from_container, 
           move_from: move_from,
           to_container: to_container,
           move_to: move_to }.to_json

  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "move_obj response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end

#obj_exists(filename) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kvpbase.rb', line 77

def obj_exists(filename)
  url = @endpoint_url + @user_guid + "/" + filename 
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Head.new(uri.request_uri)
  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "obj_exists response " + response.code
  return response    
end

#rename_container(container_path, rename_from, rename_to) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/kvpbase.rb', line 306

def rename_container(container_path, rename_from, rename_to)
  url = @endpoint_url + @user_guid + "/rename?container=true"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = { container_path: container_path, 
           rename_from: rename_from,
           rename_to: rename_to }.to_json

  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "rename_container response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response    
end

#rename_obj(container_path, rename_from, rename_to) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/kvpbase.rb', line 282

def rename_obj(container_path, rename_from, rename_to)
  url = @endpoint_url + @user_guid + "/rename"
  uri = URI.parse(url)
  
  headers = { "x-api-key" => @api_key }
  http = Net::HTTP.new(uri.host, uri.port)
  
  if @enable_ssl
    http.use_ssl = true
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = { container_path: container_path, 
           rename_from: rename_from,
           rename_to: rename_to }.to_json

  request.initialize_http_header(headers)
  response = http.request(request)
  self.log "rename_obj response " + response.code + ": " + response.body
  
  # JSON.parse(response.body)
  return response
end