growing-ruby-sdk 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8653274457355c18253007bd74a1b61f2bcd50c471ac3ae40f66cf35d319d31
4
- data.tar.gz: 7e7ec59e08797388b2d0241c91a4e8d23bebe4988e491e0dee9f94123def2566
3
+ metadata.gz: a1f6f5c4d2e3aa4a78739c142b9aae2ec59438f5accf62f941fe8d5b81affc31
4
+ data.tar.gz: 4031d2430899e01ad0a3e8e6e223083573065f776673e92e19dfc404df441d0c
5
5
  SHA512:
6
- metadata.gz: 6d76917f01baac10afe2016f0af08bda0993355767067f0cde8cca258bc28bec873fbd28ad583e5817abe91befc780e313724674b83efeb96d8838aee454c194
7
- data.tar.gz: 69644310553e561a5af9a6cc261ef3d41da28c493a58590e6897ffc852af3c330cea6aa43fa45d3b8403d0f58ea9dcc8be484991ea99b4f2e34d7dadbcf1f3f8
6
+ metadata.gz: 4a020dff40ff3bd4e81d24b73ef5e16f4ebbaf38ce73145f583c095391d3307499b674c37cf6fcd4df5084e769a07e928e6675f46ed3abd952f7fe2a6a342b5d
7
+ data.tar.gz: 681a201723b93f3138f4179113389a780be9e6e5ddfbf2fd0cc60f7ad7eb3db3c72d93f3cdb7700c85ab71fa03af1c5bcfd4432dc91a983fa07e423fda19a483
@@ -1,19 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- growing-ruby-sdk (0.2.0)
4
+ growing-ruby-sdk (0.3.0)
5
+ activesupport
5
6
  faraday
6
7
  google-protobuf
8
+ timers
7
9
 
8
10
  GEM
9
11
  remote: https://rubygems.org/
10
12
  specs:
13
+ activesupport (6.0.3.2)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ zeitwerk (~> 2.2, >= 2.2.2)
11
19
  coderay (1.1.3)
20
+ concurrent-ruby (1.1.6)
12
21
  diff-lcs (1.4.4)
13
22
  faraday (1.0.1)
14
23
  multipart-post (>= 1.2, < 3)
15
- google-protobuf (3.12.2-universal-darwin)
24
+ google-protobuf (3.12.2)
25
+ i18n (1.8.3)
26
+ concurrent-ruby (~> 1.0)
16
27
  method_source (1.0.0)
28
+ minitest (5.14.1)
17
29
  multipart-post (2.1.1)
18
30
  pry (0.13.1)
19
31
  coderay (~> 1.1)
@@ -32,6 +44,11 @@ GEM
32
44
  diff-lcs (>= 1.2.0, < 2.0)
33
45
  rspec-support (~> 3.9.0)
34
46
  rspec-support (3.9.3)
47
+ thread_safe (0.3.6)
48
+ timers (4.3.0)
49
+ tzinfo (1.2.7)
50
+ thread_safe (~> 0.1)
51
+ zeitwerk (2.4.0)
35
52
 
36
53
  PLATFORMS
37
54
  ruby
@@ -29,6 +29,8 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_dependency "faraday"
31
31
  spec.add_dependency "google-protobuf"
32
+ spec.add_dependency "activesupport"
33
+ spec.add_dependency "timers"
32
34
  spec.add_development_dependency "rspec"
33
35
  spec.add_development_dependency 'pry'
34
36
  end
@@ -1,3 +1,4 @@
1
+ require 'active_support/time'
1
2
  require "growing/ruby/sdk/version"
2
3
  require "growing/ruby/sdk/client"
3
4
 
@@ -1,8 +1,11 @@
1
1
  require 'date'
2
+ require 'json'
2
3
  require 'faraday'
3
4
  require 'google/protobuf'
4
5
  require "growing/ruby/sdk/pb/v1/dto/event_pb"
5
6
  require "growing/ruby/sdk/pb/v1/dto/user_pb"
7
+ require 'active_support/core_ext/array/grouping'
8
+ require 'timers'
6
9
 
7
10
  ::Protocol = ::Io::Growing::Tunnel::Protocol
8
11
 
@@ -10,7 +13,7 @@ module Growing
10
13
  module Ruby
11
14
  module Sdk
12
15
  class Client
13
- attr_reader :account_id, :data_source_id, :api_host
16
+ attr_reader :account_id, :data_source_id, :api_host, :event_queue
14
17
 
15
18
  @instance_mutex = Mutex.new
16
19
 
@@ -20,6 +23,8 @@ module Growing
20
23
  @account_id = account_id
21
24
  @data_source_id = data_source_id
22
25
  @api_host = api_host
26
+ @event_queue = {}
27
+ @timers = Timers::Group.new
23
28
  end
24
29
 
25
30
  def self.instance(account_id, data_source_id, api_host)
@@ -38,7 +43,8 @@ module Growing
38
43
  gio_id: login_user_id,
39
44
  attributes: props,
40
45
  timestamp: current_timestamp)
41
- send("collect_user", ::Protocol::UserDto.encode_json(user))
46
+ @event_queue['collect_user'] ||= []
47
+ @event_queue['collect_user'] << user
42
48
  end
43
49
 
44
50
  def collect_cstm(login_user_id, event_key, props = {})
@@ -50,16 +56,41 @@ module Growing
50
56
  event_key: event_key,
51
57
  attributes: props,
52
58
  timestamp: current_timestamp)
53
- send("collect_cstm", ::Protocol::EventDto.encode_json(event))
59
+ @event_queue['collect_cstm'] ||= []
60
+ @event_queue['collect_cstm'] << event
54
61
  end
55
62
 
56
- private
57
- def send(action, data)
58
- resp = ::Faraday.post(url(action), "[#{data}]", "Content-Type" => "application/json")
59
- unless resp.success?
60
- raise Error
63
+ def send_data
64
+ @event_queue['collect_user'].to_a.in_groups_of(100) do |group|
65
+ user_list = ::Protocol::UserList.new
66
+ group.each do |user|
67
+ user_list['values'] << user
68
+ end
69
+ _send_data('collect_user', JSON.parse(::Protocol::UserList.encode_json(user_list))["values"])
70
+ end
71
+ @event_queue['collect_cstm'].to_a.in_groups_of(100) do |group|
72
+ event_list = ::Protocol::EventList.new
73
+ group.each do |event|
74
+ event_list['values'] << event
75
+ end
76
+ _send_data('collect_cstm', JSON.parse(::Protocol::EventList.encode_json(event_list))["values"])
77
+ end
78
+ @event_queue = {}
79
+ end
80
+
81
+ # thread unsafe
82
+ def auto_track
83
+ Thread.new do
84
+ @timers.every(60) { send_data }
85
+ loop { @timers.wait }
61
86
  end
62
- resp.success?
87
+ end
88
+
89
+ private
90
+ def _send_data(action, data)
91
+ Faraday.post(url(action), "#{data}", "Content-Type" => "application/json")
92
+ rescue
93
+ pp 'Error'
63
94
  end
64
95
 
65
96
  def url(action)
@@ -1,7 +1,7 @@
1
1
  module Growing
2
2
  module Ruby
3
3
  module Sdk
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growing-ruby-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ZhenyuanLau
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-18 00:00:00.000000000 Z
11
+ date: 2020-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rspec
43
71
  requirement: !ruby/object:Gem::Requirement