uboost-client 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -59,6 +59,32 @@ client.points.add_points_to_account(921679359, 30, {:description => 'a descripti
59
59
  client.badges.award(921679359, 1)
60
60
 
61
61
  client.badges.unaward(921679359, 1)
62
+ ```
63
+
64
+ The widgets section can make use of a session store. Just pass a session object - something that quacks like a hash - and the first call it makes will cache the `uboost_session_id`.
65
+
66
+ ````ruby
67
+
68
+ # No caching
69
+ client.widgets.profile(:account_id => 921679373)
70
+ # Caching activated. It will cache the uboost sesion.
71
+ client.widgets(:session => session).profile(:account_id => 921679373)
72
+ # Caching activated. It will use the cached uboost sesion.
73
+ client.widgets(:session => session).profile(:account_id => 921679373)
74
+
75
+ client.widgets.my_badges(:account_id => 921679373)
76
+ client.widgets(:session => session).my_badges(:account_id => 921679373)
77
+ client.widgets(:session => session).my_badges(:account_id => 921679373)
78
+
79
+ client.widgets.ubar(921679373)
80
+
81
+ client.widgets.list_of_leaderboards(:account_id => 921679373)
82
+ client.widgets(:session => session).list_of_leaderboards(:account_id => 921679373)
83
+ client.widgets(:session => session).list_of_leaderboards(:account_id => 921679373)
84
+
85
+ client.widgets.leaderboard(:account_id => 921679373, :leaderboard_id => 226)
86
+ client.widgets(:session => session).leaderboard(:account_id => 921679373, :leaderboard_id => 226)
87
+ client.widgets(:session => session).leaderboard(:account_id => 921679373, :leaderboard_id => 226)
62
88
 
63
89
  client.widgets.profile(921679358)
64
90
 
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'faraday'
3
3
  require 'json'
4
4
  require 'ostruct'
5
+ require 'cgi'
5
6
 
6
7
  module UboostClient
7
8
 
@@ -12,6 +13,7 @@ module UboostClient
12
13
  def initialize(options = Hash.new)
13
14
  @subdomain = options[:subdomain]
14
15
  @api_credentials = options[:api_credentials]
16
+ @debug = options[:debug] || false
15
17
  end
16
18
 
17
19
  def connection
@@ -23,6 +25,15 @@ module UboostClient
23
25
  end
24
26
  end
25
27
 
28
+ def connection_with_uboost_session(uboost_session_id)
29
+ url = "https://#{@subdomain}.uboost.com"
30
+ Faraday.new(url, {:headers=>{'Cookie'=> uboost_session_id}}) do |faraday|
31
+ faraday.request :url_encoded # form-encode POST params
32
+ faraday.response :logger # log requests to STDOUT
33
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
34
+ end
35
+ end
36
+
26
37
  def account
27
38
  @account ||= UboostClient::Account.new(self)
28
39
  end
@@ -35,8 +46,8 @@ module UboostClient
35
46
  @badges ||= UboostClient::Badges.new(self)
36
47
  end
37
48
 
38
- def widgets
39
- @widgets ||= UboostClient::Widgets.new(self)
49
+ def widgets(options = Hash.new)
50
+ @widgets = UboostClient::Widgets.new(self, options)
40
51
  end
41
52
 
42
53
  end
@@ -165,44 +176,75 @@ module UboostClient
165
176
  end
166
177
 
167
178
  class Widgets
168
- attr_accessor :url, :client
179
+ attr_accessor :url, :client, :session
169
180
 
170
- def initialize(client)
181
+ def initialize(client, options)
171
182
  @client = client
172
183
  @url = '/api/widgets'
184
+ @session = options[:session] || false
185
+ end
186
+
187
+ def session_cache_available?
188
+ @session
189
+ end
190
+
191
+ def cached_uboost_id_available?
192
+ @session && @session[:uboost_session_id]
173
193
  end
174
194
 
175
195
  def get_sso_token(account_id)
176
196
  client.account.token(account_id).student["sso_token"]
177
197
  end
178
198
 
179
- def profile(account_id)
180
- response = @client.connection.get @url + '/profile', :sso_token => get_sso_token(account_id)
181
- OpenStruct.new(JSON.parse(response.body))
199
+ def cache_uboost_cookie(response)
200
+ cookie = CGI::Cookie.parse(response.headers['set-cookie'])
201
+ @session[:uboost_session_id] = "_uboost_session_id=" + cookie["_uboost_session_id"][0]
182
202
  end
183
203
 
184
- def my_badges(account_id, badge_category_id = "all")
185
- response = @client.connection.get @url + '/badges/mine/' + badge_category_id, :sso_token => get_sso_token(account_id)
204
+ def get(url, options)
205
+ response = nil
206
+ if !session_cache_available?
207
+ response = @client.connection.get url, :sso_token => get_sso_token(options[:account_id])
208
+ elsif !cached_uboost_id_available?
209
+ response = @client.connection.get url, :sso_token => get_sso_token(options[:account_id])
210
+ cache_uboost_cookie(response)
211
+ elsif cached_uboost_id_available?
212
+ response = @client.connection_with_uboost_session(@session[:uboost_session_id]).get url
213
+ cache_uboost_cookie(response)
214
+ end
215
+ response
216
+ end
217
+
218
+ def profile(options)
219
+ options = {:account_id => nil}.merge(options)
220
+ response = get(@url + '/profile', options)
186
221
  OpenStruct.new(JSON.parse(response.body))
187
222
  end
188
-
189
- def list_of_leaderboards(account_id)
190
- response = @client.connection.get @url + '/leaderboards', :sso_token => get_sso_token(account_id)
223
+
224
+ def my_badges(options)
225
+ options = {:account_id => nil, :badge_category_id => 'all'}.merge(options)
226
+ response = get(@url + '/badges/mine/' + options[:badge_category_id], options)
191
227
  OpenStruct.new(JSON.parse(response.body))
192
228
  end
193
-
194
- def leaderboard(account_id, leaderboard_id)
195
- response = @client.connection.get @url + '/leaderboards/' + leaderboard_id.to_s, :sso_token => get_sso_token(account_id)
196
- OpenStruct.new(JSON.parse(response.body))
229
+
230
+ def list_of_leaderboards(options)
231
+ response = get(@url + '/leaderboards/', options)
232
+ OpenStruct.new(JSON.parse(response.body))
233
+ end
234
+
235
+ def leaderboard(options)
236
+ options = {:account_id => nil, :leaderboard_id => nil}.merge(options)
237
+ response = get(@url + '/leaderboards/' + options[:leaderboard_id].to_s, options)
238
+ OpenStruct.new(JSON.parse(response.body))
197
239
  end
198
240
 
199
241
  def ubar(account_id, options = Hash.new)
200
- options = {:align => "", :bar_color => '', :div_id => ''}.merge(options)
242
+ options = {:align => "", :bar_color => '', :div_id => 'ubar'}.merge(options)
201
243
  token = get_sso_token(account_id)
202
244
  subdomain_url = "http://" + client.subdomain + ".uboost.com"
203
245
 
204
- " <script type='text/javascript' src='#{subdomain_url}/javascripts/uBar.js'></script>
205
- <div style='position: absolute; bottom: 0px; z-index: 2; height: 36px;' id='#{options[:div_id]}'>
246
+ " <script type='text/javascript' src='#{subdomain_url}/javascripts/uBar.js'></script>
247
+ <div class='uboost_ubar' id='#{options[:div_id]}'>
206
248
  <object>
207
249
  <param value='#{subdomain_url}/uBar.swf' name='movie'>
208
250
  <param value='100%' name='width'>
@@ -1,3 +1,3 @@
1
1
  class UboostClient
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
data/spec/simple_test.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require File.expand_path('lib/uboost-client')
2
+ require "pp"
2
3
 
3
4
  subdomain = ENV['UBOOST_SUBDOMAIN']
4
5
  api_credentials = { :username => ENV['UBOOST_USERNAME'], :password => ENV['UBOOST_PASSWORD'] }
6
+ session = Hash.new
5
7
 
6
8
  client = UboostClient::Client.new(:subdomain => subdomain, :api_credentials => api_credentials)
7
9
 
@@ -17,10 +19,21 @@ client = UboostClient::Client.new(:subdomain => subdomain, :api_credentials => a
17
19
  # puts client.points.add_points_to_account(921679359, 30, {:description => 'whatps'})
18
20
  # puts client.badges.award(921679373, 467)
19
21
  # puts client.badges.unaward(921679373, 467)
20
- # puts client.widgets.profile(921679373)
21
- # puts client.widgets.my_badges(921679373)
22
+
23
+ # puts client.widgets.profile(:account_id => 921679373)
24
+ # puts client.widgets(:session => session).profile(:account_id => 921679373)
25
+ # puts client.widgets(:session => session).profile(:account_id => 921679373)
26
+
27
+ # puts client.widgets.my_badges(:account_id => 921679373)
28
+ # puts client.widgets(:session => session).my_badges(:account_id => 921679373)
29
+ # puts client.widgets(:session => session).my_badges(:account_id => 921679373)
30
+
22
31
  # puts client.widgets.ubar(921679373)
23
- # puts client.widgets.list_of_leaderboards(921679373)
24
- puts client.widgets.leaderboard(921679373, 226)
25
- puts client.widgets.leaderboard(921679373, 227)
26
- puts client.widgets.leaderboard(921679373, 228)
32
+
33
+ # puts client.widgets.list_of_leaderboards(:account_id => 921679373)
34
+ # puts client.widgets(:session => session).list_of_leaderboards(:account_id => 921679373)
35
+ # puts client.widgets(:session => session).list_of_leaderboards(:account_id => 921679373)
36
+
37
+ # puts client.widgets.leaderboard(:account_id => 921679373, :leaderboard_id => 226)
38
+ # puts client.widgets(:session => session).leaderboard(:account_id => 921679373, :leaderboard_id => 226)
39
+ # puts client.widgets(:session => session).leaderboard(:account_id => 921679373, :leaderboard_id => 226)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uboost-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday