Class: IntellisenseRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/intellisense-ruby/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

public: Creates a new client

options - Hash

:secret         - String of your project's secret
:max_queue_size - Fixnum of the max calls to remain queued (optional)
:on_error       - Proc which handles error calls from the API


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/intellisense-ruby/client.rb', line 19

def initialize(options = {})

  @queue = Queue.new
  @secret = options[:secret]
  @max_queue_size = options[:max_queue_size] || IntellisenseRuby::Defaults::Queue::MAX_SIZE

  check_api_key

  @consumer = IntellisenseRuby::Consumer.new(@queue, @secret, options)
  @thread = Thread.new { @consumer.run }

  @logger = options[:logger]
  log('intellisense-ruby: SENSOR initialized!')

end

Instance Method Details

#alias(options) ⇒ Object

public: Aliases a user from one id to another

options - Hash

:from      - String of the id to alias from
:to        - String of the id to alias to
:timestamp - Time of when the alias occured (optional)
:context   - Hash of context (optional)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/intellisense-ruby/client.rb', line 196

def alias(options)

  check_api_key

  from = options[:from].to_s
  to = options[:to].to_s
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  ensure_user(from)
  ensure_user(to)
  check_timestamp(timestamp)

  add_context(context)

  enqueue({ from:      from,
            to:        to,
            context:   context,
            timestamp: timestamp.iso8601,
            action:    'alias' })
end

#describe(entity) ⇒ Object

public: describe entities in the graph

entity - Hash

:type       - type of entity (program, course, user, etc.)
:entity_id  - entity Id
:properties - Hash of entity properties (optional, but recommended)
:timestamp  - Time of when the event occurred. (optional)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/intellisense-ruby/client.rb', line 44

def describe(entity)

  log("intellisense-ruby: describing entity #{entity.to_s}")

  check_api_key

  type       = entity[:type]
  entity_id  = entity[:entity_id].to_s
  properties = entity[:properties] || {}
  timestamp  = entity[:timestamp] || Time.new

  check_timestamp(timestamp)

  if type.nil? || type.empty?
    fail ArgumentError, 'Must supply type as a non-empty string'
  end

  if entity_id.nil? || entity_id.empty?
    fail ArgumentError, 'Must supply entity_id as a non-empty string'
  end

  fail ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash

  enqueue({ type:        type,
            entityId:    entity_id,
            properties:  properties,
            timestamp:   timestamp.iso8601,
            action:      'describe',
            apiKey:      @secret})
end

#flushObject

public: Synchronously waits until the consumer has flushed the queue.

Use only for scripts which are not long-running, and will
specifically exit


116
117
118
119
120
# File 'lib/intellisense-ruby/client.rb', line 116

def flush
  while !@queue.empty? || @consumer.is_requesting?
    sleep(0.1)
  end
end

#identify(options) ⇒ Object

public: Identifies a user

options - Hash

:user_id   - String of the user id
:traits    - Hash of user traits. (optional)
:timestamp - Time of when the event occurred. (optional)
:context   - Hash of context. (optional)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/intellisense-ruby/client.rb', line 166

def identify(options)

  check_api_key

  user_id = options[:user_id].to_s
  traits = options[:traits] || {}
  timestamp = options[:timestamp] || Time.new
  context = options[:context] || {}

  ensure_user(user_id)
  check_timestamp(timestamp)

  fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash

  add_context(context)

  enqueue({ userId:    user_id,
            context:   context,
            traits:    traits,
            timestamp: timestamp.iso8601,
            action:    'identify' })
end

#queued_messagesObject

public: Returns the number of queued messages

returns Fixnum of messages in the queue



221
222
223
# File 'lib/intellisense-ruby/client.rb', line 221

def queued_messages
  @queue.length
end

#track(options) ⇒ Object

public: Tracks an event

options - Hash

:event      - String of event name.
:user_id    - String of the user id.
:properties - Hash of event properties. (optional)
:timestamp  - Time of when the event occurred. (optional)
:context    - Hash of context. (optional)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/intellisense-ruby/client.rb', line 84

def track(learning_event)

  log("intellisense-ruby: recording learning event - #{learning_event.to_s}")

  check_api_key

  type       = options[:type]
  entity_id  = options[:entity_id].to_s
  properties = options[:properties] || {}
  timestamp  = options[:timestamp] || Time.new

  check_timestamp(timestamp)

  if type.nil? || type.empty?
    fail ArgumentError, 'Must supply type as a non-empty string'
  end

  fail ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash

  enqueue({ type:      type,
            entityId:     entity_id,
            properties: properties,
            timestamp:  timestamp.iso8601,
            action:     'measure',
            apiKey:      @secret})
end