Class: RTokBox::TokBox

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

Overview

The TokBox class can register new users and retrieve TokBoxUser classes. You use it by doing the following:

tokb = RTokBox::TokBox.new('API_KEY', 'API_SECRET')
tokbuser = tokb.register_user('USER', 'FIRST NAME', 'LAST NAME', 'EMAIL', 'PASSWORD')

Direct Known Subclasses

TokBoxUser

Instance Method Summary collapse

Constructor Details

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

Initializes the TokBox Class.



14
15
16
17
18
# File 'lib/rtokbox/tokbox.rb', line 14

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

Instance Method Details

#get_user(email, password) ⇒ Object

Returns an instance of TokBoxUser.



46
47
48
# File 'lib/rtokbox/tokbox.rb', line 46

def get_user(email, password)
    return TokBoxUser.new(email, password, @api_key, @api_secret)
end

#register_user(username, firstname, lastname, email, password) ⇒ Object

Registers a new user. Returns nil if the request failed.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rtokbox/tokbox.rb', line 21

def register_user(username, firstname, lastname, email, password)
    call = 'registerUser'
    call_id = Time.now.to_i.to_s
    cr_password = Digest::MD5.hexdigest(password)

    msg = @api_key + call + call_id + email + firstname + FORMAT +  lastname + cr_password + username + VER
    sig = Digest::MD5.hexdigest(msg +  @api_secret)

    params = {}
    params[:username]  = username
    params[:firstname] = firstname
    params[:lastname]  = lastname
    params[:email]     = email
    params[:password]  = cr_password

    result = request(call, call_id, params, sig)
    if result['success']
            return TokBoxUser.new(email, password, @api_key, @api_secret)
    else
#                raise "Could not create user. (error: #{result['errorCode']})"
            return nil                
    end
end

#request(call, call_id, params, sig, api_key = @api_key, api_url = @api_url) ⇒ Object

Processes the request. Creates the API call and returns a JSON parsed result.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rtokbox/tokbox.rb', line 51

def request(call, call_id, params, sig, api_key = @api_key, api_url = @api_url)
    rq  = "#{api_url}?api_key=#{CGI.escape api_key}&format=#{CGI.escape FORMAT}&v=#{CGI.escape VER}"
    rq += "&sig=#{CGI.escape sig}&call_id=#{CGI.escape call_id}&call=#{CGI.escape call}"

    params.each_pair do |key, value|
        rq += "&#{key}=#{CGI.escape value}"
    end

    result =  Net::HTTP.get_response(URI.parse(rq)).body
    return JSON.parse(result)
end