simple_segment 0.1.0.pre2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5979b1b25f12af923da26348d23edba401da59d
4
- data.tar.gz: 5e5fb48ad7401d4f940074d82f6588f4efea2b84
3
+ metadata.gz: 47f4f7b0de0d4bf9985f3a4d479fdbbe9a4e1348
4
+ data.tar.gz: f826f5d41f0e0bcd4e73bf7c387abd217511b6dd
5
5
  SHA512:
6
- metadata.gz: 528412610b190fa09d3db9ddfb7401b69752e475d699229315d260538b774dc2a0449f52870fc0a5ed1dc75d232684c44076c406e2ca50e16c1207cbe49c6501
7
- data.tar.gz: 0b7c89ab4123685489cf5dbd79bf3650ddb58c70b63c36fa754a762047172f8dd39def607f3e68a0417b7276d0015adf0c7b2214bd1b93cf4ecb5ea996db746b
6
+ metadata.gz: 039f22d6d9008b18da395ed169ae1498e4f06d875ced8114da315f25be3389ecb876156f2548a8364073d4cab3d7ec768c8a7c143e7625a9a7d8e76e54f2c469
7
+ data.tar.gz: 7198dcfdd23555625149dd5709b6ba5c844a6d6508c5dbd00e87564d335e7029aee8dd5aac2140982f76a29374bef14cf834fb70249d1a6e54884f296dabdc9d
@@ -2,14 +2,15 @@ module SimpleSegment
2
2
  class Batch
3
3
  include SimpleSegment::Utils
4
4
 
5
- attr_reader :config, :payload
6
- attr_accessor :context, :integrations
5
+ attr_reader :client, :payload
7
6
 
8
- def initialize(config)
9
- @config = config
10
- @payload = { batch: [] }
11
- @context = {}
12
- @integrations = {}
7
+ def self.deserialize(client, payload)
8
+ new(client, symbolize_keys(payload))
9
+ end
10
+
11
+ def initialize(client, payload = { batch: [] })
12
+ @client = client
13
+ @payload = payload
13
14
  end
14
15
 
15
16
  def identify(options)
@@ -28,20 +29,30 @@ module SimpleSegment
28
29
  add(Operations::Group, options, __method__)
29
30
  end
30
31
 
32
+ def context=(context)
33
+ payload[:context] = context
34
+ end
35
+
36
+ def integrations=(integrations)
37
+ payload[:integrations] = integrations
38
+ end
39
+
40
+ def serialize
41
+ payload
42
+ end
43
+
31
44
  def commit
32
45
  if payload[:batch].length == 0
33
46
  raise ArgumentError, 'A batch must contain at least one action'
34
47
  end
35
- payload[:context] = context
36
- payload[:integrations] = integrations
37
48
 
38
- Request.new('/v1/import', config).post(payload)
49
+ Request.new(client).post('/v1/import', payload)
39
50
  end
40
51
 
41
52
  private
42
53
 
43
54
  def add(operation_class, options, action)
44
- operation = operation_class.new(symbolize_keys(options), config)
55
+ operation = operation_class.new(client, symbolize_keys(options))
45
56
  operation_payload = operation.build_payload
46
57
  operation_payload[:action] = action
47
58
  payload[:batch] << operation_payload
@@ -21,7 +21,7 @@ module SimpleSegment
21
21
  # @option :integrations [Hash]
22
22
  # @option :timestamp [#iso8601] (Time.now)
23
23
  def identify(options)
24
- Operations::Identify.new(symbolize_keys(options), config).call
24
+ Operations::Identify.new(self, symbolize_keys(options)).call
25
25
  end
26
26
 
27
27
  # @param [Hash] options
@@ -33,7 +33,7 @@ module SimpleSegment
33
33
  # @option :integrations [Hash]
34
34
  # @option :timestamp [#iso8601] (Time.now)
35
35
  def track(options)
36
- Operations::Track.new(symbolize_keys(options), config).call
36
+ Operations::Track.new(self, symbolize_keys(options)).call
37
37
  end
38
38
 
39
39
  # @param [Hash] options
@@ -45,7 +45,7 @@ module SimpleSegment
45
45
  # @option :integrations [Hash]
46
46
  # @option :timestamp [#iso8601] (Time.now)
47
47
  def page(options)
48
- Operations::Page.new(symbolize_keys(options), config).call
48
+ Operations::Page.new(self, symbolize_keys(options)).call
49
49
  end
50
50
 
51
51
  # @param [Hash] options
@@ -57,7 +57,7 @@ module SimpleSegment
57
57
  # @option :integrations [Hash]
58
58
  # @option :timestamp [#iso8601] (Time.now)
59
59
  def group(options)
