Class: Growing::Ruby::Sdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/growing/ruby/sdk/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id, data_source_id, api_host) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
# File 'lib/growing/ruby/sdk/client.rb', line 22

def initialize(, data_source_id, api_host)
  @account_id = 
  @data_source_id = data_source_id
  @api_host = api_host
  @event_queue = {}
  @timers = Timers::Group.new
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



16
17
18
# File 'lib/growing/ruby/sdk/client.rb', line 16

def 
  @account_id
end

#api_hostObject (readonly)

Returns the value of attribute api_host.



16
17
18
# File 'lib/growing/ruby/sdk/client.rb', line 16

def api_host
  @api_host
end

#data_source_idObject (readonly)

Returns the value of attribute data_source_id.



16
17
18
# File 'lib/growing/ruby/sdk/client.rb', line 16

def data_source_id
  @data_source_id
end

#event_queueObject (readonly)

Returns the value of attribute event_queue.



16
17
18
# File 'lib/growing/ruby/sdk/client.rb', line 16

def event_queue
  @event_queue
end

Class Method Details

.instance(account_id, data_source_id, api_host) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/growing/ruby/sdk/client.rb', line 30

def self.instance(, data_source_id, api_host)
  return @instance if @instance
  @instance_mutex.synchronize do
    @instance ||= new(, data_source_id, api_host)
  end
  @instance
end

Instance Method Details

#auto_trackObject

thread unsafe



82
83
84
85
86
87
# File 'lib/growing/ruby/sdk/client.rb', line 82

def auto_track
  Thread.new do
    @timers.every(60) { send_data }
    loop { @timers.wait }
  end
end

#collect_cstm(login_user_id, event_key, props = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/growing/ruby/sdk/client.rb', line 50

def collect_cstm(, event_key, props = {}) 
  event = ::Protocol::EventDto.new(
    project_key: @account_id,
    data_source_id: @data_source_id,
    user_id: ,
    gio_id: ,
    event_key: event_key,
    attributes: props,
    timestamp: current_timestamp)
  @event_queue['collect_cstm'] ||= [] 
  @event_queue['collect_cstm'] << event
end

#collect_user(login_user_id, props = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/growing/ruby/sdk/client.rb', line 38

def collect_user(, props = {})
  user = ::Protocol::UserDto.new(
    project_key: @account_id,
    data_source_id: @data_source_id,
    user_id: ,
    gio_id: ,
    attributes: props,
    timestamp: current_timestamp)
  @event_queue['collect_user'] ||= [] 
  @event_queue['collect_user'] << user
end

#send_dataObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/growing/ruby/sdk/client.rb', line 63

def send_data
  @event_queue['collect_user'].to_a.in_groups_of(100) do |group|
    user_list = ::Protocol::UserList.new
    group.each do |user|
      user_list['values'] << user
    end
    _send_data('collect_user', JSON.parse(::Protocol::UserList.encode_json(user_list))["values"])
  end
  @event_queue['collect_cstm'].to_a.in_groups_of(100) do |group|
    event_list = ::Protocol::EventList.new
    group.each do |event|
      event_list['values'] << event
    end
    _send_data('collect_cstm', JSON.parse(::Protocol::EventList.encode_json(event_list))["values"])
  end
  @event_queue = {}
end