SuperRewards 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ == 0.5.1 / 2007-12-17
2
+
3
+ * Initial Release
4
+ * Birthday!
5
+
@@ -0,0 +1,9 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/super_rewards
6
+ lib/super_rewards.rb
7
+ lib/super_rewards/client.rb
8
+ lib/super_rewards/parser.rb
9
+ test/test_super_rewards.rb
@@ -0,0 +1,50 @@
1
+ SuperRewards
2
+ by Shane Vitarana
3
+ http://shanesbrain.net
4
+
5
+ == DESCRIPTION:
6
+
7
+ A Ruby client for the Super Rewards API by KITN Media
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Aims to implement all the functionality of the Super Rewards PHP client
12
+
13
+ == SYNOPSIS:
14
+
15
+ offer_code = SuperRewards::Client.offers_display(:iframe, uid)
16
+ points = SuperRewards::Client.get_points(uids).first.user.points
17
+
18
+ == REQUIREMENTS:
19
+
20
+ * API / Secret keys for the Super Rewards service
21
+ * Shoulda gem (for testing)
22
+
23
+ == INSTALL:
24
+
25
+ * sudo gem install super_rewards
26
+
27
+ == LICENSE:
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2007 Shane Vitarana
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/super_rewards.rb'
6
+
7
+ Hoe.new('SuperRewards', SuperRewards::VERSION) do |p|
8
+ p.rubyforge_name = 'superrewards'
9
+ p.author = 'Shane Vitarana'
10
+ p.email = 'shanev@gmail.com'
11
+ p.summary = 'A Ruby client for the Super Rewards web service'
12
+ p.description = 'A Ruby client for the Super Rewards web service'
13
+ p.url = 'http://shanesbrain.net'
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.remote_rdoc_dir = ''
16
+ end
17
+
18
+ # vim: syntax=Ruby
File without changes
@@ -0,0 +1,13 @@
1
+ require 'net/http'
2
+ require 'rexml/document'
3
+ require 'lib/super_rewards/parser'
4
+ require 'lib/super_rewards/client'
5
+
6
+ module SuperRewards
7
+ VERSION = '0.5.1'
8
+
9
+ SUPERREWARDS_API_KEY = "gsdf7g987sdg987dsfg897sd9g87sdf98g7dfs"
10
+ SUPERREWARDS_SECRET_KEY = "adsfasdf908asdf908asdf09a8dsf09ad8sf09ads8"
11
+ SUPERREWARDS_ENDPOINT = "http://apps.kitnmedia.com/superrewards/service.php"
12
+ SUPERREWARDS_VERSION = "1.0"
13
+ end
@@ -0,0 +1,53 @@
1
+ module SuperRewards
2
+ class Client
3
+ def self.get_required_fields(uid)
4
+ instance.post('superrewards.users.getRequiredFields', :uid => uid)
5
+ end
6
+
7
+ def self.offers_display(mode, uid, user_info=nil)
8
+ if user_info
9
+ instance.post('superrewards.offers.display', :display_mode => mode.to_s, :uid => uid, :user_info => user_info)
10
+ else
11
+ instance.post('superrewards.offers.display', :display_mode => mode.to_s, :uid => uid)
12
+ end
13
+ end
14
+
15
+ def self.get_points(uids)
16
+ instance.post('superrewards.users.getPoints', :uids => uids.join(','))
17
+ end
18
+
19
+ def post(method, params = {})
20
+ params[:method] = method
21
+ params[:api_key] = SUPERREWARDS_API_KEY
22
+ params[:version] = SUPERREWARDS_VERSION
23
+ service.post(params.merge(:sig => signature_for(params)))
24
+ end
25
+
26
+ def self.instance
27
+ @instance ||= self.new
28
+ end
29
+
30
+ def service
31
+ @service ||= Service.new
32
+ end
33
+
34
+ private
35
+ def signature_for(params)
36
+ raw_string = params.inject([]) do |collection, pair|
37
+ collection << pair.join("=")
38
+ collection
39
+ end.sort.join
40
+ Digest::MD5.hexdigest([SUPERREWARDS_SECRET_KEY, raw_string].join)
41
+ end
42
+ end
43
+
44
+ class Service
45
+ def post(params)
46
+ Parser.parse(params[:method], Net::HTTP.post_form(url, params))
47
+ end
48
+ private
49
+ def url
50
+ URI.parse(SUPERREWARDS_ENDPOINT)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,68 @@
1
+ module SuperRewards
2
+ class Parser
3
+ module REXMLElementExtensions
4
+ def text_value
5
+ self.children.first.to_s.strip
6
+ end
7
+ end
8
+ ::REXML::Element.__send__(:include, REXMLElementExtensions)
9
+
10
+ def self.parse(method, data)
11
+ parser = Parser::PARSERS[method]
12
+ parser.process(data)
13
+ end
14
+
15
+ def self.element(name, data)
16
+ data = data.body rescue data # either data or an HTTP response
17
+ doc = REXML::Document.new(data)
18
+ doc.elements.each(name) do |element|
19
+ return element
20
+ end
21
+ raise "Element #{name} not found in #{data}"
22
+ end
23
+ end
24
+
25
+ class GetRequiredFields < Parser
26
+ def self.process(data)
27
+ fields = element('users_getRequiredFields_response', data).text_value
28
+ fields.split(',').map { |f| f.to_sym }
29
+ end
30
+ end
31
+
32
+ class OffersDisplay < Parser
33
+ def self.process(data)
34
+ element('offers_display_response', data).text_value
35
+ end
36
+ end
37
+
38
+ class User
39
+ attr_accessor :uid, :points
40
+ end
41
+
42
+ class GetPoints < Parser
43
+ def self.process(data)
44
+ users = []
45
+ data = data.body rescue data # either data or an HTTP response
46
+ doc = REXML::Document.new(data)
47
+ doc.elements.each("users_getPoints_response/user") do |element|
48
+ user = User.new
49
+ element.each_element("uid") do |uid_element|
50
+ user.uid = uid_element.get_text.to_s
51
+ end
52
+ element.each_element("points") do |points_element|
53
+ user.points = points_element.get_text.to_s.to_i
54
+ end
55
+ users << user
56
+ end
57
+ users
58
+ end
59
+ end
60
+
61
+ class Parser
62
+ PARSERS = {
63
+ 'superrewards.users.getRequiredFields' => GetRequiredFields,
64
+ 'superrewards.offers.display' => OffersDisplay,
65
+ 'superrewards.users.getPoints' => GetPoints
66
+ }
67
+ end
68
+ end
@@ -0,0 +1,110 @@
1
+ require 'test/unit'
2
+ require 'digest/md5'
3
+ require 'cgi'
4
+ require 'rubygems'
5
+ require 'shoulda'
6
+ require 'super_rewards'
7
+
8
+ class TestSuperRewards < Test::Unit::TestCase
9
+
10
+ TEST_UID = "1342314"
11
+
12
+ context "Using curl" do
13
+ setup do
14
+ @params = {}
15
+ @params[:api_key] = SuperRewards::SUPERREWARDS_API_KEY
16
+ @params[:version] = SuperRewards::SUPERREWARDS_VERSION
17
+ end
18
+
19
+ context "for a single user" do
20
+ setup do
21
+ @params[:uid] = TEST_UID
22
+ end
23
+
24
+ should "get required fields" do
25
+ @params[:method] = "superrewards.users.getRequiredFields"
26
+ out = `curl -d "#{curl_params}" #{SuperRewards::SUPERREWARDS_ENDPOINT}`
27
+ assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?><users_getRequiredFields_response>affiliations,birthday,sex,meeting_for</users_getRequiredFields_response>", out
28
+ end
29
+
30
+ should "get offer display code" do
31
+ @params[:method] = "superrewards.offers.display"
32
+ @params[:display_mode] = "iframe"
33
+ # TODO: sending the XML user_info always gives us an invalid signature
34
+ # @params[:user_info] = CGI.escape(user_info.to_s)
35
+ out = `curl -d "#{curl_params}" #{SuperRewards::SUPERREWARDS_ENDPOINT}`
36
+ assert_match(/<offers_display_response>/, out)
37
+ end
38
+ end
39
+
40
+ should "get points for the given users" do
41
+ @params[:method] = "superrewards.users.getPoints"
42
+ @params[:uids] = TEST_UID
43
+ out = `curl -d "#{curl_params}" #{SuperRewards::SUPERREWARDS_ENDPOINT}`
44
+ assert_match(/users_getPoints_response list=\"true\"/, out)
45
+ end
46
+ end
47
+
48
+ context "Using SuperRewards Ruby API" do
49
+ should "get required fields" do
50
+ fields = SuperRewards::Client.get_required_fields(TEST_UID)
51
+ [ :affiliations, :birthday, :sex, :meeting_for ].each do |field|
52
+ assert fields.include?(field)
53
+ end
54
+ end
55
+
56
+ should "get offer display code" do
57
+ offer_code = SuperRewards::Client.offers_display(:iframe, TEST_UID, nil)
58
+ assert_match(/offers.php/, offer_code)
59
+ end
60
+
61
+ should "get points for the given users" do
62
+ uids = [TEST_UID, "1234"]
63
+ users = SuperRewards::Client.get_points(uids)
64
+ assert_equal TEST_UID, users.first.uid
65
+ assert_equal 0, users.first.points
66
+ end
67
+ end
68
+
69
+ private
70
+ def curl_params
71
+ @params.map { |k,v| "#{k}=#{v}" }.join('&') + "&sig=#{signature_for(@params)}"
72
+ end
73
+
74
+ def signature_for(params)
75
+ raw_string = params.inject([]) do |collection, pair|
76
+ collection << pair.join("=")
77
+ collection
78
+ end.sort.join
79
+ Digest::MD5.hexdigest([SuperRewards::SUPERREWARDS_SECRET_KEY, raw_string].join)
80
+ end
81
+
82
+ def user_info
83
+ xml =<<EOF
84
+ <?xml version='1.0' encoding='UTF-8'?>
85
+ <users_getInfo_response xmlns='http://api.facebook.com/1.0/'
86
+ xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
87
+ xsi:schemaLocation='http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd' list='true'>
88
+ <user>
89
+ <uid>8055</uid>
90
+ <affiliations list='true'>
91
+ <affiliation>
92
+ <nid>50453093</nid>
93
+ <name>Facebook Developers</name>
94
+ <type>work</type>
95
+ <status/>
96
+ <year/>
97
+ </affiliation>
98
+ </affiliations>
99
+ <birthday>November 3</birthday>
100
+ <meeting_for list='true'>
101
+ <seeking>Friendship</seeking>
102
+ </meeting_for>
103
+ <sex>male</sex>
104
+ </user>
105
+ </users_getInfo_response>
106
+ EOF
107
+ xml.gsub!(/[\t\r\n]/,'')
108
+ end
109
+
110
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: SuperRewards
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.1
7
+ date: 2007-12-17 00:00:00 -06:00
8
+ summary: A Ruby client for the Super Rewards web service
9
+ require_paths:
10
+ - lib
11
+ email: shanev@gmail.com
12
+ homepage: http://shanesbrain.net
13
+ rubyforge_project: superrewards
14
+ description: A Ruby client for the Super Rewards web service
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
+ - Shane Vitarana
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - bin/super_rewards
37
+ - lib/super_rewards.rb
38
+ - lib/super_rewards/client.rb
39
+ - lib/super_rewards/parser.rb
40
+ - test/test_super_rewards.rb
41
+ test_files:
42
+ - test/test_super_rewards.rb
43
+ rdoc_options:
44
+ - --main
45
+ - README.txt
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.txt
50
+ executables:
51
+ - super_rewards
52
+ extensions: []
53
+
54
+ requirements: []
55
+
56
+ dependencies:
57
+ - !ruby/object:Gem::Dependency
58
+ name: hoe
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Version::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.0
65
+ version: