Class: Ravelin::Client

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

Direct Known Subclasses

ProxyClient

Constant Summary collapse

API_BASE =
'https://api.ravelin.com'

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_version: 2) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ravelin/client.rb', line 14

def initialize(api_key:, api_version: 2)
  @api_key = api_key
  @url_prefix = ''

  raise ArgumentError.new("api_version must be 2 or 3") unless [2,3].include? api_version
  @api_version = api_version

  @connection = Faraday.new(API_BASE, faraday_options) do |conn|
    conn.response :json, context_type: /\bjson$/
    conn.adapter Ravelin.faraday_adapter
  end
end

Instance Method Details

#delete_tag(**args) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ravelin/client.rb', line 52

def delete_tag(**args)
  tag = Tag.new(**args).serializable_hash
  customer_id = tag["customerId"]
  tags = tag["tagNames"].join(",")

  delete("#{@url_prefix}/v#{@api_version}/tag/customer?customerId=#{customer_id}&tagName=#{tags}")
end

#get_tag(**args) ⇒ Object



60
61
62
63
64
65
# File 'lib/ravelin/client.rb', line 60

def get_tag(**args)
  tag = Tag.new(**args).serializable_hash
  customer_id = tag["customerId"]

  get("#{@url_prefix}/v#{@api_version}/tag/customer/#{customer_id}")
end

#send_backfill_event(**args) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/ravelin/client.rb', line 36

def send_backfill_event(**args)
  unless args.has_key?(:timestamp)
    raise ArgumentError.new('missing parameters: timestamp')
  end

  event = Event.new(**args)

  post("#{@url_prefix}/v#{@api_version}/backfill/#{event.name}", event.serializable_hash)
end

#send_event(**args) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ravelin/client.rb', line 27

def send_event(**args)
  score = args.delete(:score)
  event = Event.new(**args)

  score_param = score ? "?score=true" : nil

  post("#{@url_prefix}/v#{@api_version}/#{event.name}#{score_param}", event.serializable_hash)
end

#send_tag(**args) ⇒ Object



46
47
48
49
50
# File 'lib/ravelin/client.rb', line 46

def send_tag(**args)
  tag = Tag.new(**args)

  post("#{@url_prefix}/v#{@api_version}/tag/customer", tag.serializable_hash)
end