amplitude-rb 0.1.1 → 0.1.2

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: 3b8e867a1ec802a05698e732705abfbadb07a5ab7b34f631b8c654347d79299b
4
- data.tar.gz: bf49743c98e8d86d3195ff5b4ed7782694278f44152d94610be622bea023a61e
3
+ metadata.gz: dab0df7986383948be579ce9a8f659e39176b2fc60f8ad319adad9c017f9e2d8
4
+ data.tar.gz: 7f06bbfdae98fafeb6aa3e09a4dd0a1fbafc491ccc3a7713a17ec296575f3cd9
5
5
  SHA512:
6
- metadata.gz: 3a920cac5fdb886715ece75318afe538bdd33fa923bb8064b4bb5a6b317dad10fbe0335b8a5e09d8bc0c767d54e6f2f6e93b7d8d8f81165702bae8b425744a6f
7
- data.tar.gz: b9f8ea0a55eebc01ed7345f16f1cf757c1c2de52bd8c86e451ee51a40a15808fac9e6f36b4270225d36ea9ecdd2949945996919e553c78109a9d7103abc6fda5
6
+ metadata.gz: eda031fca9a2527d79a9e80acc41ef740d17bb42a80adfcc9286e7573c68136681c1384ade43e5396799dc7ed73406b0bfe2f4b5821ccf6b08ee8532b5f936c2
7
+ data.tar.gz: 77d8e6e8fda07fa2edeb01e127caa1029f87c8a9f041c27ad3e1d3a7010581d04cf55f84e91c5bbd6fa403b7f396a2e62a5276d19c22d605c77369c071326df0
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- amplitude (0.1.0)
4
+ amplitude-rb (0.1.1)
5
5
  httparty (~> 0.15.6)
6
- multi_json (~> 1.13.1)
6
+ multi_json (~> 1.13)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
@@ -61,7 +61,7 @@ PLATFORMS
61
61
 
62
62
  DEPENDENCIES
63
63
  activejob
64
- amplitude!
64
+ amplitude-rb!
65
65
  pry
66
66
  rspec
67
67
  simplecov
data/README.md CHANGED
@@ -14,13 +14,13 @@ Amplitude analytics service. It communicates with Amplitude over their
14
14
  Via the command-line:
15
15
 
16
16
  ```
17
- gem install amplitude
17
+ gem install amplitude-rb
18
18
  ```
19
19
 
20
20
  Via a Gemfile:
21
21
 
22
22
  ```
23
- gem 'amplitude', '~> x.y.z'
23
+ gem 'amplitude-rb', '~> x.y.z', require: 'amplitude'
24
24
  ```
25
25
 
26
26
  ## Configuration
@@ -1,12 +1,18 @@
1
1
  require 'httparty'
2
2
 
3
- class Amplitude
3
+ module Amplitude
4
4
  require_relative 'amplitude/errors'
5
5
 
6
6
  autoload :Config, "#{__dir__}/amplitude/config.rb"
7
7
  autoload :Event, "#{__dir__}/amplitude/event.rb"
8
+ autoload :Formatter, "#{__dir__}/amplitude/formatter.rb"
8
9
  autoload :HashSlicer, "#{__dir__}/amplitude/hash_slicer.rb"
9
10
 
11
+ module Formatters
12
+ autoload :Hash, "#{__dir__}/amplitude/formatters/hash.rb"
13
+ autoload :Time, "#{__dir__}/amplitude/formatters/time.rb"
14
+ end
15
+
10
16
  module Jobs
11
17
  autoload :Default, "#{__dir__}/amplitude/jobs/default.rb"
12
18
  end
@@ -43,8 +49,7 @@ class Amplitude
43
49
 
44
50
  def queue(data, options = {})
45
51
  raise AsyncError unless async_enabled?
46
- event = Marshal.dump(create_event(data))
47
- Amplitude::Jobs::Default.perform_later(event, options)
52
+ Amplitude::Jobs::Default.perform_later(create_event(data), options)
48
53
  end
49
54
 
50
55
  def async_enabled?
@@ -34,9 +34,9 @@ class Amplitude::Config
34
34
  android_id event_properties user_properties insert_id session_id
35
35
  event_id
36
36
  ),
37
- time_formatter: ->(time) { time ? (time.to_f * 1_000).to_i : nil },
38
- event_prop_formatter: ->(props) { props&.to_h },
39
- user_prop_formatter: ->(props) { props&.to_h },
37
+ time_formatter: Amplitude::Formatters::Time,
38
+ event_prop_formatter: Amplitude::Formatters::Hash,
39
+ user_prop_formatter: Amplitude::Formatters::Hash,
40
40
  iid_generator: ->() { SecureRandom.uuid }
41
41
  }
42
42
  end
@@ -1,4 +1,4 @@
1
- class Amplitude
1
+ module Amplitude
2
2
  class Error < StandardError
3
3
  end
