credvault-post-analytics 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +11 -0
  3. data/lib/post_analytics.rb +53 -0
  4. metadata +38 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63ec41314ac54911d726b16d51cb4fc0451bc09109731e053b8aaf263980ae23
4
+ data.tar.gz: 5eb13e515c7b73ac6172e0dd5f66bbff8381fef4d1c1c4cf34e7fbdeef4814bc
5
+ SHA512:
6
+ metadata.gz: 76bb31717d225cf78face0048d59d422ae607c531bf0ee93103920c39b1b4a23af0b8187f895a246024db117a5d330af2b4eb96eea8b71c4481401fbec2b1b70
7
+ data.tar.gz: 156c92244bbe2c201cb19101be02886cb780175a936047500a1204c5e2367f420abdf5c60bca40e78edbe6765f2ebc1ff712ef58898efc7e7389cbb809e63ea9
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Post Analytics Ruby SDK
2
+
3
+ Install this gem from the source tree or publish it to your configured gem registry. Create one client with the
4
+ project API key and host, then call `capture` when your application performs an action:
5
+
6
+ ```ruby
7
+ post = PostAnalytics::Client.new
8
+ post.capture('invoice_paid', distinct_id: invoice.customer_id, properties: { amount: invoice.amount })
9
+ ```
10
+
11
+ The HTTP transport uses Ruby's standard library and targets `/i/v0/e/` and `/flags/?v=2` on the configured host.
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'time'
4
+ require 'uri'
5
+
6
+ module PostAnalytics
7
+ VERSION = '0.1.0'
8
+
9
+ class Error < StandardError; end
10
+
11
+ class Client
12
+ def initialize(api_key: ENV.fetch('POST_PROJECT_API_KEY', ''), host: ENV.fetch('POST_ANALYTICS_HOST', ''), open_timeout: 5, read_timeout: 10)
13
+ @api_key = api_key.strip
14
+ @host = URI.parse(host)
15
+ raise ArgumentError, 'project API key is required' if @api_key.empty?
16
+ raise ArgumentError, 'host must use HTTP or HTTPS' unless %w[http https].include?(@host.scheme)
17
+ @open_timeout, @read_timeout = open_timeout, read_timeout
18
+ rescue URI::InvalidURIError => error
19
+ raise ArgumentError, "invalid host: #{error.message}"
20
+ end
21
+
22
+ def capture(event, distinct_id:, properties: {})
23
+ raise ArgumentError, 'event and distinct_id are required' if event.to_s.strip.empty? || distinct_id.to_s.empty?
24
+ props = properties.merge('$lib' => 'post-analytics-ruby', '$lib_version' => VERSION)
25
+ post('/i/v0/e/', api_key: @api_key, event: event, distinct_id: distinct_id.to_s,
26
+ properties: props, timestamp: Time.now.utc.iso8601(6))
27
+ end
28
+
29
+ def identify(user_id, anonymous_id: nil, properties: {})
30
+ fields = { '$set' => properties }
31
+ fields['$anon_distinct_id'] = anonymous_id if anonymous_id
32
+ capture('$identify', distinct_id: user_id, properties: fields)
33
+ end
34
+
35
+ def load_feature_flags(distinct_id:, groups: nil)
36
+ payload = { api_key: @api_key, distinct_id: distinct_id }
37
+ payload[:groups] = groups if groups
38
+ post('/flags/?v=2', payload)
39
+ end
40
+
41
+ private
42
+
43
+ def post(path, payload)
44
+ uri = @host.dup
45
+ uri.path = "#{uri.path.to_s.sub(%r{/+$}, '')}#{path}"
46
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Accept' => 'application/json')
47
+ request.body = JSON.generate(payload)
48
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: @open_timeout, read_timeout: @read_timeout) { |http| http.request(request) }
49
+ raise Error, "Post Analytics returned HTTP #{response.code}: #{response.body.to_s[0, 500]}" unless response.is_a?(Net::HTTPSuccess)
50
+ response.body.to_s.empty? ? {} : JSON.parse(response.body)
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,38 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: credvault-post-analytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - CredVault
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ executables: []
13
+ extensions: []
14
+ extra_rdoc_files: []
15
+ files:
16
+ - README.md
17
+ - lib/post_analytics.rb
18
+ licenses:
19
+ - Proprietary
20
+ metadata: {}
21
+ rdoc_options: []
22
+ require_paths:
23
+ - lib
24
+ required_ruby_version: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '3.0'
29
+ required_rubygems_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ requirements: []
35
+ rubygems_version: 3.6.7
36
+ specification_version: 4
37
+ summary: First-party Post Analytics SDK for Ruby
38
+ test_files: []