rtokbox 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,60 @@
1
+ = RTokBox: Gem for accessing the TokBox.com API.
2
+
3
+ See http://rtokbox.rubyforge.org/ for more information.
4
+
5
+ Version 0.1.
6
+
7
+ == Basic Usage
8
+
9
+ === Creating a New User
10
+
11
+ tokb = RTokBox::TokBox.new('API_KEY', 'API_SECRET')
12
+
13
+ # Returns nil if it fails to create the user.
14
+ tokbuser = tokb.register_user('USER', 'FIRST NAME', 'LAST NAME', 'EMAIL', 'PASSWORD')
15
+
16
+ === The Widget Code
17
+
18
+ puts tokbuser.widget_code
19
+
20
+ Just the widget id:
21
+
22
+ puts tokbuser.widget_id
23
+
24
+ Just the TokBox user id:
25
+
26
+ puts tokbuser.user_id
27
+
28
+ Note: You can create an TokBoxUser Instance directly using:
29
+ tokbuser = = RTokBox::TokBoxUser.new(EMAIL, PASSWORD, API_KEY, API_SECRET)
30
+
31
+ == License
32
+
33
+ Copyright (c) 2007, Radu Spineanu
34
+ All rights reserved.
35
+
36
+ Redistribution and use in source and binary forms, with or without modification,
37
+ are permitted provided that the following conditions are met:
38
+
39
+ Redistributions of source code must retain the above copyright notice,
40
+ this list of conditions and the following disclaimer.
41
+
42
+ Redistributions in binary form must reproduce the above copyright notice,
43
+ this list of conditions and the following disclaimer in the documentation
44
+ and/or other materials provided with the distribution.
45
+
46
+ Neither the name of the original author nor the names of contributors
47
+ may be used to endorse or promote products derived from this software
48
+ without specific prior written permission.
49
+
50
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
54
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60
+
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2007 Radu Spineanu
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+
5
+ module RTokBox
6
+
7
+ # The TokBox class can register new users and retrieve TokBoxUser classes.
8
+ # You use it by doing the following:
9
+ # tokb = RTokBox::TokBox.new('API_KEY', 'API_SECRET')
10
+ # tokbuser = tokb.register_user('USER', 'FIRST NAME', 'LAST NAME', 'EMAIL', 'PASSWORD')
11
+ class TokBox
12
+
13
+ # Initializes the TokBox Class.
14
+ def initialize(api_key, api_secret, api_url = 'http://api.tokbox.com/api.php')
15
+ @api_key = api_key
16
+ @api_secret = api_secret
17
+ @api_url = api_url
18
+ end
19
+
20
+ # Registers a new user. Returns nil if the request failed.
21
+ def register_user(username, firstname, lastname, email, password)
22
+ call = 'registerUser'
23
+ call_id = Time.now.to_i.to_s
24
+ cr_password = Digest::MD5.hexdigest(password)
25
+
26
+ msg = @api_key + call + call_id + email + firstname + FORMAT + lastname + cr_password + username + VER
27
+ sig = Digest::MD5.hexdigest(msg + @api_secret)
28
+
29
+ params = {}
30
+ params[:username] = username
31
+ params[:firstname] = firstname
32
+ params[:lastname] = lastname
33
+ params[:email] = email
34
+ params[:password] = cr_password
35
+
36
+ result = request(call, call_id, params, sig)
37
+ if result['success']
38
+ return TokBoxUser.new(email, password, @api_key, @api_secret)
39
+ else
40
+ # raise "Could not create user. (error: #{result['errorCode']})"
41
+ return nil
42
+ end
43
+ end
44
+
45
+ # Returns an instance of TokBoxUser.
46
+ def get_user(email, password)
47
+ return TokBoxUser.new(email, password, @api_key, @api_secret)
48
+ end
49
+
50
+ # Processes the request. Creates the API call and returns a JSON parsed result.
51
+ def request(call, call_id, params, sig, api_key = @api_key, api_url = @api_url)
52
+ rq = "#{api_url}?api_key=#{CGI.escape api_key}&format=#{CGI.escape FORMAT}&v=#{CGI.escape VER}"
53
+ rq += "&sig=#{CGI.escape sig}&call_id=#{CGI.escape call_id}&call=#{CGI.escape call}"
54
+
55
+ params.each_pair do |key, value|
56
+ rq += "&#{key}=#{CGI.escape value}"
57
+ end
58
+
59
+ result = Net::HTTP.get_response(URI.parse(rq)).body
60
+ return JSON.parse(result)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,85 @@
1
+ # Copyright (c) 2007 Radu Spineanu
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+
5
+ module RTokBox
6
+
7
+ # This class creates API calls for different user functions.
8
+ # You get an instance using TokBox.register_user, TokBox.get_user or directly:
9
+ # tokbuser = RTokBox::TokBoxUser.new(EMAIL, PASSWORD, API_KEY, API_SECRET)
10
+ #
11
+ # Returns nil on failure to log in.
12
+ class TokBoxUser < TokBox
13
+ attr_accessor :email, :password, :widget_id, :user_id, :error_code
14
+
15
+ # Initializes the class and retrieves the needed information.
16
+ # Practically validates a login and returns nil if it fails.
17
+ def initialize(email, password, api_key, api_secret, api_url = 'http://api.tokbox.com/api.php')
18
+ @api_key = api_key
19
+ @api_secret = api_secret
20
+ @api_url = api_url
21
+
22
+ self.email = email
23
+ self.password = Digest::MD5.hexdigest(password)
24
+
25
+ self.user_id = get_UserID
26
+ self.widget_id = get_WidgetID
27
+
28
+ unless self.user_id or self.widget_id
29
+ # raise "Could not log in. (error: #{self.error_code})"
30
+ return nul
31
+ end
32
+ end
33
+
34
+ # Retrieves the user's id.
35
+ def get_UserID
36
+ call = 'getUserID'
37
+ call_id = Time.now.to_i.to_s
38
+
39
+ msg = @api_key + call + call_id + self.email + FORMAT + self.password + VER
40
+ sig = Digest::MD5.hexdigest(msg + @api_secret)
41
+
42
+ params = {}
43
+ params[:email] = self.email
44
+ params[:password] = self.password
45
+
46
+ result = request(call, call_id, params, sig, @api_key, @api_url)
47
+ if result['success']
48
+ return result['userid']
49
+ else
50
+ self.error_code = result['errorCode']
51
+ return nil
52
+ end
53
+ end
54
+
55
+ # Retrieves the user's widget id.
56
+ def get_WidgetID
57
+ call = 'getWidgetID'
58
+ call_id = Time.now.to_i.to_s
59
+
60
+ msg = @api_key + call + call_id + self.email + FORMAT + self.password + VER
61
+ sig = Digest::MD5.hexdigest(msg + @api_secret)
62
+
63
+ params = {}
64
+ params[:email] = self.email
65
+ params[:password] = self.password
66
+
67
+ result = request(call, call_id, params, sig, @api_key, @api_url)
68
+ if result['success']
69
+ return result['widgetid']
70
+ else
71
+ self.error_code = result['errorCode']
72
+ return nil
73
+ end
74
+ end
75
+
76
+ # Creates the Widget HTML code for this user.
77
+ def make_Widget
78
+ result = '<object type="application/x-shockwave-flash" data="http://www.tokbox.com/f/' + self.widget_id + '" width="540" height="260">
79
+ <param name="movie" value="http://www.tokbox.com/f/' + self.widget_id + '"></param>
80
+ <param name=FlashVars value="user=' + self.user_id + '&pass=' + self.password + '"></param>
81
+ </object>'
82
+ result
83
+ end
84
+ end
85
+ end
data/lib/rtokbox.rb ADDED
@@ -0,0 +1,20 @@
1
+ # Copyright (c) 2007 Radu Spineanu
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+
5
+ require 'rubygems'
6
+ require 'digest/md5'
7
+ require "cgi"
8
+ require 'net/http'
9
+ require 'json'
10
+ require 'json/ext'
11
+
12
+ # RTokBox module contains a TokBox connection class and a TokBox user class.
13
+ module RTokBox
14
+ FORMAT = "JSON"
15
+ VER = "1.0"
16
+ end
17
+
18
+ require 'rtokbox/tokbox'
19
+ require 'rtokbox/tokbox_user'
20
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: rtokbox
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-11-28 00:00:00 +02:00
8
+ summary: Gem for accessing the TokBox.com API.
9
+ require_paths:
10
+ - lib
11
+ email: radu @nospam@ mypadz.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Radu Spineanu
31
+ files:
32
+ - lib/rtokbox.rb
33
+ - lib/rtokbox/tokbox_user.rb
34
+ - lib/rtokbox/tokbox.rb
35
+ - README
36
+ test_files: []
37
+
38
+ rdoc_options: []
39
+
40
+ extra_rdoc_files:
41
+ - README
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ requirements: []
47
+
48
+ dependencies:
49
+ - !ruby/object:Gem::Dependency
50
+ name: json
51
+ version_requirement:
52
+ version_requirements: !ruby/object:Gem::Version::Requirement
53
+ requirements:
54
+ - - ">"
55
+ - !ruby/object:Gem::Version
56
+ version: 0.0.0
57
+ version: