openpanel-sdk 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: f058e44eeada090f73556e3717608d7f61868776840970d2ca51ffa14120b108
4
- data.tar.gz: c9a7d201a55dca8e07096e13e8a5d7e415970cd2ccda2b3bae1ff2c08280db8a
3
+ metadata.gz: d14ac55d813c9ee97bcee346b856798bd52b1acac190a62945b4914f6588d1ad
4
+ data.tar.gz: 9bd22b7c57b950edb91b33c98c4653ad43dcbd18906dddda910fdb402481ec26
5
5
  SHA512:
6
- metadata.gz: fbe6e1946cb014f429fdce91ab381900f7701a536a6142b8eac6cdf7a957f8e2264933f525f90ac9b20eba370c826dfa323cbf018cb29d62405a4fbb7e51560e
7
- data.tar.gz: 1b3254a51f15c7a4604013c56b523fe1e53263d32c0c815e71a578970e08b097357f30462015442eaa4b523655eb885961c0114871bf5ea82e346d4ef284c0c5
6
+ metadata.gz: 36be770f2140341f272334848831d667be6dbd44435e2aa6fa5a2ae1752fa76ee7b8c67bf348611c06b6b9817423fe3c11b5eb6835e61bdc0faa7d214ad7701b
7
+ data.tar.gz: 41be2ad0aeeb1ceb343e1fce8190591447bba27827bf0c7bbba2349e8ec2edcd0374ffd259bea6c30cffb5ea7f35735ec8fdea2322a087831df852f763df59f0
data/README.md CHANGED
@@ -16,9 +16,21 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
16
  gem install openpanel-sdk
