openpanel-sdk 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: 8ff0fc445a94bfbb173cbdf14497ffcc92822239f9db6f0ba8d6ff9825c6fa17
4
+ data.tar.gz: ac7759e06ca0c32474be0b03ada5c2475631dc4838aea363178e41023a4e6843
5
+ SHA512:
6
+ metadata.gz: 3cbbafc13b00761bd24f0eeae536ea7162fb3beaf58ab8da8a5ac0d57f95a6cabd66cdefce2a7988f926cd52b5f29cdaf0022f657fe948fb8e87fd3ceda184fa
7
+ data.tar.gz: 8c9257183f18ba1fd007653b7e9846c6cd6ac9866289727234f2108ae1798b61ab97a203acf78f7ca1e5fbed777348e63310226f1c09b739a882705e12c7a7e8
data/.env_sample ADDED
@@ -0,0 +1,3 @@
1
+ OPENPANEL_TRACK_URL=https://api.openpanel.dev/track
2
+ OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID>
3
+ OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 tstaetter
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Openpanel::SDK
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/openpanel/sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openpanel-sdk.
36
+
37
+ ## License
38
+
39
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ module OpenPanel
2
+ module SDK
3
+ class IdentifyUser
4
+ attr_accessor :profile_id, :email, :first_name, :last_name, :properties
5
+
6
+ def to_json
7
+ {
8
+ profileId: profile_id,
9
+ email: email,
10
+ firstName: first_name,
11
+ lastName: last_name,
12
+ properties: properties
13
+ }.to_json
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,116 @@
1
+ require "faraday"
2
+
3
+ module OpenPanel
4
+ module SDK
5
+ class OpenPanelError < StandardError; end
6
+
7
+ class Tracker
8
+ TRACKING_TYPE_IDENTIFY = "identify"
9
+ TRACKING_TYPE_TRACK = "track"
10
+ TRACKING_TYPE_INCREMENT = "increment"
11
+ TRACKING_TYPE_DECREMENT = "decrement"
12
+
13
+ attr_reader :headers
14
+
15
+ ##############################
16
+ # Initialize OpenPanel tracker
17
+ ##############################
18
+ def initialize
19
+ @headers = {
20
+ "Content-Type" => "application/json",
21
+ "openpanel-client-id" => ENV["OPENPANEL_CLIENT_ID"],
22
+ "openpanel-client-secret" => ENV["OPENPANEL_CLIENT_SECRET"],
23
+ }
24
+ end
25
+
26
+ ##############################
27
+ # Set custom header for OpenPanel request
28
+ # @param key [String] header key
29
+ # @param value [String] header value
30
+ ##############################
31
+ def set_header(key, value)
32
+ @headers[key] = value
33
+ end
34
+
35
+ ##############################
36
+ # Track events in OpenPanel
37
+ # https://openpanel.dev/docs/api/track
38
+ # @param event [String] name of event
39
+ # @param tracking_type [String]
40
+ # @param payload [Hash] event payload
41
+ ##############################
42
+ def track(event, tracking_type: TRACKING_TYPE_TRACK, payload: {})
43
+ payload = { type: tracking_type, payload: { name: event, properties: payload } }
44
+
45
+ send_request payload: payload
46
+ end
47
+
48
+ ##############################
49
+ # Track page view in OpenPanel
50
+ # @param user [User] user to track
51
+ # @param path [String] page path
52
+ ##############################
53
+ def track_page_view(user, path)
54
+ if user
55
+ track "view", payload: { profileId: user.profile_id, path: path }
56
+ else
57
+ track "view", payload: { path: path }
58
+ end
59
+ end
60
+
61
+ ##############################
62
+ # Identify user in OpenPanel
63
+ # https://openpanel.dev/docs/api/track
64
+ # @param user [OpenPanel::SDK::IdentifyUser] user to identify
65
+ ##############################
66
+ def identify(user)
67
+ payload = { profileId: user.profile_id, firstName: user.first_name, lastName: user.last_name, email: user.email }
68
+ payload = { type: TRACKING_TYPE_IDENTIFY, payload: payload }
69
+
70
+ send_request payload: payload
71
+ end
72
+
73
+ def increment_property(user, property = "visits", value = 1)
74
+ payload = { profileId: user.profile_id, property: property, value: value }
75
+ payload = { type: TRACKING_TYPE_INCREMENT, payload: payload }
76
+
77
+ send_request payload: payload
78
+ end
79
+
80
+ ##############################
81
+ # Decrement property in OpenPanel
82
+ # @param user [User] user to identify
83
+ #############################
84
+ def decrement_property(user, property = "visits", value = 1)
85
+ payload = { profileId: user.profile_id, property: property, value: value }
86
+ payload = { type: TRACKING_TYPE_DECREMENT, payload: payload }
87
+
88
+ send_request payload: payload
89
+ end
90
+
91
+ private
92
+
93
+ ##############################
94
+ # Send request to OpenPanel API
95
+ # @param url [String] API endpoint URL
96
+ # @param payload [Hash] request payload
97
+ ##############################
98
+ def send_request(url: ENV["OPENPANEL_TRACK_URL"], payload: {})
99
+ response = Faraday.post url, payload.to_json, @headers
100
+
101
+ case response.status
102
+ when 401
103
+ raise OpenPanel::SDK::OpenPanelError, "Unauthorized"
104
+ when 429
105
+ raise OpenPanel::SDK::OpenPanelError, "Too many requests"
106
+ when 500
107
+ raise OpenPanel::SDK::OpenPanelError, "Internal server error"
108
+ else
109
+ response
110
+ end
111
+ rescue StandardError => e
112
+ raise OpenPanel::SDK::OpenPanelError, e.message
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,5 @@
1
+ module OpenPanel
2
+ module SDK
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sdk/identify_user"
4
+ require_relative "sdk/tracker"
5
+ require_relative "sdk/version"
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openpanel-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Stätter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: OpenPanel SDK for Ruby
14
+ email:
15
+ - Thomas Stätter <thomas.staetter@gmail.com>
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".env_sample"
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - lib/openpanel/sdk.rb
25
+ - lib/openpanel/sdk/identify_user.rb
26
+ - lib/openpanel/sdk/tracker.rb
27
+ - lib/openpanel/sdk/version.rb
28
+ homepage: https://github.com/tstaetter/openpanel-sdk
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ allowed_push_host: https://rubygems.org
33
+ homepage_uri: https://github.com/tstaetter/openpanel-sdk
34
+ source_code_uri: https://github.com/tstaetter/openpanel-sdk
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.4.19
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: OpenPanel SDK for Ruby
54
+ test_files: []