4
4
 
@@ -1,69 +1,8 @@
1
1
  require 'multi_json'
2
2
 
3
3
  class Amplitude::Event
4
- class Formatter
5
- class << self
6
- def call(data)
7
- data = sanitize_data(data)
8
- data = format_data(data)
9
- add_insert_id(data)
10
- end
11
-
12
- def sanitize_data(data)
13
- Amplitude::HashSlicer.call(data, Amplitude.config.whitelist)
14
- end
15
-
16
- def format_data(data)
17
- format_props(format_time(data))
18
- end
19
-
20
- def format_time(data)
21
- format_if_present(data, :time, time_formatter)
22
- end
23
-
24
- def format_props(data)
25
- data = format_if_present(
26
- data,
27
- :event_properties,
28
- event_prop_formatter
29
- )
30
- format_if_present(
31
- data,
32
- :user_properties,
33
- user_prop_formatter
34
- )
35
- end
36
-
37
- def format_if_present(data, key, formatter)
38
- return data unless data[key]
39
- data[key] = formatter.call(data[key])
40
- data
41
- end
42
-
43
- def add_insert_id(data)
44
- return data if data[:insert_id] || Amplitude.config.iid_generator.nil?
45
- data[:insert_id] = Amplitude.config.iid_generator.call()
46
- data
47
- end
48
-
49
- protected
50
-
51
- def time_formatter
52
- Amplitude.config.time_formatter
53
- end
54
-
55
- def event_prop_formatter
56
- Amplitude.config.event_prop_formatter
57
- end
58
-
59
- def user_prop_formatter
60
- Amplitude.config.user_prop_formatter
61
- end
62
- end
63
- end
64
-
65
4
  def initialize(data)
66
- @data = Formatter.call(data)
5
+ @data = Amplitude::Formatter.call(data)
67
6
  end
68
7
 
69
8
  def to_json
@@ -75,7 +14,7 @@ class Amplitude::Event
75
14
  super
76
15
  end
77
16
 
78
- def respond_to?(method)
17
+ def respond_to_missing?(method, include_all = false)
79
18
  return true if @data.key?(method)
80
19
  super
81
20
  end
@@ -0,0 +1,60 @@
1
+ class Amplitude::Formatter
2
+ class << self
3
+ def call(data)
4
+ data = sanitize_data(data)
5
+ data = format_data(data)
6
+ add_insert_id(data)
7
+ end
8
+
9
+ def sanitize_data(data)
10
+ Amplitude::HashSlicer.call(data, Amplitude.config.whitelist)
11
+ end
12
+
13
+ def format_data(data)
14
+ format_props(format_time(data))
15
+ end
16
+
17
+ def format_time(data)
18
+ format_if_present(data, :time, time_formatter)
19
+ end
20
+
21
+ def format_props(data)
22
+ data = format_if_present(
23
+ data,
24
+ :event_properties,
25
+ event_prop_formatter
26
+ )
27
+ format_if_present(
28
+ data,
29
+ :user_properties,
30
+ user_prop_formatter
31
+ )
32
+ end
33
+
34
+ def format_if_present(data, key, formatter)
35
+ return data unless data[key]
36
+ data[key] = formatter.call(data[key])
37
+ data
38
+ end
39
+
40
+ def add_insert_id(data)
41
+ return data if data[:insert_id] || Amplitude.config.iid_generator.nil?
42
+ data[:insert_id] = Amplitude.config.iid_generator.call()
43
+ data
44
+ end
45
+
46
+ protected
47
+
48
+ def time_formatter
49
+ Amplitude.config.time_formatter
50
+ end
51
+
52
+ def event_prop_formatter
53
+ Amplitude.config.event_prop_formatter
54
+ end
55
+
56
+ def user_prop_formatter
57
+ Amplitude.config.user_prop_formatter
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ module Amplitude::Formatters
2
+ class Hash
3
+ class << self
4
+ def call(obj)
5
+ obj&.to_h
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Amplitude::Formatters
2
+ class Time
3
+ class << self
4
+ def call(time)
5
+ return time if time.nil?
6
+ guess = time.to_i
7
+ ::Time.at(guess) > ::Time.now ? guess : convert_to_ms(time)
8
+ end
9
+
10
+ def convert_to_ms(time)
11
+ (time.to_f * 1_000).to_i
12
+ end
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplitude-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Eon
@@ -53,6 +53,9 @@ files:
53
53
  - lib/amplitude/config.rb
54
54
  - lib/amplitude/errors.rb
55
55
  - lib/amplitude/event.rb
56
+ - lib/amplitude/formatter.rb
57
+ - lib/amplitude/formatters/hash.rb
58
+ - lib/amplitude/formatters/time.rb
56
59
  - lib/amplitude/hash_slicer.rb
57
60
  - lib/amplitude/jobs/default.rb
58
61
  homepage: https://gitlab.com/kolorahl/amplitude