beintoo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/CHANGELOG +3 -0
- data/Gemfile +3 -0
- data/README.md +6 -0
- data/Rakefile +6 -0
- data/beintoo.gemspec +29 -0
- data/lib/beintoo/api_exception.rb +6 -0
- data/lib/beintoo/app.rb +20 -0
- data/lib/beintoo/model_additions.rb +47 -0
- data/lib/beintoo/player.rb +102 -0
- data/lib/beintoo/railtie.rb +9 -0
- data/lib/beintoo/user.rb +84 -0
- data/lib/beintoo/version.rb +3 -0
- data/lib/beintoo/vgood.rb +90 -0
- data/lib/beintoo.rb +179 -0
- data/lib/generators/beintoo_generator.rb +13 -0
- data/lib/generators/templates/beintoo.rb +12 -0
- data/script/console +1 -0
- data/spec/beintoo/beintoo_player_spec.rb +49 -0
- data/spec/beintoo/beintoo_user_spec.rb +27 -0
- data/spec/beintoo/model_additions_spec.rb +49 -0
- data/spec/beintoo/real_usage_spec.rb +24 -0
- data/spec/spec_helper.rb +23 -0
- metadata +160 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
data/beintoo.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "beintoo/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "beintoo"
|
7
|
+
s.version = Beintoo::VERSION
|
8
|
+
s.authors = ["Enrico Carlesso", "Mattia Gheda"]
|
9
|
+
s.email = ["enricocarlesso@gmail.com", "ghedamat@gmail.com"]
|
10
|
+
s.homepage = "http://www.beintoo.com"
|
11
|
+
s.summary = "Ruby Wrapper for the Beintoo API"
|
12
|
+
s.description = "This gem tries to wrap the Beintoo API (http://documentation.beintoo.com) to a ruby friendly Module meant to be used within Ruby on Rails applications"
|
13
|
+
|
14
|
+
s.rubyforge_project = "beintoo"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "supermodel"
|
24
|
+
s.add_development_dependency "pry"
|
25
|
+
s.add_runtime_dependency "rest-client"
|
26
|
+
s.add_dependency "rake"
|
27
|
+
s.add_dependency "json"
|
28
|
+
s.add_dependency "activesupport"
|
29
|
+
end
|
data/lib/beintoo/app.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Beintoo
|
2
|
+
class App
|
3
|
+
RESOURCE = "app"
|
4
|
+
|
5
|
+
# returns a map the most delivered vgood with you application and the number those have been erogated.
|
6
|
+
# This call can be very useful because you have visibility on the vgoods which are generating more
|
7
|
+
# revenues for you. You could decide for example to provide a functional advantage to users who converted
|
8
|
+
# one of these goods, for example giving him more energy in case of a game.
|
9
|
+
def topvgood
|
10
|
+
end
|
11
|
+
|
12
|
+
# returns a map with your players and the score of those.
|
13
|
+
def leaderboard
|
14
|
+
end
|
15
|
+
|
16
|
+
# returns a list of contest for your app, you can choose a single contest and to list all or just public contest
|
17
|
+
def contestShow
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Beintoo
|
2
|
+
module ModelAdditions
|
3
|
+
# field is the name of the field used for guid i.e. :id
|
4
|
+
def acts_as_beintoo_user(params = {})
|
5
|
+
|
6
|
+
par = {guid_field: :id, email_field: :email, name_field: :name, nickname_field: :nickname}.merge(params)
|
7
|
+
|
8
|
+
define_method 'beintoo_player' do
|
9
|
+
if @beintoo_player.nil?
|
10
|
+
@beintoo_player = Beintoo::Player.new({guid: self.send(par[:guid_field])})
|
11
|
+
@beintoo_player.login
|
12
|
+
end
|
13
|
+
@beintoo_player
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method 'beintoo_user' do
|
17
|
+
begin
|
18
|
+
if @beintoo_user.nil?
|
19
|
+
@beintoo_user = beintoo_player.user({
|
20
|
+
email: self.send(par[:email_field]),
|
21
|
+
name: self.send(par[:name_field]),
|
22
|
+
nickname: self.send(par[:nickname_field]),
|
23
|
+
sendGreetingsEmail: false
|
24
|
+
})
|
25
|
+
@beintoo_user.create
|
26
|
+
end
|
27
|
+
@beintoo_user
|
28
|
+
rescue Beintoo::UserAlreadyRegisteredException => e
|
29
|
+
Beintoo.get_connect_url self.send(par[:guid_field])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method 'beintoo_get_potential_rewards' do
|
34
|
+
@beintoo_user.potential_rewards
|
35
|
+
end
|
36
|
+
|
37
|
+
define_method 'beintoo_accept_reward' do |reward|
|
38
|
+
@beintoo_user.accept_reward(reward)
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method 'beintoo_list_rewards' do
|
42
|
+
@beintoo_user.list_rewards
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Beintoo
|
2
|
+
class Player
|
3
|
+
RESOURCE = "player"
|
4
|
+
attr_accessor :guid, :logged_in, :b_user, :score
|
5
|
+
|
6
|
+
def initialize(params = {})
|
7
|
+
self.guid = params[:guid]
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def user(params = {})
|
12
|
+
raise Beintoo::ApiException, "No guid set when creating new Beintoo::User" if guid.nil?
|
13
|
+
if b_user.nil?
|
14
|
+
self.b_user = Beintoo::User.new params.merge(guid: guid)
|
15
|
+
end
|
16
|
+
b_user
|
17
|
+
end
|
18
|
+
|
19
|
+
def login(_guid = nil, publicname = nil)
|
20
|
+
self.guid = _guid unless _guid.nil?
|
21
|
+
headers = {}
|
22
|
+
headers[:guid] = guid unless guid.nil?
|
23
|
+
headers = Beintoo::build_headers headers
|
24
|
+
|
25
|
+
params_get = {}
|
26
|
+
params_get[:language] = 1
|
27
|
+
params_get[:publicname] = publicname unless publicname.nil?
|
28
|
+
result = Beintoo::get "#{RESOURCE}/login", headers, params_get
|
29
|
+
self.guid = result["guid"]
|
30
|
+
self.logged_in = true
|
31
|
+
if result.has_key? "user"
|
32
|
+
self.b_user = Beintoo::User.new result["user"].merge(guid: guid)
|
33
|
+
self.b_user.created = true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def logged_in?
|
38
|
+
!!logged_in
|
39
|
+
end
|
40
|
+
|
41
|
+
def logout
|
42
|
+
return true if !logged_in?
|
43
|
+
headers = {}
|
44
|
+
headers[:guid] = guid
|
45
|
+
headers = Beintoo::build_headers headers
|
46
|
+
|
47
|
+
result = Beintoo::get "#{RESOURCE}/logout", headers
|
48
|
+
self.logged_in = false
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_player(_guid = nil)
|
52
|
+
self.guid = _guid unless _guid.nil?
|
53
|
+
raise Beintoo::ApiException, "No guid setted when calling get_player" if guid.nil?
|
54
|
+
headers = Beintoo::build_headers
|
55
|
+
result = Beintoo::get "#{RESOURCE}/byguid/#{guid}", headers
|
56
|
+
if result.has_key? :playerScore
|
57
|
+
self.score = result[:playerScore]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def submit_score(score, code_id = nil, balance = nil)
|
62
|
+
raise Beintoo::ApiException, "User not logged in when trying to submit score" unless logged_in?
|
63
|
+
headers = {
|
64
|
+
guid: guid,
|
65
|
+
}
|
66
|
+
headers[:codeID] = code_id unless code_id.nil?
|
67
|
+
headers = Beintoo::build_headers headers
|
68
|
+
body = {lastScore: score}
|
69
|
+
body[:balance] = balance unless balance.nil?
|
70
|
+
if Beintoo.debug
|
71
|
+
Beintoo.log "Submitting score of #{score} for player with guid #{guid}"
|
72
|
+
Beintoo.log body.inspect
|
73
|
+
end
|
74
|
+
result = Beintoo::get "#{RESOURCE}/submitscore/", headers, body
|
75
|
+
if result.has_key? :playerScore
|
76
|
+
self.score = result[:playerScore]
|
77
|
+
end
|
78
|
+
if result[:message] == "OK" && result[:messageID] == 0
|
79
|
+
true
|
80
|
+
else
|
81
|
+
false
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def last_score(contest = :default)
|
86
|
+
score[contest][:lastscore] rescue nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def best_score(contest = :default)
|
90
|
+
score[contest][:bestscore] rescue nil
|
91
|
+
end
|
92
|
+
|
93
|
+
def balance(contest = :default)
|
94
|
+
score[contest][:balance] rescue nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def assign_reward
|
98
|
+
Beintoo::Vgood.byplayer(self)
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
data/lib/beintoo/user.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module Beintoo
|
2
|
+
class User
|
3
|
+
RESOURCE = "user"
|
4
|
+
ATTRIBUTES = [:email, :address, :country, :gender, :nickname, :name, :password, :sendGreetingsEmail, :guid, :id, :userimg, :usersmallimg, :isverified, :lastupdate, :level, :bedollars, :bescore, :userExt]
|
5
|
+
|
6
|
+
ATTRIBUTES.each do |a|
|
7
|
+
self.send("attr_accessor", a)
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :created
|
11
|
+
|
12
|
+
def initialize(params = {})
|
13
|
+
set_data params
|
14
|
+
end
|
15
|
+
|
16
|
+
def create(params = {})
|
17
|
+
set_data params
|
18
|
+
return self if created?
|
19
|
+
if valid?
|
20
|
+
headers = {
|
21
|
+
guid: guid
|
22
|
+
}
|
23
|
+
headers = Beintoo::build_headers headers
|
24
|
+
result = Beintoo::post "#{RESOURCE}/set", headers, get_user_params
|
25
|
+
set_data result
|
26
|
+
self.created = true
|
27
|
+
self
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def created?
|
34
|
+
created
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid?
|
38
|
+
!email.nil? && !guid.nil?
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
h = {}
|
43
|
+
ATTRIBUTES.each do |a|
|
44
|
+
h[a] = self.send(a)
|
45
|
+
end
|
46
|
+
h
|
47
|
+
end
|
48
|
+
|
49
|
+
##### rewards methods
|
50
|
+
|
51
|
+
# Get a list of POTENTIAL rewards for the user
|
52
|
+
def potential_rewards
|
53
|
+
Beintoo::Vgood.byuser(self)
|
54
|
+
end
|
55
|
+
|
56
|
+
# User can accept a given reward previously fetched with self.byuser
|
57
|
+
def accept_reward(reward)
|
58
|
+
Beintoo::Vgood.accept(self, reward)
|
59
|
+
end
|
60
|
+
|
61
|
+
# return the vgoods owned by the current user not yet converted.
|
62
|
+
def list_rewards
|
63
|
+
Beintoo::Vgood.showbyuser(self)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
##### private
|
68
|
+
def set_data(params = {})
|
69
|
+
params = params.with_indifferent_access
|
70
|
+
ATTRIBUTES.each do |at|
|
71
|
+
self.send("#{at}=", params[at]) unless params[at].nil?
|
72
|
+
end
|
73
|
+
get_user_params
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_user_params
|
77
|
+
params = {}
|
78
|
+
ATTRIBUTES.each do |at|
|
79
|
+
params[at] = self.send("#{at}") unless self.send(at).nil?
|
80
|
+
end
|
81
|
+
params
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Beintoo
|
2
|
+
class Vgood
|
3
|
+
RESOURCE = "vgood"
|
4
|
+
|
5
|
+
ATTRIBUTES = %w{getRealURL showURL description descriptionSmall id imageUrl imageSmallUrl startdate enddate name bedollars discount VgoodPoiWrapper whoAlsoConverted categories isBanner contentType content rewardText shareURL uvext}
|
6
|
+
|
7
|
+
ATTRIBUTES.each do |a|
|
8
|
+
self.send("attr_accessor", a)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(params = {})
|
12
|
+
params.each do |a,v|
|
13
|
+
self.send("#{a}=", v)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
ATTRIBUTES.inject(''){|s,a| s << "#{a}: #{self.send(a).to_s}\n"}
|
19
|
+
end
|
20
|
+
|
21
|
+
def == (other)
|
22
|
+
self.id == other.id
|
23
|
+
end
|
24
|
+
|
25
|
+
##### VGood api wrapped methods see http://documentation.beintoo.com/home/api-docs/resources/vgood
|
26
|
+
|
27
|
+
# return the vgoods owned by the current user not yet converted.
|
28
|
+
def self.showbyuser(user = nil)
|
29
|
+
raise Beintoo::ApiException, "Called Vgood.byuser without passing a user!" if user.nil?
|
30
|
+
headers = Beintoo::build_headers
|
31
|
+
result = Beintoo::get "#{RESOURCE}/show/byuser/#{user.id}", headers
|
32
|
+
vgoods = []
|
33
|
+
result.each do |vgood|
|
34
|
+
vgoods << Beintoo::Vgood.new(vgood)
|
35
|
+
end
|
36
|
+
vgoods
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get a list of POTENTIAL rewards for the user
|
40
|
+
# Should return an array of Beintoo::Vgood objects
|
41
|
+
def self.byuser(user = nil)
|
42
|
+
raise Beintoo::ApiException, "Called Vgood.byuser without passing a user!" if user.nil?
|
43
|
+
headers = Beintoo::build_headers
|
44
|
+
result = Beintoo::get "#{RESOURCE}/byuser/#{user.id}", headers
|
45
|
+
vgoods = []
|
46
|
+
result[:vgoods].each do |vgood|
|
47
|
+
vgoods << Beintoo::Vgood.new(vgood)
|
48
|
+
end
|
49
|
+
vgoods
|
50
|
+
end
|
51
|
+
|
52
|
+
# User can accept a given reward previously fetched with self.byuser
|
53
|
+
def self.accept(user = nil, reward = nil)
|
54
|
+
raise Beintoo::ApiException, "Called Vgood.accept_reward without passing a user!" if user.nil?
|
55
|
+
raise Beintoo::ApiException, "Called Vgood.accept_reward without passing a reward!" if reward.nil?
|
56
|
+
headers = Beintoo::build_headers
|
57
|
+
if reward.is_a? Beintoo::Vgood
|
58
|
+
result = Beintoo::get "#{RESOURCE}/accept/#{reward.id}/#{user.id}", headers
|
59
|
+
else
|
60
|
+
result = Beintoo::get "#{RESOURCE}/accept/#{reward}/#{user.id}", headers
|
61
|
+
end
|
62
|
+
# XXX WE SHOULD DETECT ERRORS HERE
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.byplayer(player = nil)
|
67
|
+
raise Beintoo::ApiException, "Called Vgood.byplayer without passing a player!" if !player.logged_in?
|
68
|
+
headers = Beintoo::build_headers
|
69
|
+
result = Beintoo::get "#{RESOURCE}/byguid/#{player.guid}", headers
|
70
|
+
vgoods = []
|
71
|
+
result[:vgoods].each do |vgood|
|
72
|
+
vgoods << Beintoo::Vgood.new(vgood)
|
73
|
+
end
|
74
|
+
vgoods
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
# Returns a vgood, this method act as Vgood.getByUser but providing player guid instead of userExt.
|
79
|
+
def getVgoodByGuid
|
80
|
+
end
|
81
|
+
|
82
|
+
# return the friends of the given user
|
83
|
+
def convert
|
84
|
+
end
|
85
|
+
|
86
|
+
# return the friends of the given user
|
87
|
+
def sendAsGift
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/beintoo.rb
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
require "beintoo/version"
|
2
|
+
require "beintoo/player"
|
3
|
+
require "beintoo/user"
|
4
|
+
require "beintoo/vgood"
|
5
|
+
require "beintoo/api_exception"
|
6
|
+
require "beintoo/model_additions"
|
7
|
+
require "beintoo/railtie" if defined? Rails
|
8
|
+
require "rest_client"
|
9
|
+
require "json"
|
10
|
+
require "yaml"
|
11
|
+
require "active_support"
|
12
|
+
|
13
|
+
module Beintoo
|
14
|
+
SERVER_URL = "https://api.beintoo.com/api/rest/"
|
15
|
+
SANDBOX_SERVER_URL = "https://sandbox.beintoo.com/api/rest/"
|
16
|
+
|
17
|
+
DEFAULT_HEADER = {
|
18
|
+
'accept' => '*/*',
|
19
|
+
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
|
20
|
+
'X-BEINTOO-SDK-VERSION' => '0.0.1-ruby'
|
21
|
+
}
|
22
|
+
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_accessor :apikey, :sandbox, :debug, :transformer, :redirect_uri, :logger
|
26
|
+
LOG_DEBUG = 0
|
27
|
+
LOG_INFO = 1
|
28
|
+
LOG_WARNING = 2
|
29
|
+
LOG_ERROR = 3
|
30
|
+
|
31
|
+
def configure
|
32
|
+
yield self
|
33
|
+
self.sandbox = false if sandbox != true
|
34
|
+
if defined? Rails
|
35
|
+
self.logger = Rails.logger
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def log(message, level = LOG_DEBUG)
|
40
|
+
begin
|
41
|
+
if defined? Rails
|
42
|
+
case level
|
43
|
+
when LOG_DEBUG
|
44
|
+
self.logger.debug message
|
45
|
+
when LOG_INFO, LOG_WARNING
|
46
|
+
self.logger.info message
|
47
|
+
when LOG_ERROR
|
48
|
+
self.logger.error message
|
49
|
+
end
|
50
|
+
elsif self.logger.is_a?(String) && self.logger == "file"
|
51
|
+
if @log_file.nil?
|
52
|
+
@log_file = File.open 'beintoo_log.log', 'a'
|
53
|
+
end
|
54
|
+
if debug || level >= LOG_INFO
|
55
|
+
@log_file.write message + "\n"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
rescue Exception => e
|
59
|
+
puts e.message
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def build_headers(headers = {})
|
64
|
+
raise Beintoo::ApiException, "Api Key not provided" if Beintoo::apikey.nil?
|
65
|
+
{apikey: apikey, sandbox: sandbox}.merge headers
|
66
|
+
end
|
67
|
+
|
68
|
+
def get(url, headers = {}, params = {})
|
69
|
+
url = "#{Beintoo::SERVER_URL}#{url}"
|
70
|
+
start = Time.now
|
71
|
+
headers.merge! Beintoo::DEFAULT_HEADER
|
72
|
+
url += "?#{hash_to_params(params)}" unless params.empty?
|
73
|
+
|
74
|
+
log "#"*80
|
75
|
+
log " Calling Beintoo Api"
|
76
|
+
log url
|
77
|
+
log headers.inspect
|
78
|
+
log "#"*80
|
79
|
+
|
80
|
+
resource = RestClient::Resource.new url, headers: headers
|
81
|
+
|
82
|
+
log "Connecting GET to #{url} with headers #{headers}"
|
83
|
+
|
84
|
+
begin
|
85
|
+
response = resource.get
|
86
|
+
stop = Time.now
|
87
|
+
http_code = response.code
|
88
|
+
|
89
|
+
log "------ RAW RESULT ------"
|
90
|
+
log response.body
|
91
|
+
log "------ /RAW RESULT ------"
|
92
|
+
|
93
|
+
result = JSON.parse(response.body)
|
94
|
+
result = result.with_indifferent_access if result.is_a? Hash
|
95
|
+
result
|
96
|
+
rescue RestClient::Exception => e
|
97
|
+
log "Exception grabbed, response is", LOG_ERROR
|
98
|
+
log e.response.inspect, LOG_ERROR
|
99
|
+
log e.inspect, LOG_ERROR
|
100
|
+
mex = JSON.parse(e.response)["message"]
|
101
|
+
if mex == "An user with this email already registered"
|
102
|
+
raise Beintoo::UserAlreadyRegisteredException, mex
|
103
|
+
else
|
104
|
+
raise Beintoo::ApiException, mex
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def post(url, headers = {}, params = {})
|
110
|
+
url = "#{Beintoo::SERVER_URL}#{url}"
|
111
|
+
start = Time.now
|
112
|
+
headers.merge! Beintoo::DEFAULT_HEADER
|
113
|
+
ll = {"Content-Type" => nil}
|
114
|
+
headers.merge! ll
|
115
|
+
if params.empty?
|
116
|
+
params = ""
|
117
|
+
end
|
118
|
+
|
119
|
+
log "#"*80
|
120
|
+
log " Calling Beintoo Api"
|
121
|
+
log url
|
122
|
+
log headers.inspect
|
123
|
+
log "#"*80
|
124
|
+
|
125
|
+
resource = RestClient::Resource.new url, headers: headers
|
126
|
+
if debug
|
127
|
+
Beintoo.log "Connecting POST to #{url} with headers #{headers}"
|
128
|
+
Beintoo.log "and posting #{params.inspect}"
|
129
|
+
end
|
130
|
+
begin
|
131
|
+
response = resource.post params
|
132
|
+
|
133
|
+
stop = Time.now
|
134
|
+
http_code = response.code
|
135
|
+
result = JSON.parse(response.body).with_indifferent_access
|
136
|
+
log "RESULT IS #{result.inspect}"
|
137
|
+
result
|
138
|
+
rescue RestClient::Exception => e
|
139
|
+
log "Exception grabbed, response is", LOG_ERROR
|
140
|
+
log e.response.inspect, LOG_ERROR
|
141
|
+
log e.inspect, LOG_ERROR
|
142
|
+
mex = JSON.parse(e.response)["message"]
|
143
|
+
if mex == "An user with this email already registered"
|
144
|
+
raise Beintoo::UserAlreadyRegisteredException, mex
|
145
|
+
else
|
146
|
+
raise Beintoo::ApiException, mex
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def hash_to_params(data = {})
|
152
|
+
out = []
|
153
|
+
data.each do |k, v|
|
154
|
+
if ![Hash, Array].include? v.class
|
155
|
+
out << "#{k.to_s}=#{v.to_s}"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
out.join '&'
|
159
|
+
end
|
160
|
+
|
161
|
+
# generates beintoo-connect url when user creation fails
|
162
|
+
# XXX logged_uri parameter is from php API but don't know yet how it works
|
163
|
+
# see http://documentation.beintoo.com/home/api-docs/user-player-connection
|
164
|
+
# redirect will have param userext=XXXXXXXXXXXXXyouruserGUID
|
165
|
+
def get_connect_url(guid, display=nil, signup="facebook", logged_uri=nil)
|
166
|
+
return false if guid.nil?
|
167
|
+
url = "http://www.beintoo.com/connect.html?guid=#{guid}&apikey=#{self.apikey}&redirect_uri=#{self.redirect_uri}"
|
168
|
+
unless display.nil?
|
169
|
+
url += "&display=#{display}"
|
170
|
+
end
|
171
|
+
url += "&signup=#{signup}"
|
172
|
+
unless logged_uri.nil?
|
173
|
+
url += "&logged_uri=#{logged_uri}"
|
174
|
+
end
|
175
|
+
url
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
|
4
|
+
class BeintooGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
desc "This generator creates the initializer file at config/initializers"
|
9
|
+
def copy_initializer_file
|
10
|
+
copy_file "beintoo.rb", "config/initializers/beintoo.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Beintoo.configure do |config|
|
2
|
+
# Here you have to setup your apikey and the redirect uri used
|
3
|
+
# during the beintoo-connect procedure
|
4
|
+
|
5
|
+
# config.apikey = "1234567890"
|
6
|
+
# config.redirect_uri = "http://localhost:3000"
|
7
|
+
|
8
|
+
# These are useful during debug
|
9
|
+
|
10
|
+
# config.sandbox = true
|
11
|
+
# config.debug = true
|
12
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
RUBYLIB=./lib irb -r './lib/beintoo'
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'BeintooPlayer' do
|
4
|
+
before :each do
|
5
|
+
set_testing_data
|
6
|
+
end
|
7
|
+
|
8
|
+
it "Should login/logout Player" do
|
9
|
+
player = Beintoo::Player.new
|
10
|
+
player.respond_to?(:logout).should be true
|
11
|
+
player.login
|
12
|
+
player.logged_in?.should be true
|
13
|
+
if Beintoo.debug
|
14
|
+
puts player.guid
|
15
|
+
end
|
16
|
+
player.logout
|
17
|
+
player.logged_in?.should be false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "Should get player with guid" do
|
21
|
+
player = Beintoo::Player.new guid: "test"
|
22
|
+
player.login
|
23
|
+
player.get_player
|
24
|
+
end
|
25
|
+
|
26
|
+
it "Should have score" do
|
27
|
+
player = Beintoo::Player.new guid: "test"
|
28
|
+
player.login
|
29
|
+
player.respond_to?(:submit_score).should be true
|
30
|
+
player.submit_score(40)
|
31
|
+
if Beintoo.debug
|
32
|
+
puts player.get_player
|
33
|
+
end
|
34
|
+
player.submit_score(30)
|
35
|
+
if Beintoo.debug
|
36
|
+
puts player.get_player
|
37
|
+
end
|
38
|
+
player.submit_score(50)
|
39
|
+
if Beintoo.debug
|
40
|
+
puts player.get_player
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "Should assign a reward to the Beintoo::Player" do
|
45
|
+
player = Beintoo::Player.new guid: "BeintooGemTest_#{rand(300)}"
|
46
|
+
player.login
|
47
|
+
player.assign_reward
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'BeintooUser' do
|
4
|
+
before :each do
|
5
|
+
set_testing_data
|
6
|
+
end
|
7
|
+
|
8
|
+
it "Should generate a Beintoo::User for a Beintoo::Player" do
|
9
|
+
player = Beintoo::Player.new guid: "test#{rand(300)}"
|
10
|
+
player.login
|
11
|
+
user = player.user.create email: "foo_bar+#{rand(300)}@example.com", name: "Foo Bar", nickname: "foo_bar"
|
12
|
+
if Beintoo.debug
|
13
|
+
puts player.user.get_user_params
|
14
|
+
puts user.inspect
|
15
|
+
end
|
16
|
+
user.id.should_not be nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "Should generate a reward for a Beintoo::User" do
|
20
|
+
player = Beintoo::Player.new guid: "test#{rand(300)}"
|
21
|
+
player.login
|
22
|
+
user = player.user.create email: "foo_bar+#{rand(300)}@example.com", name: "Foo Bar", nickname: "foo_bar"
|
23
|
+
user.potential_rewards
|
24
|
+
user.list_rewards
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User < SuperModel::Base
|
4
|
+
include SuperModel::RandomID
|
5
|
+
extend Beintoo::ModelAdditions
|
6
|
+
acts_as_beintoo_user guid_field: :id, email_field: :email, name_field: :name, nickname_field: :nickname
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Beintoo::ModelAdditions do
|
10
|
+
before :each do
|
11
|
+
set_testing_data
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should create a player with a user" do
|
15
|
+
u = User.create email: "foo_bar+#{rand(1000)}@example.com", name: "Foo Bar", nickname: "foo_bar"
|
16
|
+
u.beintoo_player.class.should be Beintoo::Player
|
17
|
+
u.beintoo_user.class.should be Beintoo::User
|
18
|
+
u.beintoo_user.email.should match(/foo_bar/)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should retrieve a player with his user" do
|
22
|
+
# if player has already a user we should fetch him from beintoo and istantiate it locally
|
23
|
+
u = User.create id: 1234567, email: "foobar@example.com", name: "Foo Bar", nickname: "foo_bar"
|
24
|
+
u.beintoo_player.class.should be Beintoo::Player
|
25
|
+
u.beintoo_user.class.should be Beintoo::User
|
26
|
+
u.beintoo_user.name.should match(/Foo/)
|
27
|
+
u.beintoo_user.id.should_not be nil
|
28
|
+
|
29
|
+
u.respond_to?(:beintoo_get_potential_rewards).should be true
|
30
|
+
rews = u.beintoo_get_potential_rewards
|
31
|
+
|
32
|
+
u.beintoo_list_rewards.should_not include rews.first
|
33
|
+
|
34
|
+
u.respond_to?(:beintoo_accept_reward).should be true
|
35
|
+
u.beintoo_accept_reward(rews.first).should be true # XXX see XXX on vgood.rb
|
36
|
+
|
37
|
+
u.respond_to?(:beintoo_list_rewards).should be true
|
38
|
+
u.beintoo_list_rewards.should include rews.first
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return a beintoo connect url when creating a user with an existing email" do
|
42
|
+
u = User.create id: 22222, email: "foobar@example.com", name: "Mario", nickname: "Rossi"
|
43
|
+
u.beintoo_player.class.should be Beintoo::Player
|
44
|
+
bu = u.beintoo_user
|
45
|
+
bu.class.should be String
|
46
|
+
puts bu
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'BeintooUsage' do
|
4
|
+
before :each do
|
5
|
+
set_real_case_data
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should create a player" do
|
9
|
+
player = Beintoo::Player.new guid: "1"
|
10
|
+
player.login
|
11
|
+
puts player.submit_score(0, nil, 0)
|
12
|
+
sleep 1
|
13
|
+
puts player.get_player
|
14
|
+
sleep 1
|
15
|
+
binding.pry
|
16
|
+
player.last_score.should eq 0.0
|
17
|
+
player.submit_score(30)
|
18
|
+
sleep 1
|
19
|
+
player.last_score.should eq 30.0
|
20
|
+
player.submit_score(40)
|
21
|
+
sleep 1
|
22
|
+
player.last_score.should eq 70.0
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'supermodel'
|
3
|
+
require 'beintoo'
|
4
|
+
require 'rspec'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
def set_testing_data
|
8
|
+
Beintoo.configure do |config|
|
9
|
+
config.apikey = "1234567890"
|
10
|
+
config.redirect_uri = "http://localhost:3000"
|
11
|
+
config.sandbox = true
|
12
|
+
config.debug = true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_real_case_data
|
17
|
+
# Beintoo.configure do |config|
|
18
|
+
# config.apikey = ""
|
19
|
+
# config.sandbox = false
|
20
|
+
# config.debug = true
|
21
|
+
# end
|
22
|
+
pending "Add your config block in spec/spec_helper.rb"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: beintoo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Enrico Carlesso
|
9
|
+
- Mattia Gheda
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-02-15 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &70094414489660 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70094414489660
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: supermodel
|
28
|
+
requirement: &70094414489240 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70094414489240
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: pry
|
39
|
+
requirement: &70094414488820 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70094414488820
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rest-client
|
50
|
+
requirement: &70094414488380 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :runtime
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *70094414488380
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rake
|
61
|
+
requirement: &70094414487960 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :runtime
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *70094414487960
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: json
|
72
|
+
requirement: &70094414487540 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *70094414487540
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: activesupport
|
83
|
+
requirement: &70094414487100 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
type: :runtime
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: *70094414487100
|
92
|
+
description: This gem tries to wrap the Beintoo API (http://documentation.beintoo.com)
|
93
|
+
to a ruby friendly Module meant to be used within Ruby on Rails applications
|
94
|
+
email:
|
95
|
+
- enricocarlesso@gmail.com
|
96
|
+
- ghedamat@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- CHANGELOG
|
104
|
+
- Gemfile
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- beintoo.gemspec
|
108
|
+
- lib/beintoo.rb
|
109
|
+
- lib/beintoo/api_exception.rb
|
110
|
+
- lib/beintoo/app.rb
|
111
|
+
- lib/beintoo/model_additions.rb
|
112
|
+
- lib/beintoo/player.rb
|
113
|
+
- lib/beintoo/railtie.rb
|
114
|
+
- lib/beintoo/user.rb
|
115
|
+
- lib/beintoo/version.rb
|
116
|
+
- lib/beintoo/vgood.rb
|
117
|
+
- lib/generators/beintoo_generator.rb
|
118
|
+
- lib/generators/templates/beintoo.rb
|
119
|
+
- script/console
|
120
|
+
- spec/beintoo/beintoo_player_spec.rb
|
121
|
+
- spec/beintoo/beintoo_user_spec.rb
|
122
|
+
- spec/beintoo/model_additions_spec.rb
|
123
|
+
- spec/beintoo/real_usage_spec.rb
|
124
|
+
- spec/spec_helper.rb
|
125
|
+
homepage: http://www.beintoo.com
|
126
|
+
licenses: []
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
hash: -40742333141001870
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
hash: -40742333141001870
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project: beintoo
|
151
|
+
rubygems_version: 1.8.10
|
152
|
+
signing_key:
|
153
|
+
specification_version: 3
|
154
|
+
summary: Ruby Wrapper for the Beintoo API
|
155
|
+
test_files:
|
156
|
+
- spec/beintoo/beintoo_player_spec.rb
|
157
|
+
- spec/beintoo/beintoo_user_spec.rb
|
158
|
+
- spec/beintoo/model_additions_spec.rb
|
159
|
+
- spec/beintoo/real_usage_spec.rb
|
160
|
+
- spec/spec_helper.rb
|