Class: Distelli::RequestInfo

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

Constant Summary collapse

VALID_CONTENT_TYPES =
Set.new [ServiceConstants::CONTENT_TYPE_JSON, ServiceConstants::CONTENT_TYPE_XML, ServiceConstants::CONTENT_TYPE_OCTET_STREAM]
VALID_HTTP_METHODS =
Set.new [ServiceConstants::HTTP_METHOD_GET, ServiceConstants::HTTP_METHOD_PUT, ServiceConstants::HTTP_METHOD_POST, ServiceConstants::HTTP_METHOD_DELETE]
VALID_RESPONSE_TYPES =
Set.new [ServiceConstants::RESPONSE_TYPE_XML, ServiceConstants::RESPONSE_TYPE_JSON]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequestInfo

Returns a new instance of RequestInfo.



296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/distelli/clientframework.rb', line 296

def initialize()
  @query_params = Hash.new
  @headers = Hash.new
  @resource_uri = "/"
  @operation = nil
  @request_id = nil
  @content_type = ServiceConstants::CONTENT_TYPE_JSON
  @http_method = "GET"
  @request = nil
  @request_bytes = nil
  @response_type = ServiceConstants::RESPONSE_TYPE_JSON
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def headers
  @headers
end

#operationObject

Returns the value of attribute operation.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def operation
  @operation
end

#query_paramsObject

Returns the value of attribute query_params.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def query_params
  @query_params
end

#requestObject

Returns the value of attribute request.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def request
  @request
end

#request_bytesObject

Returns the value of attribute request_bytes.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def request_bytes
  @request_bytes
end

#request_idObject

Returns the value of attribute request_id.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def request_id
  @request_id
end

#resource_uriObject

Returns the value of attribute resource_uri.



292
293
294
# File 'lib/distelli/clientframework.rb', line 292

def resource_uri
  @resource_uri
end

Instance Method Details

#add_header(header, value) ⇒ Object



376
377
378
379
380
381
# File 'lib/distelli/clientframework.rb', line 376

def add_header(header, value)
  if header == nil or value == nil
    return
  end
  @headers[header] = value
end

#add_query_param(key, value) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/distelli/clientframework.rb', line 397

def add_query_param(key, value)
  if value == nil
    return
  end
  param_values = nil
  if @query_params.has_key?(key)
    param_values = @query_params[key]
  else
    param_values = Array.new
    @query_params[key] = param_values
  end

  if value.instance_of?(DateTime)
    #TODO: Check if the timezone is present. if not then set it to UTC
    param_values.push(value.strftime("%Y-%m-%dT%H:%M:%S%z"))
  else
    param_values.push(value.to_s)
  end
end

#contains_header(header) ⇒ Object



372
373
374
# File 'lib/distelli/clientframework.rb', line 372

def contains_header(header)
  return @headers.has_key?(header)
end

#content_typeObject



339
340
341
# File 'lib/distelli/clientframework.rb', line 339

def content_type
  @content_type
end

#content_type=(content_type) ⇒ Object



343
344
345
346
347
348
# File 'lib/distelli/clientframework.rb', line 343

def content_type=(content_type)
  if !VALID_CONTENT_TYPES.include?(content_type)
    raise ClientException.new("Invalid content type: "+content_type)
  end
  @content_type = content_type
end

#get_uri(endpoint) ⇒ Object

Returns tho fully qualified uri by concatenating the endpoint and the resource_uri and adding the query params



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/distelli/clientframework.rb', line 311

def get_uri(endpoint)
  uriparse_result = URI.parse(endpoint)
  uri_str = endpoint
  resource_uri = uriparse_result.path
  if resource_uri == nil or resource_uri.length() == 0
    resource_uri = "/"
    uri_str = endpoint+resource_uri
  end

  @resource_uri = resource_uri
  # now build the query string
  query_param_list = Array.new
  @query_params.each_pair do |k,v|
    v.each do |x|
      query_param_list.push(k+'='+x)
    end
  end
  return uri_str+'?'+query_param_list.join('&')
end

#http_methodObject



361
362
363
# File 'lib/distelli/clientframework.rb', line 361

def http_method
  @http_method
end

#http_method=(http_method) ⇒ Object



365
366
367
368
369
370
# File 'lib/distelli/clientframework.rb', line 365

def http_method=(http_method)
  if !VALID_HTTP_METHODS.include?(http_method)
    raise ClientException.new("Invalid http method: "+http_method)
  end
  @http_method = http_method
end

#remove_header(header) ⇒ Object



383
384
385
386
387
388
# File 'lib/distelli/clientframework.rb', line 383

def remove_header(header)
  if header == nil
    return
  end
  @headers.delete(header)
end

#remove_query_params(key) ⇒ Object



390
391
392
393
394
395
# File 'lib/distelli/clientframework.rb', line 390

def remove_query_params(key)
  if not @query_params.has_key?(key)
    return
  end
  @query_params.delete(key)
end

#response_typeObject



350
351
352
# File 'lib/distelli/clientframework.rb', line 350

def response_type
  @response_type
end

#response_type=(response_type) ⇒ Object



354
355
356
357
358
359
# File 'lib/distelli/clientframework.rb', line 354

def response_type=(response_type)
  if !VALID_RESPONSE_TYPES.include?(response_type)
    raise ClientException.new("Invalid response type: "+response_type)
  end
  @response_type = response_type
end

#to_sObject

Lets implement the to_s method of the RequestInfo



332
333
334
335
336
337
# File 'lib/distelli/clientframework.rb', line 332

def to_s
  vars = instance_variables.map do |n|
    "#{n}=#{instance_variable_get(n).inspect}"
  end
  "{%s}" % [vars.join(', ')]
end