17
17
  ```
18
18
 
19
- ## Usage
19
+ ## Usage example
20
20
 
21
- TODO: Write usage instructions here
21
+ Use the gem by adding the following line to the Gemfile:
22
+
23
+ ```ruby
24
+ tracker = OpenPanel::SDK::Tracker.new
25
+ identify_user = OpenPanel::SDK::IdentifyUser.new
26
+
27
+ # ... set user props
28
+
29
+ tracker.identify identify_user
30
+ tracker.track 'test_event', payload: { name: 'test' }
31
+ tracker.increment_property identify_user, 'test_property', 1
32
+ tracker.decrement_property identify_user, 'test_property', 1
33
+ ```
22
34
 
23
35
  ## Development
24
36
 
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenPanel
2
4
  module SDK
3
5
  class IdentifyUser
4
6
  attr_accessor :profile_id, :email, :first_name, :last_name, :properties
5
7
 
6
- def to_json
8
+ def to_json(*_args)
7
9
  {
8
10
  profileId: profile_id,
9
11
  email: email,
@@ -1,26 +1,30 @@
1
- require "faraday"
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
2
4
 
3
5
  module OpenPanel
4
6
  module SDK
5
7
  class OpenPanelError < StandardError; end
6
8
 
7
9
  class Tracker
8
- TRACKING_TYPE_IDENTIFY = "identify"
9
- TRACKING_TYPE_TRACK = "track"
10
- TRACKING_TYPE_INCREMENT = "increment"
11
- TRACKING_TYPE_DECREMENT = "decrement"
10
+ TRACKING_TYPE_IDENTIFY = 'identify'
11
+ TRACKING_TYPE_TRACK = 'track'
12
+ TRACKING_TYPE_INCREMENT = 'increment'
13
+ TRACKING_TYPE_DECREMENT = 'decrement'
12
14
 
13
15
  attr_reader :headers
14
16
 
15
17
  ##############################
16
18
  # Initialize OpenPanel tracker
19
+ # @param disabled [Boolean] disable sending tracking events, defaults to false
17
20
  ##############################
18
- def initialize
21
+ def initialize(disabled: false)
19
22
  @headers = {
20
- "Content-Type" => "application/json",
21
- "openpanel-client-id" => ENV["OPENPANEL_CLIENT_ID"],
22
- "openpanel-client-secret" => ENV["OPENPANEL_CLIENT_SECRET"],
23
+ 'Content-Type' => 'application/json',
24
+ 'openpanel-client-id' => ENV['OPENPANEL_CLIENT_ID'],
25
+ 'openpanel-client-secret' => ENV['OPENPANEL_CLIENT_SECRET']
23
26
  }
27
+ @disabled = disabled
24
28
  end
25
29
 
26
30
  ##############################
@@ -52,9 +56,9 @@ module OpenPanel
52
56
  ##############################
53
57
  def track_page_view(user, path)
54
58
  if user
55
- track "view", payload: { profileId: user.profile_id, path: path }
59
+ track 'view', payload: { profileId: user.profile_id, path: path }
56
60
  else
57
- track "view", payload: { path: path }
61
+ track 'view', payload: { path: path }
58
62
  end
59
63
  end
60
64
 
@@ -64,13 +68,20 @@ module OpenPanel
64
68
  # @param user [OpenPanel::SDK::IdentifyUser] user to identify
65
69
  ##############################
66
70
  def identify(user)
67
- payload = { profileId: user.profile_id, firstName: user.first_name, lastName: user.last_name, email: user.email }
71
+ payload = { profileId: user.profile_id, firstName: user.first_name, lastName: user.last_name,
72
+ email: user.email }
68
73
  payload = { type: TRACKING_TYPE_IDENTIFY, payload: payload }
69
74
 
70
75
  send_request payload: payload
71
76
  end
72
77
 
73
- def increment_property(user, property = "visits", value = 1)
78
+ ##############################
79
+ # Decrement property in OpenPanel
80
+ # @param user [User] user to identify
81
+ # @param property [String] property to increment
82
+ # @param value [Integer] value to increment by
83
+ #############################
84
+ def increment_property(user, property = 'visits', value = 1)
74
85
  payload = { profileId: user.profile_id, property: property, value: value }
75
86
  payload = { type: TRACKING_TYPE_INCREMENT, payload: payload }
76
87
 
@@ -80,8 +91,10 @@ module OpenPanel
80
91
  ##############################
81
92
  # Decrement property in OpenPanel
82
93
  # @param user [User] user to identify
94
+ # @param property [String] property to increment
95
+ # @param value [Integer] value to decrement by
83
96
  #############################
84
- def decrement_property(user, property = "visits", value = 1)
97
+ def decrement_property(user, property = 'visits', value = 1)
85
98
  payload = { profileId: user.profile_id, property: property, value: value }
86
99
  payload = { type: TRACKING_TYPE_DECREMENT, payload: payload }
87
100
 
@@ -94,17 +107,21 @@ module OpenPanel
94
107
  # Send request to OpenPanel API
95
108
  # @param url [String] API endpoint URL
96
109
  # @param payload [Hash] request payload
110
+ # @return [Faraday::Response] The Faraday plain response object
111
+ # @raise [OpenPanel::SDK::OpenPanelError] if request fails
97
112
  ##############################
98
- def send_request(url: ENV["OPENPANEL_TRACK_URL"], payload: {})
113
+ def send_request(url: ENV['OPENPANEL_TRACK_URL'], payload: {})
114
+ return if @disabled
115
+
99
116
  response = Faraday.post url, payload.to_json, @headers
100
117
 
101
118
  case response.status
102
119
  when 401
103
- raise OpenPanel::SDK::OpenPanelError, "Unauthorized"
120
+ raise OpenPanel::SDK::OpenPanelError, 'Unauthorized'
104
121
  when 429
105
- raise OpenPanel::SDK::OpenPanelError, "Too many requests"
122
+ raise OpenPanel::SDK::OpenPanelError, 'Too many requests'
106
123
  when 500
107
- raise OpenPanel::SDK::OpenPanelError, "Internal server error"
124
+ raise OpenPanel::SDK::OpenPanelError, 'Internal server error'
108
125
  else
109
126
  response
110
127
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenPanel
2
4
  module SDK
3
- VERSION = "0.1.1"
5
+ VERSION = '0.1.2'
4
6
  end
5
7
  end
data/lib/openpanel/sdk.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "sdk/identify_user"
4
- require_relative "sdk/tracker"
5
- require_relative "sdk/version"
3
+ require_relative 'sdk/identify_user'
4
+ require_relative 'sdk/tracker'
5
+ require_relative 'sdk/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openpanel-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Stätter