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
Assign 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/heap/client.rb', line 41 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
Assign custom properties to an existing user.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/heap/client.rb', line 82 def add_user_properties(identity, properties) ensure_valid_app_id! ensure_valid_properties! properties body = { :app_id => @app_id, :identity => identity.to_s, :properties => properties, } response = connection.post '/api/identify', 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.
132 133 134 |
# File 'lib/heap/client.rb', line 132 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`.
189 190 191 |
# File 'lib/heap/client.rb', line 189 def new(*args) HeapAPI::Client.new(*args) end |
#track(event, identity, properties = nil) ⇒ HeapAPI::Client
Sends a custom event to the Heap API servers.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/heap/client.rb', line 107 def track(event, identity, properties = nil) ensure_valid_app_id! event_name = event.to_s ensure_valid_event_name! event_name 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 |