Class: UboostClient::Widgets

Inherits:
Object
  • Object
show all
Defined in:
lib/uboost-client/uboost_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Widgets

Returns a new instance of Widgets.



186
187
188
189
190
191
# File 'lib/uboost-client/uboost_client.rb', line 186

def initialize(client, options)
  @client = client
  @url = '/api/widgets'
  @session = options[:session] || false
  @credentials = options[:credentials] || false
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



184
185
186
# File 'lib/uboost-client/uboost_client.rb', line 184

def client
  @client
end

#credentialsObject

Returns the value of attribute credentials.



184
185
186
# File 'lib/uboost-client/uboost_client.rb', line 184

def credentials
  @credentials
end

#sessionObject

Returns the value of attribute session.



184
185
186
# File 'lib/uboost-client/uboost_client.rb', line 184

def session
  @session
end

#urlObject

Returns the value of attribute url.



184
185
186
# File 'lib/uboost-client/uboost_client.rb', line 184

def url
  @url
end

Instance Method Details

#badge_categories(options = Hash.new) ⇒ Object



236
237
238
239
# File 'lib/uboost-client/uboost_client.rb', line 236

def badge_categories(options = Hash.new)      
  response = get(@url + '/badge_categories', options)
  OpenStruct.new(JSON.parse(response.body))
end

#badges_for_category(options = Hash.new) ⇒ Object



241
242
243
244
245
# File 'lib/uboost-client/uboost_client.rb', line 241

def badges_for_category(options = Hash.new)
  options = {:account_id => nil, :badge_category_id => nil}.merge(options)
  response = get(@url + '/badge_categories/' + options[:badge_category_id].to_s + '/badges', options)
  OpenStruct.new(JSON.parse(response.body))
end


209
210
211
212
# File 'lib/uboost-client/uboost_client.rb', line 209

def cache_uboost_cookie(response)
  cookie = CGI::Cookie.parse(response.headers['set-cookie'])
  @session[:uboost_session_id] = "_uboost_session_id=" + cookie["_uboost_session_id"][0]
end

#cached_uboost_id_available?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/uboost-client/uboost_client.rb', line 201

def cached_uboost_id_available?
  @session && @session[:uboost_session_id]
end

#credentials_available?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/uboost-client/uboost_client.rb', line 193

def credentials_available?
  @credentials
end

#get(url, options) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/uboost-client/uboost_client.rb', line 214

def get(url, options)
  response = nil
  if credentials_available?
    response = @client.connection(@credentials).get url
  elsif !session_cache_available?
    response = @client.connection.get url, :sso_token => get_sso_token(options[:account_id])
  elsif !cached_uboost_id_available?
    response = @client.connection.get url, :sso_token => get_sso_token(options[:account_id])
    cache_uboost_cookie(response)
  elsif cached_uboost_id_available?
    response = @client.connection_with_uboost_session(@session[:uboost_session_id]).get url
    cache_uboost_cookie(response)
  end
  response
end

#get_sso_token(account_id) ⇒ Object



205
206
207
# File 'lib/uboost-client/uboost_client.rb', line 205

def get_sso_token()
  client..token().student["sso_token"]
end

#leaderboard(options = Hash.new) ⇒ Object



264
265
266
267
268
# File 'lib/uboost-client/uboost_client.rb', line 264

def leaderboard(options = Hash.new)
  options = {:account_id => nil, :leaderboard_id => nil}.merge(options)
  response = get(@url + '/leaderboards/' + options[:leaderboard_id].to_s, options)
  OpenStruct.new(JSON.parse(response.body))
end

#list_of_leaderboards(options = Hash.new) ⇒ Object



259
260
261
262
# File 'lib/uboost-client/uboost_client.rb', line 259

def list_of_leaderboards(options = Hash.new)      
  response = get(@url + '/leaderboards/', options)
  OpenStruct.new(JSON.parse(response.body))
end

#my_badges(options = Hash.new) ⇒ Object



247
248
249
250
251
# File 'lib/uboost-client/uboost_client.rb', line 247

def my_badges(options = Hash.new)
  options = {:account_id => nil, :badge_category_id => 'all'}.merge(options)
  response = get(@url + '/badges/mine/' + options[:badge_category_id].to_s, options)
  OpenStruct.new(JSON.parse(response.body))
end

#profile(options = Hash.new) ⇒ Object



230
231
232
233
234
# File 'lib/uboost-client/uboost_client.rb', line 230

def profile(options = Hash.new)
  options = {:account_id => nil}.merge(options)
  response = get(@url + '/profile', options)
  OpenStruct.new(JSON.parse(response.body))
end

#session_cache_available?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/uboost-client/uboost_client.rb', line 197

def session_cache_available?
  @session
end

#ubar(options = Hash.new) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/uboost-client/uboost_client.rb', line 270

def ubar(options = Hash.new)
  options = {:align => "top", :bar_color => '0x222222', :div_id => 'ubar'}.merge(options)
  token = get_sso_token(options[:account_id])
  subdomain_url = "http://" + client.subdomain + ".uboost.com"

"     <script type='text/javascript' src='#{subdomain_url}/javascripts/uBar.js'></script>
  <div class='uboost_ubar' id='#{options[:div_id]}'>
    <object>
      <param value='#{subdomain_url}/uBar.swf' name='movie'>
      <param value='100%' name='width'>
      <param value='100%' name='height'>
      <param value='transparent' name='wmode'>
      <param value='always' name='allowScriptAccess'>
      <param value='url=#{subdomain_url}/ubar/&token=#{token}&align=#{options[:align]}&barColor=#{options[:bar_color]}&divId=#{options[:div_id]}'
       name='flashvars'>
      <param value='false' name='cacheBusting'>
      <param value='true' name='allowFullScreen'>
      <embed width='1280' height='400' wmode='transparent' allowfullscreen='true' quality='high'
        cachebusting='false' flashvars='url=#{subdomain_url}/ubar/&token=#{token}&align=#{options[:align]}&barColor=#{options[:bar_color]}&divId=#{options[:div_id]}'
        allowscriptaccess='always' src='#{subdomain_url}/uBar.swf'>
    </object>
  </div>"
end

#unearned_badges(options = Hash.new) ⇒ Object



253
254
255
256
257
# File 'lib/uboost-client/uboost_client.rb', line 253

def unearned_badges(options = Hash.new)
  options = {:account_id => nil, :badge_category_id => nil}.merge(options)
  response = get(@url + '/badges/unearned/' + options[:badge_category_id].to_s, options)
  OpenStruct.new(JSON.parse(response.body))
end