Class: Cryptid::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptid/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/cryptid/client.rb', line 8

def initialize(options={})
  @conn = options[:conn] if options[:conn]

  if options[:tracker_id]
    @tracker_id = options[:tracker_id]
  else
    @tracker_id = Cryptid.configuration.tracker_id
  end
end

Instance Attribute Details

#connObject



22
23
24
# File 'lib/cryptid/client.rb', line 22

def conn
  @conn ||= ::Excon.new(url)
end

#tracker_idObject



26
27
28
# File 'lib/cryptid/client.rb', line 26

def tracker_id
  @tracker_id or raise 'Missing tracker_id. Set in initializer or Cryptid::Client constructor'
end

Instance Method Details

#build_event_payload(event) ⇒ Object



41
42
43
44
45
# File 'lib/cryptid/client.rb', line 41

def build_event_payload(event)
  {
    event: Helpers.camelize(event.merge(tracker_id: tracker_id))
  }.to_json
end

#headersObject



30
31
32
33
34
35
# File 'lib/cryptid/client.rb', line 30

def headers
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end

#send(event) ⇒ Object



37
38
39
# File 'lib/cryptid/client.rb', line 37

def send(event)
  conn.post(body: build_event_payload(event), headers: headers)
end

#urlObject



18
19
20
# File 'lib/cryptid/client.rb', line 18

def url
  Cryptid.configuration.url
end