60
- Operations::Group.new(symbolize_keys(options), config).call
60
+ Operations::Group.new(self, symbolize_keys(options)).call
61
61
  end
62
62
 
63
63
  # @param [Hash] options
@@ -69,11 +69,20 @@ module SimpleSegment
69
69
  # @option :integrations [Hash]
70
70
  # @option :timestamp [#iso8601] (Time.now)
71
71
  def alias(options)
72
- Operations::Alias.new(symbolize_keys(options), config).call
72
+ Operations::Alias.new(self, symbolize_keys(options)).call
73
73
  end
74
74
 
75
+ # @yield [batch] Yields a special batch object that can be used to group
76
+ # `identify`, `track`, `page` and `group` calls into a
77
+ # single API request.
78
+ # @example
79
+ # client.batch do |analytics|
80
+ # analytics.context = { 'foo' => 'bar' }
81
+ # analytics.identify(user_id: 'id')
82
+ # analytics.track(event: 'Delivered Package', user_id: 'id')
83
+ # end
75
84
  def batch
76
- batch = Batch.new(config)
85
+ batch = Batch.new(self)
77
86
  yield(batch)
78
87
  batch.commit
79
88
  end
@@ -2,7 +2,7 @@ module SimpleSegment
2
2
  module Operations
3
3
  class Alias < Operation
4
4
  def call
5
- Request.new('/v1/alias', config).post(build_payload)
5
+ request.post('/v1/alias', build_payload)
6
6
  end
7
7
 
8
8
  def build_payload
@@ -2,7 +2,7 @@ module SimpleSegment
2
2
  module Operations
3
3
  class Group < Operation
4
4
  def call
5
- Request.new('/v1/group', config).post(build_payload)
5
+ request.post('/v1/group', build_payload)
6
6
  end
7
7
 
8
8
  def build_payload
@@ -2,7 +2,7 @@ module SimpleSegment
2
2
  module Operations
3
3
  class Identify < Operation
4
4
  def call
5
- Request.new('/v1/identify', config).post(build_payload)
5
+ request.post('/v1/identify', build_payload)
6
6
  end
7
7
 
8
8
  def build_payload
@@ -8,11 +8,11 @@ module SimpleSegment
8
8
  }
9
9
  }.freeze
10
10
 
11
- attr_reader :options, :config
11
+ attr_reader :options, :request
12
12
 
13
- def initialize(options = {}, config)
13
+ def initialize(client, options = {})
14
14
  @options = options
15
- @config = config
15
+ @request = Request.new(client)
16
16
  end
17
17
 
18
18
  def call
@@ -2,7 +2,7 @@ module SimpleSegment
2
2
  module Operations
3
3
  class Page < Operation
4
4
  def call
5
- Request.new('/v1/page', config).post(build_payload)
5
+ request.post('/v1/page', build_payload)
6
6
  end
7
7
 
8
8
  def build_payload
@@ -2,7 +2,7 @@ module SimpleSegment
2
2
  module Operations
3
3
  class Track < Operation
4
4
  def call
5
- Request.new('/v1/track', config).post(build_payload)
5
+ request.post('/v1/track', build_payload)
6
6
  end
7
7
 
8
8
  def build_payload
@@ -6,15 +6,14 @@ module SimpleSegment
6
6
  'accept' => 'application/json'
7
7
  }.freeze
8
8
 
9
- attr_reader :path, :write_key, :error_handler
9
+ attr_reader :write_key, :error_handler
10
10
 
11
- def initialize(path, config)
12
- @path = path
13
- @write_key = config.write_key
14
- @error_handler = config.on_error
11
+ def initialize(client)
12
+ @write_key = client.config.write_key
13
+ @error_handler = client.config.on_error
15
14
  end
16
15
 
17
- def post(payload, headers: DEFAULT_HEADERS)
16
+ def post(path, payload, headers: DEFAULT_HEADERS)
18
17
  response, status_code, response_body = nil, nil, nil
19
18
  uri = URI(BASE_URL)
20
19
  payload = JSON.generate(payload)
@@ -1,5 +1,9 @@
1
1
  module SimpleSegment
2
2
  module Utils
3
+ def self.included(klass)
4
+ klass.extend(self)
5
+ end
6
+
3
7
  def symbolize_keys(hash)
4
8
  hash.each_with_object({}) { |(key, value), result|
5
9
  result[key.to_sym] = value
@@ -1,3 +1,3 @@
1
1
  module SimpleSegment
2
- VERSION = '0.1.0.pre2'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_segment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikhail Topolskiy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -141,9 +141,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  requirements:
144
- - - ">"
144
+ - - ">="
145
145
  - !ruby/object:Gem::Version
146
- version: 1.3.1
146
+ version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
149
  rubygems_version: 2.6.2