Class: RTokBox::TokBoxUser

Inherits:
TokBox
  • Object
show all
Defined in:
lib/rtokbox/tokbox_user.rb

Overview

This class creates API calls for different user functions. You get an instance using TokBox.register_user, TokBox.get_user or directly:

tokbuser = RTokBox::TokBoxUser.new(EMAIL, PASSWORD, API_KEY, API_SECRET)

Returns nil on failure to log in.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TokBox

#get_user, #register_user, #request

Constructor Details

#initialize(email, password, api_key, api_secret, api_url = 'http://api.tokbox.com/api.php') ⇒ TokBoxUser

Initializes the class and retrieves the needed information. Practically validates a login and returns nil if it fails.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rtokbox/tokbox_user.rb', line 17

def initialize(email, password, api_key, api_secret, api_url = 'http://api.tokbox.com/api.php')
    @api_key = api_key
    @api_secret = api_secret
    @api_url = api_url

    self.email = email
    self.password = Digest::MD5.hexdigest(password)

    self.user_id = get_UserID
    self.widget_id = get_WidgetID

    unless self.user_id or self.widget_id
#            raise "Could not log in. (error: #{self.error_code})"
        return nil            
    end 
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



13
14
15
# File 'lib/rtokbox/tokbox_user.rb', line 13

def email
  @email
end

#error_codeObject

Returns the value of attribute error_code.



13
14
15
# File 'lib/rtokbox/tokbox_user.rb', line 13

def error_code
  @error_code
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/rtokbox/tokbox_user.rb', line 13

def password
  @password
end

#user_idObject

Returns the value of attribute user_id.



13
14
15
# File 'lib/rtokbox/tokbox_user.rb', line 13

def user_id
  @user_id
end

#widget_idObject

Returns the value of attribute widget_id.



13
14
15
# File 'lib/rtokbox/tokbox_user.rb', line 13

def widget_id
  @widget_id
end

Instance Method Details

#get_UserIDObject

Retrieves the user’s id.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rtokbox/tokbox_user.rb', line 35

def get_UserID
    call = 'getUserID'
    call_id = Time.now.to_i.to_s

    msg = @api_key + call + call_id + self.email + FORMAT + self.password + VER
    sig = Digest::MD5.hexdigest(msg + @api_secret)

    params = {}
    params[:email] = self.email
    params[:password] = self.password

    result = request(call, call_id, params, sig, @api_key, @api_url)
    if result['success']
        return result['userid']
    else
        self.error_code = result['errorCode']   
        return nil
    end
end

#get_WidgetIDObject

Retrieves the user’s widget id.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rtokbox/tokbox_user.rb', line 56

def get_WidgetID
    call = 'getWidgetID'
    call_id = Time.now.to_i.to_s

    msg = @api_key + call + call_id + self.email + FORMAT + self.password + VER
    sig = Digest::MD5.hexdigest(msg + @api_secret)

    params = {}
    params[:email] = self.email
    params[:password] = self.password

    result = request(call, call_id, params, sig, @api_key, @api_url)
    if result['success']
        return result['widgetid']
    else
        self.error_code = result['errorCode']
        return nil
    end
end

#make_widgetObject

Creates the Widget HTML code for this user.



77
78
79
80
81
82
83
# File 'lib/rtokbox/tokbox_user.rb', line 77

def make_widget
    result = '<object type="application/x-shockwave-flash" data="http://www.tokbox.com/f/' + self.widget_id + '" width="540" height="260">
                     <param name="movie" value="http://www.tokbox.com/f/' + self.widget_id + '"></param>
                     <param name=FlashVars value="user=' + self.user_id + '&pass=' + self.password + '"></param>
              </object>'
    result
end