Class: Kameleoon::Network::NetworkManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/network/network_manager.rb

Overview

NetworkManager is used to make API calls.

Constant Summary collapse

FETCH_CONFIGURATION_ATTEMPT_NUMBER =
3
TRACKING_CALL_ATTEMPT_NUMBER =
3
TRACKING_CALL_RETRY_DELAY =

in seconds

5.0
SDK_TYPE_HEADER =
'X-Kameleoon-SDK-Type'
SDK_VERSION_HEADER =
'X-Kameleoon-SDK-Version'
ACCESS_TOKEN_GRANT_TYPE =
'client_credentials'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, default_timeout, access_token_source_factory, url_provider) ⇒ NetworkManager

Returns a new instance of NetworkManager.



25
26
27
28
29
30
31
# File 'lib/kameleoon/network/network_manager.rb', line 25

def initialize(environment, default_timeout, access_token_source_factory, url_provider)
  @environment = environment
  @default_timeout = default_timeout
  @access_token_source = access_token_source_factory.create(self)
  @url_provider = url_provider
  @sync_net_provider = SyncNetProvider.new
end

Instance Attribute Details

#access_token_sourceObject (readonly)

Returns the value of attribute access_token_source.



23
24
25
# File 'lib/kameleoon/network/network_manager.rb', line 23

def access_token_source
  @access_token_source
end

#default_timeoutObject (readonly)

Returns the value of attribute default_timeout.



23
24
25
# File 'lib/kameleoon/network/network_manager.rb', line 23

def default_timeout
  @default_timeout
end

#environmentObject (readonly)

Returns the value of attribute environment.



23
24
25
# File 'lib/kameleoon/network/network_manager.rb', line 23

def environment
  @environment
end

#url_providerObject (readonly)

Returns the value of attribute url_provider.



23
24
25
# File 'lib/kameleoon/network/network_manager.rb', line 23

def url_provider
  @url_provider
end

Instance Method Details

#fetch_access_jwtoken(client_id, client_secret, timeout = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kameleoon/network/network_manager.rb', line 64

def fetch_access_jwtoken(client_id, client_secret, timeout = nil)
  url = @url_provider.make_access_token_url
  timeout = ensure_timeout(timeout)
  data_map = {
    grant_type: ACCESS_TOKEN_GRANT_TYPE,
    client_id: client_id,
    client_secret: client_secret
  }
  data = UriHelper.encode_query(data_map).encode('UTF-8')
  request = Request.new(Method::POST, url, ContentType::FORM, timeout, data: data)
  make_call(request, false)
end

#fetch_configuration(timestamp = nil, timeout = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/kameleoon/network/network_manager.rb', line 33

def fetch_configuration(timestamp = nil, timeout = nil)
  url = @url_provider.make_configuration_url(@environment, timestamp)
  timeout = ensure_timeout(timeout)
  sdk_headers = { SDK_TYPE_HEADER => SDK_NAME, SDK_VERSION_HEADER => SDK_VERSION }
  request = Request.new(Method::GET, url, ContentType::JSON, timeout, extra_headers: sdk_headers)
  make_call(request, false, FETCH_CONFIGURATION_ATTEMPT_NUMBER - 1)
end

#get_remote_data(key, timeout = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/kameleoon/network/network_manager.rb', line 41

def get_remote_data(key, timeout = nil)
  url = @url_provider.make_api_data_get_request_url(key)
  timeout = ensure_timeout(timeout)
  request = Request.new(Method::GET, url, ContentType::JSON, timeout)
  make_call(request, true)
end

#get_remote_visitor_data(visitor_code, filter, is_unique_identifier, timeout = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/kameleoon/network/network_manager.rb', line 48

def get_remote_visitor_data(visitor_code, filter, is_unique_identifier, timeout = nil)
  url = @url_provider.make_visitor_data_get_url(visitor_code, filter, is_unique_identifier)
  timeout = ensure_timeout(timeout)
  request = Request.new(Method::GET, url, ContentType::JSON, timeout)
  make_call(request, true)
end

#send_tracking_data(lines, timeout = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/kameleoon/network/network_manager.rb', line 55

def send_tracking_data(lines, timeout = nil)
  return if lines.nil? || lines.empty?

  url = @url_provider.make_tracking_url
  timeout = ensure_timeout(timeout)
  request = Request.new(Method::POST, url, ContentType::TEXT, timeout, data: lines)
  make_call(request, true, TRACKING_CALL_ATTEMPT_NUMBER - 1, TRACKING_CALL_RETRY_DELAY)
end