altertable 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c46dba4a691e7862c18858af04ef49a5f04be3f0517329689619093044a96281
4
+ data.tar.gz: fbd8369a7db172dbc2ef4b5d836484a0513ff90ac749c2d5bf800a4515a933f3
5
+ SHA512:
6
+ metadata.gz: f35d1fbda8a0a0e27d01dcb556d67393a2ca4a9ad82de9b6ee6158aaf08e7fe1c5626f505c1969a436bda3d381ee6831f2bc3d79ab15fca5a4b084d8bfac7015
7
+ data.tar.gz: ed184510f935084ea09bc939094ee450894ebcfbd66652cb5656f9cc2e4e684fb5b22cc260cff16e0d1037760a4c8640465e899b1846968d2b2409786f163a31
@@ -0,0 +1,47 @@
1
+ name: Bug Report
2
+ description: Report a bug
3
+ labels: ["bug"]
4
+ body:
5
+ - type: textarea
6
+ id: description
7
+ attributes:
8
+ label: Description
9
+ description: A clear description of the bug.
10
+ validations:
11
+ required: true
12
+ - type: textarea
13
+ id: reproduction
14
+ attributes:
15
+ label: Steps to Reproduce
16
+ description: Minimal code or steps to reproduce the issue.
17
+ validations:
18
+ required: true
19
+ - type: textarea
20
+ id: expected
21
+ attributes:
22
+ label: Expected Behavior
23
+ validations:
24
+ required: true
25
+ - type: textarea
26
+ id: actual
27
+ attributes:
28
+ label: Actual Behavior
29
+ validations:
30
+ required: true
31
+ - type: input
32
+ id: version
33
+ attributes:
34
+ label: SDK Version
35
+ validations:
36
+ required: true
37
+ - type: input
38
+ id: runtime
39
+ attributes:
40
+ label: "{language} Version"
41
+ validations:
42
+ required: true
43
+ - type: dropdown
44
+ id: os
45
+ attributes:
46
+ label: Operating System
47
+ options: [macOS, Linux, Windows, Other]
@@ -0,0 +1,21 @@
1
+ name: Feature Request
2
+ description: Suggest a feature
3
+ labels: ["enhancement"]
4
+ body:
5
+ - type: textarea
6
+ id: problem
7
+ attributes:
8
+ label: Problem
9
+ description: What problem does this solve?
10
+ validations:
11
+ required: true
12
+ - type: textarea
13
+ id: solution
14
+ attributes:
15
+ label: Proposed Solution
16
+ validations:
17
+ required: true
18
+ - type: textarea
19
+ id: alternatives
20
+ attributes:
21
+ label: Alternatives Considered
@@ -0,0 +1,42 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ["3.2", "3.3", "3.4", "4.0"]
15
+
16
+ services:
17
+ altertable:
18
+ image: ghcr.io/altertable-ai/altertable-mock:latest
19
+ ports:
20
+ - 15001:15001
21
+ env:
22
+ ALTERTABLE_MOCK_API_KEYS: test_pk_abc123
23
+ options: >-
24
+ --health-cmd "exit 0"
25
+ --health-interval 5s
26
+ --health-timeout 3s
27
+ --health-retries 3
28
+ --health-start-period 10s
29
+
30
+ steps:
31
+ - uses: actions/checkout@v4
32
+ with:
33
+ submodules: recursive
34
+
35
+ - name: Set up Ruby
36
+ uses: ruby/setup-ruby@v1
37
+ with:
38
+ ruby-version: ${{ matrix.ruby-version }}
39
+ bundler-cache: true
40
+
41
+ - name: Run tests
42
+ run: bundle exec rake spec
@@ -0,0 +1,47 @@
1
+ name: Release Please
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ check:
10
+ runs-on: ubuntu-latest
11
+
12
+ permissions:
13
+ contents: write
14
+ pull-requests: write
15
+
16
+ steps:
17
+ - uses: googleapis/release-please-action@v4
18
+ id: release
19
+ with:
20
+ token: ${{ secrets.GITHUB_TOKEN }}
21
+ config-file: release-please-config.json
22
+ manifest-file: .release-please-manifest.json
23
+ outputs:
24
+ tag_name: ${{ steps.release.outputs.tag_name }}
25
+ release_created: ${{ steps.release.outputs.release_created }}
26
+
27
+ publish:
28
+ needs: check
29
+
30
+ if: ${{ needs.check.outputs.release_created }}
31
+
32
+ runs-on: ubuntu-latest
33
+
34
+ permissions:
35
+ contents: write
36
+ pull-requests: write
37
+ id-token: write
38
+
39
+ steps:
40
+ - uses: actions/checkout@v4
41
+ with:
42
+ persist-credentials: false
43
+ - uses: ruby/setup-ruby@v1
44
+ with:
45
+ ruby-version: 3.4
46
+ bundler-cache: true
47
+ - uses: rubygems/release-gem@v1
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "specs"]
2
+ path = specs
3
+ url = https://github.com/altertable-ai/altertable-client-specs.git
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "0.1.0"
3
+ }
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ Exclude:
8
+ - 'vendor/**/*'
9
+ - 'spec/spec_helper.rb'
10
+
11
+ Layout/LineLength:
12
+ Max: 120
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/FrozenStringLiteralComment:
18
+ Enabled: true
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,83 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or advances of any kind
22
+ * Trolling, insulting or derogatory comments, and personal or political attacks
23
+ * Public or private harassment
24
+ * Publishing others' private information, such as a physical or email address, without their explicit permission
25
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
26
+
27
+ ## Enforcement Responsibilities
28
+
29
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
32
+
33
+ ## Scope
34
+
35
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
36
+
37
+ ## Enforcement
38
+
39
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at opensource@altertable.ai. All complaints will be reviewed and investigated promptly and fairly.
40
+
41
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
42
+
43
+ ## Enforcement Guidelines
44
+
45
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
46
+
47
+ ### 1. Correction
48
+
49
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
50
+
51
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
52
+
53
+ ### 2. Warning
54
+
55
+ **Community Impact**: A violation through a single incident or series of actions.
56
+
57
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
58
+
59
+ ### 3. Temporary Ban
60
+
61
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
62
+
63
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
64
+
65
+ ### 4. Permanent Ban
66
+
67
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
68
+
69
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
70
+
71
+ ## Attribution
72
+
73
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
74
+
75
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
76
+
77
+ For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations].
78
+
79
+ [homepage]: https://www.contributor-covenant.org
80
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
81
+ [Mozilla CoC]: https://github.com/mozilla/diversity
82
+ [FAQ]: https://www.contributor-covenant.org/faq
83
+ [translations]: https://www.contributor-covenant.org/translations
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,32 @@
1
+ # Contributing to altertable-ruby
2
+
3
+ ## Development Setup
4
+
5
+ 1. Fork and clone the repository
6
+ 2. Install dependencies: `bundle install`
7
+ 3. Run tests: `bundle exec rspec`
8
+
9
+ ## Making Changes
10
+
11
+ 1. Create a branch from `main`
12
+ 2. Make your changes
13
+ 3. Add or update tests
14
+ 4. Run the full check suite: `bundle exec rake`
15
+ 5. Commit using [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `docs:`, etc.)
16
+ 6. Open a pull request
17
+
18
+ ## Code Style
19
+
20
+ This project uses `RuboCop` for linting and formatting. Run `bundle exec rubocop` before committing.
21
+
22
+ ## Tests
23
+
24
+ - Unit tests are required for all new functionality
25
+ - Integration tests run in CI when credentials are available
26
+ - Run tests locally: `bundle exec rspec`
27
+
28
+ ## Pull Requests
29
+
30
+ - Keep PRs focused on a single change
31
+ - Update `CHANGELOG.md` under `[Unreleased]`
32
+ - Ensure CI passes before requesting review
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rspec"
6
+ gem "testcontainers"
7
+ gem "base64"
data/Gemfile.lock ADDED
@@ -0,0 +1,128 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ altertable (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.9)
10
+ public_suffix (>= 2.0.2, < 8.0)
11
+ ast (2.4.3)
12
+ base64 (0.3.0)
13
+ bigdecimal (4.0.1)
14
+ diff-lcs (1.6.2)
15
+ docker-api (2.4.0)
16
+ excon (>= 0.64.0)
17
+ multi_json
18
+ excon (1.4.0)
19
+ logger
20
+ faraday (2.14.1)
21
+ faraday-net_http (>= 2.0, < 3.5)
22
+ json
23
+ logger
24
+ faraday-net_http (3.4.2)
25
+ net-http (~> 0.5)
26
+ faraday-retry (2.4.0)
27
+ faraday (~> 2.0)
28
+ http-2 (1.1.3)
29
+ httpx (1.7.3)
30
+ http-2 (>= 1.1.3)
31
+ json (2.19.0)
32
+ json-schema (6.2.0)
33
+ addressable (~> 2.8)
34
+ bigdecimal (>= 3.1, < 5)
35
+ language_server-protocol (3.17.0.5)
36
+ lint_roller (1.1.0)
37
+ logger (1.7.0)
38
+ mcp (0.8.0)
39
+ json-schema (>= 4.1)
40
+ multi_json (1.19.1)
41
+ net-http (0.9.1)
42
+ uri (>= 0.11.1)
43
+ parallel (1.27.0)
44
+ parser (3.3.10.2)
45
+ ast (~> 2.4.1)
46
+ racc
47
+ prism (1.9.0)
48
+ public_suffix (7.0.5)
49
+ racc (1.8.1)
50
+ rainbow (3.1.1)
51
+ rake (13.3.1)
52
+ regexp_parser (2.11.3)
53
+ rspec (3.13.2)
54
+ rspec-core (~> 3.13.0)
55
+ rspec-expectations (~> 3.13.0)
56
+ rspec-mocks (~> 3.13.0)
57
+ rspec-core (3.13.6)
58
+ rspec-support (~> 3.13.0)
59
+ rspec-expectations (3.13.5)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.13.0)
62
+ rspec-mocks (3.13.8)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.13.0)
65
+ rspec-support (3.13.7)
66
+ rubocop (1.85.1)
67
+ json (~> 2.3)
68
+ language_server-protocol (~> 3.17.0.2)
69
+ lint_roller (~> 1.1.0)
70
+ mcp (~> 0.6)
71
+ parallel (~> 1.10)
72
+ parser (>= 3.3.0.2)
73
+ rainbow (>= 2.2.2, < 4.0)
74
+ regexp_parser (>= 2.9.3, < 3.0)
75
+ rubocop-ast (>= 1.49.0, < 2.0)
76
+ ruby-progressbar (~> 1.7)
77
+ unicode-display_width (>= 2.4.0, < 4.0)
78
+ rubocop-ast (1.49.0)
79
+ parser (>= 3.3.7.2)
80
+ prism (~> 1.7)
81
+ rubocop-capybara (2.22.1)
82
+ lint_roller (~> 1.1)
83
+ rubocop (~> 1.72, >= 1.72.1)
84
+ rubocop-factory_bot (2.28.0)
85
+ lint_roller (~> 1.1)
86
+ rubocop (~> 1.72, >= 1.72.1)
87
+ rubocop-performance (1.26.1)
88
+ lint_roller (~> 1.1)
89
+ rubocop (>= 1.75.0, < 2.0)
90
+ rubocop-ast (>= 1.47.1, < 2.0)
91
+ rubocop-rspec (2.31.0)
92
+ rubocop (~> 1.40)
93
+ rubocop-capybara (~> 2.17)
94
+ rubocop-factory_bot (~> 2.22)
95
+ rubocop-rspec_rails (~> 2.28)
96
+ rubocop-rspec_rails (2.29.1)
97
+ rubocop (~> 1.61)
98
+ ruby-progressbar (1.13.0)
99
+ testcontainers (0.2.0)
100
+ testcontainers-core (= 0.2.0)
101
+ testcontainers-core (0.2.0)
102
+ docker-api (~> 2.2)
103
+ unicode-display_width (3.2.0)
104
+ unicode-emoji (~> 4.1)
105
+ unicode-emoji (4.2.0)
106
+ uri (1.1.1)
107
+
108
+ PLATFORMS
109
+ arm64-darwin-25
110
+ ruby
111
+ x86_64-linux
112
+
113
+ DEPENDENCIES
114
+ altertable!
115
+ base64
116
+ faraday (~> 2.0)
117
+ faraday-net_http
118
+ faraday-retry
119
+ httpx
120
+ rake (~> 13.0)
121
+ rspec (~> 3.0, >= 0)
122
+ rubocop (~> 1.0)
123
+ rubocop-performance (~> 1.0)
124
+ rubocop-rspec (~> 2.0)
125
+ testcontainers
126
+
127
+ BUNDLED WITH
128
+ 4.0.7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Altertable
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Altertable Ruby SDK
2
+
3
+ Official Ruby SDK for Altertable Product Analytics.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'altertable-ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ ### Initialization
20
+
21
+ ```ruby
22
+ require 'altertable'
23
+
24
+ Altertable.init('your_api_key', {
25
+ environment: 'production'
26
+ })
27
+ ```
28
+
29
+ ### Tracking Events
30
+
31
+ ```ruby
32
+ Altertable.track('button_clicked', 'user_123', {
33
+ button_id: 'signup_btn',
34
+ page: 'home'
35
+ })
36
+ ```
37
+
38
+ ### Identifying Users
39
+
40
+ ```ruby
41
+ Altertable.identify('user_123', {
42
+ email: 'user@example.com',
43
+ name: 'John Doe'
44
+ })
45
+ ```
46
+
47
+ ### Alias
48
+
49
+ ```ruby
50
+ Altertable.alias('new_user_id', 'previous_anonymous_id')
51
+ ```
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/SECURITY.md ADDED
@@ -0,0 +1,18 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Only the latest minor release receives security patches.
6
+
7
+ ## Reporting a Vulnerability
8
+
9
+ **Do not open a public issue.**
10
+
11
+ Email security@altertable.ai with:
12
+
13
+ 1. Description of the vulnerability
14
+ 2. Steps to reproduce
15
+ 3. Impact assessment
16
+ 4. (Optional) Suggested fix
17
+
18
+ We will acknowledge receipt within 48 hours and aim to release a patch within 7 days of confirmation.
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "altertable"
5
+ spec.version = "0.1.0"
6
+ spec.authors = ["Altertable"]
7
+ spec.email = ["support@api.altertable.ai"]
8
+
9
+ spec.summary = "Altertable Product Analytics Ruby SDK"
10
+ spec.description = "Official Ruby client for Altertable Product Analytics"
11
+ spec.homepage = "https://github.com/altertable-ai/altertable-ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "https://github.com/altertable-ai/altertable-ruby/blob/main/CHANGELOG.md"
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "rake", "~> 13.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "rubocop", "~> 1.0"
29
+ spec.add_development_dependency "rubocop-performance", "~> 1.0"
30
+ spec.add_development_dependency "rubocop-rspec", "~> 2.0"
31
+ spec.add_development_dependency "testcontainers"
32
+
33
+ # Optional adapter support (development only)
34
+ spec.add_development_dependency "faraday", "~> 2.0"
35
+ spec.add_development_dependency "faraday-retry"
36
+ spec.add_development_dependency "faraday-net_http"
37
+ spec.add_development_dependency "httpx"
38
+ end
@@ -0,0 +1,106 @@
1
+ module Altertable
2
+ module Adapters
3
+ Response = Struct.new(:status, :body, :headers)
4
+
5
+ class Base
6
+ def initialize(base_url:, timeout:, headers: {})
7
+ @base_url = base_url
8
+ @timeout = timeout
9
+ @headers = headers
10
+ end
11
+
12
+ def post(path, body: nil, params: {}, &block)
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+
17
+ class FaradayAdapter < Base
18
+ def initialize(base_url:, timeout:, headers: {})
19
+ super
20
+ require "faraday"
21
+ require "faraday/retry"
22
+ require "faraday/net_http"
23
+
24
+ @conn = Faraday.new(url: @base_url) do |f|
25
+ @headers.each { |k, v| f.headers[k] = v }
26
+ f.options.timeout = @timeout
27
+ f.request :retry, max: 3, interval: 0.05, backoff_factor: 2
28
+ f.adapter Faraday.default_adapter
29
+ end
30
+ end
31
+
32
+ def post(path, body: nil, params: {}, &block)
33
+ resp = @conn.post(path) do |req|
34
+ req.params = params
35
+ req.body = body
36
+ end
37
+ wrap_response(resp)
38
+ rescue Faraday::ConnectionFailed => e
39
+ raise Altertable::NetworkError.new(e.message, e)
40
+ rescue Faraday::TimeoutError => e
41
+ raise Altertable::NetworkError.new("Timeout: #{e.message}", e)
42
+ end
43
+
44
+ private
45
+
46
+ def wrap_response(resp)
47
+ Response.new(resp.status, resp.body, resp.headers)
48
+ end
49
+ end
50
+
51
+ class HttpxAdapter < Base
52
+ def initialize(base_url:, timeout:, headers: {})
53
+ super
54
+ require "httpx"
55
+ @client = HTTPX.plugin(:retries).with(
56
+ timeout: { operation_timeout: @timeout },
57
+ headers: @headers,
58
+ base_url: @base_url
59
+ )
60
+ end
61
+
62
+ def post(path, body: nil, params: {}, &block)
63
+ resp = @client.post(path, body: body, params: params)
64
+ wrap_response(resp)
65
+ rescue HTTPX::Error => e
66
+ raise Altertable::NetworkError.new(e.message, e)
67
+ end
68
+
69
+ private
70
+
71
+ def wrap_response(resp)
72
+ if resp.is_a?(HTTPX::ErrorResponse)
73
+ raise Altertable::NetworkError.new(resp.error.message, resp.error)
74
+ end
75
+ Response.new(resp.status, resp.to_s, resp.headers)
76
+ end
77
+ end
78
+
79
+ class NetHttpAdapter < Base
80
+ def initialize(base_url:, timeout:, headers: {})
81
+ super
82
+ require "net/http"
83
+ require "uri"
84
+ @uri = URI.parse(@base_url)
85
+ end
86
+
87
+ def post(path, body: nil, params: {}, &block)
88
+ uri = URI.join(@uri, path)
89
+ uri.query = URI.encode_www_form(params) unless params.empty?
90
+
91
+ req = Net::HTTP::Post.new(uri)
92
+ @headers.each { |k, v| req[k] = v }
93
+ req.body = body if body
94
+
95
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: @timeout, read_timeout: @timeout) do |http|
96
+ resp = http.request(req)
97
+ Response.new(resp.code.to_i, resp.body, resp.to_hash)
98
+ end
99
+ rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e
100
+ raise Altertable::NetworkError.new("Timeout: #{e.message}", e)
101
+ rescue StandardError => e
102
+ raise Altertable::NetworkError.new(e.message, e)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+ require_relative "errors"
6
+ require_relative "adapters"
7
+
8
+ module Altertable
9
+ class Client
10
+ DEFAULT_BASE_URL = "https://api.altertable.ai"
11
+ DEFAULT_TIMEOUT = 5
12
+ DEFAULT_ENVIRONMENT = "production"
13
+
14
+ def initialize(api_key, options = {})
15
+ raise ConfigurationError, "API Key is required" if api_key.nil? || api_key.empty?
16
+
17
+ @api_key = api_key
18
+ @base_url = options[:base_url] || DEFAULT_BASE_URL
19
+ @environment = options[:environment] || DEFAULT_ENVIRONMENT
20
+ @timeout = options[:request_timeout] || DEFAULT_TIMEOUT
21
+ @release = options[:release]
22
+ @debug = options[:debug] || false
23
+ @on_error = options[:on_error]
24
+
25
+ # Initialize adapter
26
+ adapter_name = options[:adapter]
27
+ headers = {
28
+ "X-API-Key" => @api_key,
29
+ "Content-Type" => "application/json"
30
+ }
31
+ @adapter = select_adapter(adapter_name, { base_url: @base_url, timeout: @timeout, headers: headers })
32
+ end
33
+
34
+ def track(event, distinct_id, properties = {})
35
+ payload = {
36
+ timestamp: Time.now.utc.iso8601(3),
37
+ event: event,
38
+ environment: @environment,
39
+ distinct_id: distinct_id,
40
+ properties: {
41
+ "$lib": "altertable-ruby",
42
+ "$lib_version": Altertable::VERSION
43
+ }.merge(properties)
44
+ }
45
+ payload[:properties]["$release"] = @release if @release
46
+
47
+ post("/track", payload)
48
+ end
49
+
50
+ def identify(user_id, traits = {})
51
+ payload = {
52
+ timestamp: Time.now.utc.iso8601(3),
53
+ environment: @environment,
54
+ distinct_id: user_id,
55
+ traits: traits
56
+ }
57
+
58
+ post("/identify", payload)
59
+ end
60
+
61
+ def alias(new_user_id, previous_id)
62
+ payload = {
63
+ timestamp: Time.now.utc.iso8601(3),
64
+ environment: @environment,
65
+ distinct_id: previous_id,
66
+ new_user_id: new_user_id
67
+ }
68
+
69
+ post("/alias", payload)
70
+ end
71
+
72
+ private
73
+
74
+ def select_adapter(name, options)
75
+ case name
76
+ when :faraday
77
+ Adapters::FaradayAdapter.new(**options)
78
+ when :httpx
79
+ Adapters::HttpxAdapter.new(**options)
80
+ when :net_http
81
+ Adapters::NetHttpAdapter.new(**options)
82
+ else
83
+ # Auto-detect
84
+ if defined?(Faraday) || try_require("faraday")
85
+ Adapters::FaradayAdapter.new(**options)
86
+ elsif defined?(HTTPX) || try_require("httpx")
87
+ Adapters::HttpxAdapter.new(**options)
88
+ else
89
+ Adapters::NetHttpAdapter.new(**options)
90
+ end
91
+ end
92
+ end
93
+
94
+ def try_require(gem_name)
95
+ require gem_name
96
+ true
97
+ rescue LoadError
98
+ false
99
+ end
100
+
101
+ def post(path, payload)
102
+ res = @adapter.post(path, body: payload.to_json)
103
+ handle_response(res)
104
+ rescue StandardError => e
105
+ handle_error(e)
106
+ end
107
+
108
+ def handle_response(res)
109
+ case res.status
110
+ when 200..299
111
+ JSON.parse(res.body) rescue {}
112
+ when 422
113
+ error_data = JSON.parse(res.body) rescue {}
114
+ raise ApiError.new("Unprocessable Entity: #{error_data["message"]}", res.status, error_data)
115
+ else
116
+ raise ApiError.new("HTTP Error: #{res.status}", res.status)
117
+ end
118
+ end
119
+
120
+ def handle_error(error)
121
+ wrapped_error = if error.is_a?(AltertableError)
122
+ error
123
+ else
124
+ AltertableError.new(error.message, error)
125
+ end
126
+
127
+ @on_error&.call(wrapped_error) if @on_error
128
+ raise wrapped_error
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Altertable
4
+ class AltertableError < StandardError
5
+ attr_reader :cause
6
+
7
+ def initialize(message, cause = nil)
8
+ super(message)
9
+ @cause = cause
10
+ end
11
+ end
12
+
13
+ class ConfigurationError < AltertableError; end
14
+
15
+ class ApiError < AltertableError
16
+ attr_reader :status, :details
17
+
18
+ def initialize(message, status, details = {})
19
+ super(message)
20
+ @status = status
21
+ @details = details
22
+ end
23
+ end
24
+
25
+ class NetworkError < AltertableError; end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Altertable
4
+ VERSION = "0.1.0"
5
+ end
data/lib/altertable.rb ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "altertable/version"
4
+ require_relative "altertable/errors"
5
+ require_relative "altertable/client"
6
+
7
+ module Altertable
8
+ class << self
9
+ def init(api_key, options = {})
10
+ @client = Client.new(api_key, options)
11
+ end
12
+
13
+ def track(event, user_id, properties = {})
14
+ client.track(event, user_id, properties)
15
+ end
16
+
17
+ def identify(user_id, traits = {})
18
+ client.identify(user_id, traits)
19
+ end
20
+
21
+ def alias(new_user_id, previous_id)
22
+ client.alias(new_user_id, previous_id)
23
+ end
24
+
25
+ def client
26
+ raise ConfigurationError, "Altertable client not initialized. Call Altertable.init(api_key) first." unless @client
27
+
28
+ @client
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ {
2
+ "packages": {
3
+ ".": {
4
+ "release-type": "ruby",
5
+ "package-name": "altertable",
6
+ "version-file": "lib/altertable/version.rb"
7
+ }
8
+ }
9
+ }
metadata ADDED
@@ -0,0 +1,206 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: altertable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Altertable
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rake
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '13.0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '13.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rubocop-performance
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rubocop-rspec
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: testcontainers
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: faraday
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: faraday-retry
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: faraday-net_http
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ - !ruby/object:Gem::Dependency
139
+ name: httpx
140
+ requirement: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ description: Official Ruby client for Altertable Product Analytics
153
+ email:
154
+ - support@api.altertable.ai
155
+ executables: []
156
+ extensions: []
157
+ extra_rdoc_files: []
158
+ files:
159
+ - ".github/ISSUE_TEMPLATE/bug_report.yml"
160
+ - ".github/ISSUE_TEMPLATE/feature_request.yml"
161
+ - ".github/workflows/ci.yml"
162
+ - ".github/workflows/release-please.yml"
163
+ - ".gitmodules"
164
+ - ".release-please-manifest.json"
165
+ - ".rubocop.yml"
166
+ - CHANGELOG.md
167
+ - CODE_OF_CONDUCT.md
168
+ - CONTRIBUTING.md
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE
172
+ - README.md
173
+ - Rakefile
174
+ - SECURITY.md
175
+ - altertable.gemspec
176
+ - lib/altertable.rb
177
+ - lib/altertable/adapters.rb
178
+ - lib/altertable/client.rb
179
+ - lib/altertable/errors.rb
180
+ - lib/altertable/version.rb
181
+ - release-please-config.json
182
+ homepage: https://github.com/altertable-ai/altertable-ruby
183
+ licenses:
184
+ - MIT
185
+ metadata:
186
+ homepage_uri: https://github.com/altertable-ai/altertable-ruby
187
+ source_code_uri: https://github.com/altertable-ai/altertable-ruby
188
+ changelog_uri: https://github.com/altertable-ai/altertable-ruby/blob/main/CHANGELOG.md
189
+ rdoc_options: []
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: 2.6.0
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ requirements: []
203
+ rubygems_version: 3.6.7
204
+ specification_version: 4
205
+ summary: Altertable Product Analytics Ruby SDK
206
+ test_files: []