Class: DynamicService::ServiceCaller

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

Overview

Class to call services

Author:

  • Eleazar Gomez

Since:

  • 8/22/15

Version:

  • 1.0.0

Constant Summary collapse

GEM_ROOT =

Since:

  • 8/22/15

File.expand_path('../..', __FILE__)

Class Method Summary collapse

Class Method Details

.call_service(service_url, params = {}, method = 'post', headers = {}, destiny = nil) ⇒ Object

Parameters:

  • service_url

    this the url to invoke

  • params (defaults to: {})

    optional params that will be attached to the request

  • method (defaults to: 'post')

    http method that will be used

  • headers (defaults to: {})

    these are optional headers that will be attached to this request

  • destiny (defaults to: nil)

    this is a file that will be used to store a file from Dynamicloud servers

Since:

  • 8/22/15



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dynamic_service.rb', line 18

def self.call_service(service_url, params = {}, method = 'post', headers = {}, destiny = nil)
  http = HTTPClient.new
  http.connect_timeout = 10
  if service_url.start_with? 'https'
    http.ssl_config.set_trust_ca("#{GEM_ROOT}/lib/cacert.pem")
  end

  headers['User-Agent'] = 'Dynamicloud Client'
  headers['API-Version'] = Configuration::PROPERTIES.get_property :version
  headers['Dynamicloud-API'] = 'Ruby'
  headers['Accept-Encoding'] = 'deflate'

  # download file
  if destiny
    destiny.write(http.get_content(service_url, headers))
    return
  end

  if method.eql? 'post'
    return handle_response(http.post service_url, params, headers)
  end

  if method.eql? 'get'
    return handle_response(http.get service_url, params, headers)
  end

  if method.eql? 'delete'
    return handle_response(http.delete service_url, params, headers)
  else
    raise 'Unsupported Http Method - "' << method.to_s << '"'
  end
end