Class: TddiumClient::InternalClient

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

Direct Known Subclasses

Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = nil, scheme = 'https', version = 1, caller_version = nil, options = {}) ⇒ InternalClient

Returns a new instance of InternalClient.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tddium_client.rb', line 109

def initialize(host, port=nil, scheme='https', version=1, caller_version=nil, options={})
  @client = HTTPClient.new
  if options[:insecure]
    @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  @tddium_config = {"host" => host,
                    "port" => port,
                    "scheme" => scheme,
                    "version" => version,
                    "caller_version" => caller_version}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



107
108
109
# File 'lib/tddium_client.rb', line 107

def client
  @client
end

Instance Method Details

#call_api(method, api_path, params = {}, api_key = nil, retries = 5, xid = nil) ⇒ Object

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tddium_client.rb', line 122

def call_api(method, api_path, params = {}, api_key = nil, retries = 5, xid=nil)
  headers = {'Content-Type' => 'application/json'}
  headers.merge!(API_KEY_HEADER => api_key) if api_key
  headers.merge!(CLIENT_VERSION_HEADER => version_header)

  xid ||= xid_gen
  call_params = params.merge({:xid => xid})

  tries = 0

  begin
    http = @client.send(method, tddium_uri(api_path), :body => call_params.to_json, :header => headers)
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Timeout::Error, OpenSSL::SSL::SSLError, OpenSSL::SSL::Session::SessionError, HTTPClient::TimeoutError, HTTPClient::BadResponseError
    tries += 1
    delay = (tries>>1)*0.05*rand()
    Kernel.sleep(delay)
    retry if retries > 0 && tries <= retries
  end

  raise Error::Timeout if retries >= 0 && tries > retries

  Result::API.new(http)
end

#caller_versionObject



146
147
148
# File 'lib/tddium_client.rb', line 146

def caller_version
  @tddium_config["caller_version"]
end

#caller_version=(version) ⇒ Object



150
151
152
# File 'lib/tddium_client.rb', line 150

def caller_version=(version)
  @tddium_config["caller_version"] = version
end

#xid_genObject



154
155
156
# File 'lib/tddium_client.rb', line 154

def xid_gen
  return SecureRandom.hex(8)
end