Class: Swrve::Api::Events

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/swrve/api/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvents

Returns a new instance of Events.



13
14
15
16
17
# File 'lib/swrve/api/events.rb', line 13

def initialize
  @api_endpoint = Swrve::Middleware::Http.new(Swrve.config.api_url + "/#{Swrve.config.api_version}")
  @app_version  = Swrve.config.app_version
  @api_key      = Swrve.config.api_key
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



9
10
11
# File 'lib/swrve/api/events.rb', line 9

def api_endpoint
  @api_endpoint
end

Instance Method Details

#buy_in(uuid, amount, real_currency_name, reward_amount, reward_currency, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/swrve/api/events.rb', line 46

def buy_in(uuid, amount, real_currency_name, reward_amount, reward_currency, options = {})
  payment_provider = options.delete( :payment_provider ) || "Default Payment Provider"
  swrve_payload = fill_nil_values(options[:swrve_payload] || {})

  response = post('buy_in', query_options(uuid, {cost: amount.to_f, local_currency: real_currency_name,
                                      reward_amount: reward_amount.to_f, reward_currency: reward_currency,
                                      payment_provider: payment_provider, swrve_payload: swrve_payload}))
  handle_response(response)
end

#create_event(uuid, name, payload = {}) ⇒ Object



66
67
68
69
70
71
# File 'lib/swrve/api/events.rb', line 66

def create_event(uuid, name, payload = {})
  params = query_options(uuid, name: name, swrve_payload: fill_nil_values(payload))

  response = post('event', params)
  handle_response(response)
end

#currency_given(uuid, given_amount, given_currency, payload = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/swrve/api/events.rb', line 56

def currency_given(uuid, given_amount, given_currency, payload={})
  validate_amount(given_amount, given_currency)
  payload = fill_nil_values(payload)
  
  response = post('currency_given', query_options(uuid, { given_currency: given_currency, 
                                               given_amount: given_amount,
                                               swrve_payload: payload }))
  handle_response(response)
end

#purchase(uuid, item_id, cost, options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/swrve/api/events.rb', line 39

def purchase(uuid, item_id, cost, options = {})
  options = { item: item_id.to_s, cost: cost.to_f, currency: "USD", quantity: 1}.merge(options)
  
  response = post('purchase', query_options(uuid, options)) 
  handle_response(response)
end

#session_end(uuid, swrve_payload = {}) ⇒ Object



27
28
29
# File 'lib/swrve/api/events.rb', line 27

def session_end(uuid, swrve_payload = {})
  post('session_end', query_options(uuid, swrve_payload: fill_nil_values(swrve_payload)))
end

#session_start(uuid, swrve_payload = {}) ⇒ Object

Starts the user game session

@param [String, #uuid] uuid Unique identifier for the user starting the session
@return [String, swrve_payload (Hash) (defaults to: {}) — A customizable payload associated with the session_start event


22
23
24
25
# File 'lib/swrve/api/events.rb', line 22

def session_start(uuid, swrve_payload = {})
  response = post('session_start', query_options(uuid, swrve_payload: fill_nil_values(swrve_payload)))
  handle_response(response)
end

#update_user(uuid, user_attributes = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/swrve/api/events.rb', line 31

def update_user(uuid, user_attributes = {})
  params = { user_initiated: (user_attributes.delete(:user_initiated) || true) }
  params.merge!(swrve_payload: fill_nil_values(user_attributes.delete(:swrve_payload) || {}))
  
  response = post('user', query_options(uuid, user_attributes.merge(params)))
  handle_response(response)
end