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

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



16
17
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
# File 'lib/dynamic_service.rb', line 16

def self.call_service(service_url, params = {}, method = 'post', headers = {}, destiny = nil)
  http = HTTPClient.new
  http.connect_timeout = 10
  http.ssl_config.ssl_version = :TLSv1

  headers['User-Agent'] = 'Dynamicloud client'
  headers['API_Version'] = Configuration::PROPERTIES.get_property :version
  headers['Dynamicloud_API'] = 'Ruby'
  headers['Accept-Encoding'] = 'gzip, 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