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.



181
182
183
184
185
# File 'lib/uboost-client/uboost_client.rb', line 181

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



179
180
181
# File 'lib/uboost-client/uboost_client.rb', line 179

def client
  @client
end

#sessionObject

Returns the value of attribute session.



179
180
181
# File 'lib/uboost-client/uboost_client.rb', line 179

def session
  @session
end

#urlObject

Returns the value of attribute url.



179
180
181
# File 'lib/uboost-client/uboost_client.rb', line 179

def url
  @url
end

Instance Method Details



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

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)


191
192
193
# File 'lib/uboost-client/uboost_client.rb', line 191

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

#get(url, options) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/uboost-client/uboost_client.rb', line 204

def get(url, options)
  response = nil
  if !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



195
196
197
# File 'lib/uboost-client/uboost_client.rb', line 195

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

#leaderboard(options) ⇒ Object



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

def leaderboard(options)
  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) ⇒ Object



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

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

#my_badges(options) ⇒ Object



224
225
226
227
228
# File 'lib/uboost-client/uboost_client.rb', line 224

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

#profile(options) ⇒ Object



218
219
220
221
222
# File 'lib/uboost-client/uboost_client.rb', line 218

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

#session_cache_available?Boolean

Returns:

  • (Boolean)


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

def session_cache_available?
  @session
end

#ubar(options = Hash.new) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/uboost-client/uboost_client.rb', line 241

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