Class: PyrxSynapse::Client
- Inherits:
-
Object
- Object
- PyrxSynapse::Client
- Defined in:
- lib/pyrx_synapse/client.rb
Overview
Main Synapse client.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#contacts ⇒ Object
readonly
Returns the value of attribute contacts.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#workspace_id ⇒ Object
readonly
Returns the value of attribute workspace_id.
Instance Method Summary collapse
-
#identify(external_id:, email: nil, phone: nil, first_name: nil, last_name: nil, timezone: nil, locale: nil, properties: nil, tags: nil) ⇒ Object
– Identify ————————————————————.
- #identify_batch(contacts:, on_conflict: nil) ⇒ Object
-
#initialize(api_key:, workspace_id:, base_url: "https://synapse-api.pyrx.tech", timeout: 30, max_retries: 3) ⇒ Client
constructor
A new instance of Client.
-
#request(method, path, body: nil, params: nil) ⇒ Object
Perform an HTTP request with retry logic.
-
#send_email(template_slug:, to:, attributes: nil, idempotency_key: nil) ⇒ Object
– Send —————————————————————-.
-
#track(external_id:, event_name:, attributes: nil, contact: nil, idempotency_key: nil, occurred_at: nil) ⇒ Object
– Track —————————————————————.
- #track_batch(events:) ⇒ Object
Constructor Details
#initialize(api_key:, workspace_id:, base_url: "https://synapse-api.pyrx.tech", timeout: 30, max_retries: 3) ⇒ Client
Returns a new instance of Client.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/pyrx_synapse/client.rb', line 139 def initialize(api_key:, workspace_id:, base_url: "https://synapse-api.pyrx.tech", timeout: 30, max_retries: 3) raise ArgumentError, "Synapse: api_key is required" if api_key.nil? || api_key.strip.empty? raise ArgumentError, "Synapse: workspace_id is required" if workspace_id.nil? || workspace_id.strip.empty? @api_key = api_key @workspace_id = workspace_id @base_url = base_url.chomp("/") @timeout = timeout @max_retries = max_retries @environment = detect_environment(api_key) @contacts = ContactsClient.new(self) @templates = TemplatesClient.new(self) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def base_url @base_url end |
#contacts ⇒ Object (readonly)
Returns the value of attribute contacts.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def contacts @contacts end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def environment @environment end |
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def max_retries @max_retries end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def templates @templates end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def timeout @timeout end |
#workspace_id ⇒ Object (readonly)
Returns the value of attribute workspace_id.
136 137 138 |
# File 'lib/pyrx_synapse/client.rb', line 136 def workspace_id @workspace_id end |
Instance Method Details
#identify(external_id:, email: nil, phone: nil, first_name: nil, last_name: nil, timezone: nil, locale: nil, properties: nil, tags: nil) ⇒ Object
– Identify ————————————————————
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/pyrx_synapse/client.rb', line 198 def identify(external_id:, email: nil, phone: nil, first_name: nil, last_name: nil, timezone: nil, locale: nil, properties: nil, tags: nil) body = { "external_id" => external_id } body["email"] = email if email body["phone"] = phone if phone body["first_name"] = first_name if first_name body["last_name"] = last_name if last_name body["timezone"] = timezone if timezone body["locale"] = locale if locale body["properties"] = properties if properties body["tags"] = if Parsers.parse_contact(request(:post, "/v1/contacts", body: body)) end |
#identify_batch(contacts:, on_conflict: nil) ⇒ Object
213 214 215 216 217 218 |
# File 'lib/pyrx_synapse/client.rb', line 213 def identify_batch(contacts:, on_conflict: nil) body = { "contacts" => contacts } body["on_conflict"] = on_conflict if on_conflict Parsers.parse_bulk_contact(request(:post, "/v1/contacts/bulk", body: body)) end |
#request(method, path, body: nil, params: nil) ⇒ Object
Perform an HTTP request with retry logic.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/pyrx_synapse/client.rb', line 155 def request(method, path, body: nil, params: nil) uri = build_uri(path, params) last_error = nil (@max_retries + 1).times do |attempt| begin response = execute_request(method, uri, body) return handle_response(response) rescue SynapseError => e last_error = e raise unless RETRYABLE_STATUSES.include?(e.status) && attempt < @max_retries sleep(backoff_delay(attempt, e)) rescue *RETRYABLE_EXCEPTIONS => e last_error = e raise if attempt >= @max_retries sleep(backoff_delay(attempt)) end end raise last_error if last_error end |
#send_email(template_slug:, to:, attributes: nil, idempotency_key: nil) ⇒ Object
– Send —————————————————————-
222 223 224 225 226 227 228 |
# File 'lib/pyrx_synapse/client.rb', line 222 def send_email(template_slug:, to:, attributes: nil, idempotency_key: nil) body = { "template_slug" => template_slug, "to" => to } body["attributes"] = attributes if attributes body["idempotency_key"] = idempotency_key if idempotency_key Parsers.parse_send(request(:post, "/v1/send", body: body)) end |
#track(external_id:, event_name:, attributes: nil, contact: nil, idempotency_key: nil, occurred_at: nil) ⇒ Object
– Track —————————————————————
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/pyrx_synapse/client.rb', line 181 def track(external_id:, event_name:, attributes: nil, contact: nil, idempotency_key: nil, occurred_at: nil) body = { "external_id" => external_id, "event_name" => event_name } body["attributes"] = attributes if attributes body["contact"] = contact if contact body["idempotency_key"] = idempotency_key if idempotency_key body["occurred_at"] = occurred_at if occurred_at Parsers.parse_track(request(:post, "/v1/events", body: body)) end |
#track_batch(events:) ⇒ Object
192 193 194 |
# File 'lib/pyrx_synapse/client.rb', line 192 def track_batch(events:) Parsers.parse_batch_track(request(:post, "/v1/events/batch", body: { "events" => events })) end |