Module: Synaptic4r::StorageRest::InstanceMethods

Includes:
Utils
Defined in:
lib/synaptic4r/rest.rb

Overview


Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#create_timestamp, #read_file, #symbolize, #unary_args_given?

Instance Attribute Details

#headersObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def headers
  @headers
end

#keyObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def key
  @key
end

#methObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def meth
  @meth
end

#payloadObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def payload
  @payload
end

#signObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def sign
  @sign
end

#siteObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def site
  @site
end

#subtenantObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def subtenant
  @subtenant
end

#uidObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def uid
  @uid
end

#urlObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….……



203
204
205
# File 'lib/synaptic4r/rest.rb', line 203

def url
  @url
end

Instance Method Details

#add_header_attr(given = {}) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



254
255
256
257
258
259
260
# File 'lib/synaptic4r/rest.rb', line 254

def add_header_attr(given={})
  to_emc = lambda{|a| h = a.to_s.gsub(/_/,'-'); self.class.emc_headers.include?(a) ? "x-emc-#{h}" : h}
  header_args = self.class.header_args(meth)
  given.keys.each do |k| 
    headers.update(to_emc[k] => given[k]) if header_args.include?(k)
  end
end

#add_payload(args, ext) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



344
345
346
347
348
349
350
351
352
# File 'lib/synaptic4r/rest.rb', line 344

def add_payload(args, ext)
  if args[:file]
    @payload = read_file(args[:file], ext[:offset], ext[:length])
    if @payload
      headers['content-length'] = ext[:length]
      headers['content-md5'] = Base64.encode64(Digest::MD5.digest(payload)).chomp()
    end
  end
end

#build_service_url(args = {}) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



276
277
278
279
280
281
282
283
284
# File 'lib/synaptic4r/rest.rb', line 276

def build_service_url(args={})
  surl = if args[:rpath]
           'namespace/' + args[:rpath]
         else
           'objects' + (args[:oid].nil? ? '' : "/#{args[:oid]}")
         end
  @url = (/\/$/.match(site).nil? ? site + '/' : site) + surl + 
    ((q = self.class.query(meth)).nil? ? '' : "?#{q}")
end

#canonicalize_custom_headersObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



307
308
309
310
311
312
313
# File 'lib/synaptic4r/rest.rb', line 307

def canonicalize_custom_headers
  headers.select do |key, value| 
    /^x-emc-/.match(key)
  end.sort.inject("") do |h, (k, v)|
    h += "#{k}:#{v}\n"
  end.chomp
end

#create_sign_stringObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/synaptic4r/rest.rb', line 294

def create_sign_string
  composite_string = (self.class.http_method(meth).to_s.upcase  || '') + "\n" + \
                     (headers['content-type']                   || '') + "\n" + \
                     (headers['range']                          || '') + "\n" + \
                     (headers['date']                           || '') + "\n"
  if url
    composite_string += URI.parse(url).path.downcase
    composite_string += "?" + URI.parse(url).query.downcase unless URI.parse(url).query.nil?
  end
  composite_string += "\n" +  canonicalize_custom_headers
end

#create_signatureObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



287
288
289
290
291
# File 'lib/synaptic4r/rest.rb', line 287

def create_signature
  @sign = create_sign_string
  digest = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, Base64.decode64(key), sign)
  headers['x-emc-signature'] = Base64.encode64(digest.to_s()).chomp()
end

#credentialsObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



355
356
357
358
359
360
361
362
# File 'lib/synaptic4r/rest.rb', line 355

def credentials
  curr_time = create_timestamp
  {'x-emc-uid'    => subtenant + '/' + uid, 
   'date'         => curr_time, 
   'x-emc-date'   => curr_time, 
   'content-type' => 'application/octet-stream',
   'accept'       => '*/*'}
end

#exclusive_args_given?(exclusive_args, args) ⇒ Boolean

.….….….….….….….….….….….….….….….….….….….….….….….….……

Returns:

  • (Boolean)


244
245
246
247
248
249
250
251
# File 'lib/synaptic4r/rest.rb', line 244

def exclusive_args_given?(exclusive_args, args)
  exclusive_args.each do |alist| 
    unless alist.select{|a| args.keys.include?(a)}.length.eql?(1)
      acli = alist.map{|a| [self.class.rest_arg(a)[:cli]].flatten.first}
      raise ArgumentError, "One of '#{acli.join(', ')}' is required as an argument."
    end
  end
end

#execute(meth, *args, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/synaptic4r/rest.rb', line 216

def execute(meth, *args, &blk)
  @meth = meth
  args = args.first || {}
  set_remote_file(args)
  unary_args_given?(self.class.unary_rest_args(meth), args)
  exclusive_args_given?(self.class.exclusive_rest_args(meth), args)
  self.class.exe(meth)[self, args] if self.class.exe(meth)
  build_service_url(args)
  add_header_attr(args)
  create_signature
  self.class.result_class(meth).new(:result => http_request(args), :headers => headers, :url => url, :sign => sign,
                                    :http_request => self.class.http_method(meth), 
                                    :payload => args[:payload] ? payload : nil)
end

#extent(file, begin_offset, end_offset) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/synaptic4r/rest.rb', line 321

def extent(file, begin_offset, end_offset)
  if file
    file_offset = case file
                    when String then File.size(file)
                    when File then file.stat.size
                    else 
                      file.size
                  end - 1
    offset =  begin_offset.to_i
    end_offset = end_offset.nil? ? file_offset : end_offset.to_i 
    end_offset = file_offset if end_offset > file_offset
    length = end_offset - offset + 1
    {:offset => offset, :length => length}
  end
end

#http_request(args) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



232
233
234
235
236
237
238
239
240
241
# File 'lib/synaptic4r/rest.rb', line 232

def http_request(args)
  if self.respond_to?(meth)
    self.send(meth, args)
  else
    if args[:dump].nil? and args[:payload].nil?                
      RestClient::Request.execute(:method => self.class.http_method(meth), :url => url, 
                                  :headers => headers, :payload => payload)
    end
  end
end

#initialize(cred) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



206
207
208
209
210
211
212
213
# File 'lib/synaptic4r/rest.rb', line 206

def initialize(cred)
  unary_args_given?([:uid, :subtenant, :key, :site], cred)
  @uid = cred[:uid]
  @key = cred[:key]
  @site = cred[:site]
  @subtenant = cred[:subtenant]
  @headers = credentials
end

#set_header_extent(args, ext) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



338
339
340
341
# File 'lib/synaptic4r/rest.rb', line 338

def set_header_extent(args, ext)
  args[:beginoffset] = ext[:offset]
  args[:endoffset] = ext[:offset] + ext[:length] - 1 
end

#set_header_range(args, ext) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



316
317
318
# File 'lib/synaptic4r/rest.rb', line 316

def set_header_range(args, ext)
  headers['range'] = "bytes=#{ext[:offset]}-#{ext[:offset]+ext[:length]-1}"
end

#set_remote_file(args) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/synaptic4r/rest.rb', line 263

def set_remote_file(args)
  if args[:rpath]
    if args[:namespace] and args[:namespace].empty?
      args[:rpath] =  args[:rpath]
    elsif args[:namespace]
      args[:rpath] = args[:namespace] + '/' + args[:rpath]
    else
      args[:rpath] = uid + '/' + args[:rpath]
    end
  end
end