Class: HeapAPI::Client
- Inherits:
-
Object
- Object
- HeapAPI::Client
- Defined in:
- lib/heap/client.rb,
lib/heap/validations.rb
Overview
Internal methods used to validate API input.
Instance Attribute Summary collapse
-
#app_id ⇒ String
The Heap application ID from heapanalytics.com/app/install.
-
#faraday_adapter ⇒ Symbol
The Faraday adapter used by the Heap API server connection.
-
#faraday_adapter_args ⇒ Array
Arguments to the Faraday adapter used by the Heap API server connection.
-
#stubbed ⇒ Boolean
If true, all the requests to the Heap API are stubbed to return successfully; this overrides the Faraday-related options.
-
#user_agent ⇒ String
The User-Agent header value.
Instance Method Summary collapse
-
#add_user_properties(identity, properties) ⇒ HeapAPI::Client
Assigns custom properties to an existing user.
-
#connection ⇒ Faraday::Connection
The underlying Faraday connection used to make HTTP requests.
-
#initialize(options = {}) ⇒ Client
constructor
Creates a new client for the Heap server-side API.
-
#new(*args) ⇒ Object
Creates a new client instance.
-
#track(event, identity, properties = nil) ⇒ HeapAPI::Client
Sends a custom event to the Heap API servers.
Constructor Details
#initialize(options = {}) ⇒ Client
Creates a new client for the Heap server-side API.
For simplicity, consider using the global client instance referenced by the Heap constant instead of creating and managing new instances.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/heap/client.rb', line 42 def initialize( = {}) @app_id = nil @live_connection = nil @stubbed_connection = false @stubbed = false @faraday_adapter = Faraday.default_adapter @faraday_adapter_args = [] @user_agent = "heap-ruby/#{HeapAPI::VERSION} " + "faraday/#{Faraday::VERSION} ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM})" .each do |key, value| self.send :"#{key}=", value end end |
Instance Attribute Details
#app_id ⇒ String
Returns the Heap application ID from heapanalytics.com/app/install.
8 9 10 |
# File 'lib/heap/client.rb', line 8 def app_id @app_id end |
#faraday_adapter ⇒ Symbol
Returns the Faraday adapter used by the Heap API server connection.
16 17 18 |
# File 'lib/heap/client.rb', line 16 def faraday_adapter @faraday_adapter end |
#faraday_adapter_args ⇒ Array
Returns arguments to the Faraday adapter used by the Heap API server connection.
20 21 22 |
# File 'lib/heap/client.rb', line 20 def faraday_adapter_args @faraday_adapter_args end |
#stubbed ⇒ Boolean
If true, all the requests to the Heap API are stubbed to return successfully; this overrides the Faraday-related options
12 13 14 |
# File 'lib/heap/client.rb', line 12 def stubbed @stubbed end |
#user_agent ⇒ String
Returns the User-Agent header value.
23 24 25 |
# File 'lib/heap/client.rb', line 23 def user_agent @user_agent end |
Instance Method Details
#add_user_properties(identity, properties) ⇒ HeapAPI::Client
Assigns custom properties to an existing user.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/heap/client.rb', line 83 def add_user_properties(identity, properties) ensure_valid_app_id! ensure_valid_identity! identity ensure_valid_properties! properties body = { :app_id => @app_id, :identity => identity.to_s, :properties => properties, } response = connection.post '/api/add_user_properties', body, 'User-Agent' => user_agent raise HeapAPI::ApiError.new(response) unless response.success? self end |
#connection ⇒ Faraday::Connection
The underlying Faraday connection used to make HTTP requests.
135 136 137 |
# File 'lib/heap/client.rb', line 135 def connection @connection ||= @stubbed ? stubbed_connection : live_connection end |
#new(*args) ⇒ Object
Creates a new client instance.
This is defined here so ‘Heap.new` can be used as a shorthand for `HeapAPI::Client.new`.
192 193 194 |
# File 'lib/heap/client.rb', line 192 def new(*args) HeapAPI::Client.new(*args) end |
#track(event, identity, properties = nil) ⇒ HeapAPI::Client
Sends a custom event to the Heap API servers.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/heap/client.rb', line 109 def track(event, identity, properties = nil) ensure_valid_app_id! event_name = event.to_s ensure_valid_event_name! event_name ensure_valid_identity! identity body = { :app_id => @app_id, :identity => identity.to_s, :event => event, } unless properties.nil? body[:properties] = properties ensure_valid_properties! properties end response = connection.post '/api/track', body, 'User-Agent' => user_agent raise HeapAPI::ApiError.new(response) unless response.success? self end |