Class: Korboard::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Client

Returns a new instance of Client.



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

def initialize params


  @token = params[:api_secret]
  @iteration_number = params[:iteration]
  @http = Net::HTTP.new('api.korboard.com')
  @headers = {
    'Content-Type' => 'application/json'
  }
end

Instance Method Details



68
69
70
71
72
73
# File 'lib/korboard/client.rb', line 68

def print_response(resp,data)
  puts 'Code = ' + resp.code
  puts 'Message = ' + resp.message
  resp.each {|key, val| puts key + ' = ' + val}
  puts data
end

#record(metric, iteration_number, identifier, options = { }) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/korboard/client.rb', line 47

def record metric ,iteration_number, identifier, options ={ }
  data = { :signup  => { :identifier => identifier}, :iteration => { :number => iteration_number }  }
  data.merge!(options) 
  
  path = "/v1.1/#{metric.to_s.pluralize}?token=#{@token}"

  #wrap in a timeout so we don't slow shit down
  Timeout::timeout(1){
    puts path.inspect
    puts data.to_json.inspect
    resp, data = @http.post(path, data.to_json, @headers)
    print_response(resp,data) unless resp.code.to_i == 201
    return resp.code.to_i == 201
  }
  return false
rescue => e
  puts e.message
end

#record_invite(identifier, options = { }) ⇒ Object



32
33
34
35
# File 'lib/korboard/client.rb', line 32

def record_invite identifier,options={ }
  options.merge! :user_profile => { :session_id => identifier }
  record :invite,@iteration_number,identifier,options
end

#record_paid(identifier, options = { }) ⇒ Object



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

def record_paid identifier,options={ }
  options.merge! :user_profile => { :session_id => identifier }
  record :paid,@iteration_number,identifier,options
end

#record_signup(identifier, session_id, options = { }, parent_user_id = nil) ⇒ Object

can do this for the rest too parent_user_id => user_id_of_invitor can be passed in the options



21
22
23
24
25
# File 'lib/korboard/client.rb', line 21

def  identifier,session_id,options={ },parent_user_id=nil
  options.merge! :signup => { :parent_user_id => parent_user_id} if parent_user_id
  options.merge! :user_profile => { :session_id => session_id , :user_id => identifier}
  record :signup,@iteration_number,identifier,options
end

#record_value(identifier, options = { }) ⇒ Object



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

def record_value identifier,options={ }
  options.merge! :user_profile => { :session_id => identifier }
  record :value,@iteration_number,identifier,options
end

#record_visit(identifier, options = { }) ⇒ Object



27
28
29
30
# File 'lib/korboard/client.rb', line 27

def record_visit identifier,options={ }
  options.merge! :user_profile => { :session_id => identifier }
  record :visit,@iteration_number,identifier,options
end