Class: Evntaly::SDK
- Inherits:
-
Object
- Object
- Evntaly::SDK
- Defined in:
- lib/evntaly/sdk.rb
Constant Summary collapse
- @@tracking_enabled =
true
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#developer_secret ⇒ Object
Returns the value of attribute developer_secret.
-
#project_token ⇒ Object
Returns the value of attribute project_token.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #check_limit ⇒ Object
- #identify_user(user) ⇒ Object
-
#initialize(developer_secret:, project_token:) ⇒ SDK
constructor
A new instance of SDK.
- #set_timeout(seconds) ⇒ Object
- #track(event) ⇒ Object
Constructor Details
#initialize(developer_secret:, project_token:) ⇒ SDK
11 12 13 14 15 |
# File 'lib/evntaly/sdk.rb', line 11 def initialize(developer_secret:, project_token:) @base_url = "https://app.evntaly.com/prod" @developer_secret = developer_secret @project_token = project_token end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/evntaly/sdk.rb', line 7 def base_url @base_url end |
#developer_secret ⇒ Object
Returns the value of attribute developer_secret.
7 8 9 |
# File 'lib/evntaly/sdk.rb', line 7 def developer_secret @developer_secret end |
#project_token ⇒ Object
Returns the value of attribute project_token.
7 8 9 |
# File 'lib/evntaly/sdk.rb', line 7 def project_token @project_token end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/evntaly/sdk.rb', line 7 def timeout @timeout end |
Class Method Details
.disable_tracking ⇒ Object
86 87 88 89 |
# File 'lib/evntaly/sdk.rb', line 86 def self.disable_tracking @@tracking_enabled = false puts "🚫 Tracking disabled." end |
.enable_tracking ⇒ Object
91 92 93 94 |
# File 'lib/evntaly/sdk.rb', line 91 def self.enable_tracking @@tracking_enabled = true puts "🟢 Tracking enabled." end |
.tracking_enabled? ⇒ Boolean
82 83 84 |
# File 'lib/evntaly/sdk.rb', line 82 def self.tracking_enabled? @@tracking_enabled end |
Instance Method Details
#check_limit ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/evntaly/sdk.rb', line 21 def check_limit uri = URI("#{base_url}/api/v1/account/check-limits/#{developer_secret}") request = Net::HTTP::Get.new(uri, "Content-Type" => "application/json") response = send_request(uri, request) result = JSON.parse(response.body) raise "Unexpected API response format" unless result.key?("limitReached") !result["limitReached"] rescue => e puts "Error checking limit: #{e.}" false end |
#identify_user(user) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/evntaly/sdk.rb', line 63 def identify_user(user) uri = URI("#{base_url}/api/v1/register/user") request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" request["secret"] = developer_secret request["pat"] = project_token request.body = user.to_json response = send_request(uri, request) unless response.code.to_i.between?(200, 299) raise "Failed to identify user: status code #{response.code}" end puts "✅ User identified successfully" true end |
#set_timeout(seconds) ⇒ Object
17 18 19 |
# File 'lib/evntaly/sdk.rb', line 17 def set_timeout(seconds) @timeout = seconds end |
#track(event) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/evntaly/sdk.rb', line 36 def track(event) unless self.class.tracking_enabled? puts "Tracking is disabled. Event not sent." return end unless check_limit puts "checkLimit returned false. Event not sent." return end uri = URI("#{base_url}/api/v1/register/event") request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" request["secret"] = developer_secret request["pat"] = project_token request.body = event.to_json response = send_request(uri, request) unless response.code.to_i.between?(200, 299) raise "Failed to track event: status code #{response.code}" end puts "✅ Event tracked successfully" end |