Class: Altertable::Client
- Inherits:
-
Object
- Object
- Altertable::Client
- Defined in:
- lib/altertable/client.rb,
sig/altertable/client.rbs
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.altertable.ai"- DEFAULT_TIMEOUT =
5- DEFAULT_ENVIRONMENT =
"production"
Instance Method Summary collapse
- #alias(distinct_id, new_user_id = nil, **options) ⇒ Object
- #handle_error(error) ⇒ Object
- #handle_response(res) ⇒ Object
- #identify(user_id, **options) ⇒ Object
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(path, payload) ⇒ Object
- #select_adapter(name, options) ⇒ Object
- #track(event, distinct_id = nil, **options) ⇒ Object
- #try_require(gem_name) ⇒ Boolean
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/altertable/client.rb', line 14 def initialize(api_key, = {}) raise ConfigurationError, "API Key is required" if api_key.nil? || api_key.empty? @api_key = api_key @base_url = [:base_url] || DEFAULT_BASE_URL @environment = [:environment] || DEFAULT_ENVIRONMENT @timeout = [:request_timeout] || DEFAULT_TIMEOUT @release = [:release] @debug = [:debug] || false @on_error = [:on_error] # Initialize adapter adapter_name = [:adapter] headers = { "X-API-Key" => @api_key, "Content-Type" => "application/json" } @adapter = select_adapter(adapter_name, { base_url: @base_url, timeout: @timeout, headers: headers, proxy: [:proxy] }) end |
Instance Method Details
#alias(distinct_id, new_user_id, options) ⇒ Object #alias(alias_payload, options) ⇒ Object #alias(alias_payloads, options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/altertable/client.rb', line 58 def alias(distinct_id, new_user_id = nil, **) case distinct_id when Array payloads = map_batch(distinct_id, "aliases") { |item| alias_payload_from_item(merge_payload(item, )) } post("/alias", payloads) when Hash post("/alias", alias_payload_from_item(merge_payload(distinct_id, ))) else post("/alias", alias_payload(distinct_id, new_user_id, )) end end |
#handle_error(error) ⇒ Object
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/altertable/client.rb', line 228 def handle_error(error) wrapped_error = if error.is_a?(AltertableError) error else AltertableError.new(error., error) end @on_error&.call(wrapped_error) raise wrapped_error end |
#handle_response(res) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/altertable/client.rb', line 208 def handle_response(res) case res.status when 200..299 begin JSON.parse(res.body) rescue StandardError {} end when 422 error_data = begin JSON.parse(res.body) rescue StandardError {} end raise ApiError.new("Unprocessable Entity: #{error_data['message']}", res.status, error_data) else raise ApiError.new("HTTP Error: #{res.status}", res.status) end end |
#identify(user_id, options) ⇒ Object #identify(identify_payload, options) ⇒ Object #identify(identify_payloads, options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/altertable/client.rb', line 46 def identify(user_id, **) case user_id when Array payloads = map_batch(user_id, "identifies") { |item| identify_payload_from_item(merge_payload(item, )) } post("/identify", payloads) when Hash post("/identify", identify_payload_from_item(merge_payload(user_id, ))) else post("/identify", identify_payload(user_id, )) end end |
#post(path, payload) ⇒ Object
201 202 203 204 205 206 |
# File 'lib/altertable/client.rb', line 201 def post(path, payload) res = @adapter.post(path, body: payload.to_json) handle_response(res) rescue StandardError => e handle_error(e) end |
#select_adapter(name, options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/altertable/client.rb', line 72 def select_adapter(name, ) case name when :faraday Adapters::FaradayAdapter.new(**) when :httpx Adapters::HttpxAdapter.new(**) when :net_http Adapters::NetHttpAdapter.new(**) else # Auto-detect if defined?(Faraday) || try_require("faraday") Adapters::FaradayAdapter.new(**) elsif defined?(HTTPX) || try_require("httpx") Adapters::HttpxAdapter.new(**) else Adapters::NetHttpAdapter.new(**) end end end |
#track(event, distinct_id, options) ⇒ Object #track(event_payload, options) ⇒ Object #track(event_payloads, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/altertable/client.rb', line 34 def track(event, distinct_id = nil, **) case event when Array payloads = map_batch(event, "events") { |item| track_payload_from_item(merge_payload(item, )) } post("/track", payloads) when Hash post("/track", track_payload_from_item(merge_payload(event, ))) else post("/track", track_payload(event, distinct_id, )) end end |
#try_require(gem_name) ⇒ Boolean
92 93 94 95 96 97 |
# File 'lib/altertable/client.rb', line 92 def try_require(gem_name) require gem_name true rescue LoadError false end |