Class: UserInfuser

Inherits:
Object
  • Object
show all
Defined in:
lib/userinfuser.rb

Constant Summary collapse

UI_SPATH =
"https://cloudcaptive-userinfuser.appspot.com/api/"
UI_PATH =
"http://cloudcaptive-userinfuser.appspot.com/api/"
LOCAL_TEST =
"http://localhost:8080/api/"
API_VER =
"1"
VALID_WIDGETS =
["trophy_case", "milestones", "notifier", "points", "rank", "availablebadges", "leaderboard"]
UPDATE_USER_PATH =
"updateuser"
GET_USER_DATA_PATH =
"getuserdata"
AWARD_BADGE_PATH =
"awardbadge"
REMOVE_BADGE_PATH =
"removebadge"
AWARD_BADGE_POINTS_PATH =
"awardbadgepoints"
AWARD_POINTS_PATH =
"awardpoints"
WIDGET_PATH =
"getwidget"
CREATE_BADGE_PATH =
"createbadge"
RAISE_EXCEPTIONS =
false
UI_ANONYMOUS =
"__ui__anonymous__"

Instance Method Summary collapse

Constructor Details

#initialize(account, api_key, debug = false, local = false, encrypt = true, sync_all = false) ⇒ UserInfuser

Returns a new instance of UserInfuser.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/userinfuser.rb', line 52

def initialize(, api_key, debug=false, local=false, encrypt=true, sync_all=false)
  @ui_url = UI_PATH
  @ui_url = UI_SPATH if encrypt

  @isGAE = false
  @sync_all = sync_all

  @debug = debug
  log_debug "debug is on, account: #{}, apikey: #{api_key}"

  @api_key = api_key
  raise UserInfuserBadConfiguration if .nil? or api_key.nil?

  @account = 

  @raise_exceptions = RAISE_EXCEPTIONS

  if local
    @ui_url = LOCAL_TEST
    log_debug "Local testing enabled"
    @raise_exceptions = true
  end

  @update_user_path = @ui_url + API_VER + "/" + UPDATE_USER_PATH
  @award_badge_path = @ui_url + API_VER + "/" + AWARD_BADGE_PATH
  @award_badge_points_path = @ui_url + API_VER + "/"+ AWARD_BADGE_POINTS_PATH
  @award_points_path = @ui_url + API_VER + "/"+ AWARD_POINTS_PATH
  @get_user_data_path = @ui_url + API_VER + "/" + GET_USER_DATA_PATH
  @remove_badge_path = @ui_url + API_VER + "/" + REMOVE_BADGE_PATH
  @widget_path = @ui_url + API_VER + "/" + WIDGET_PATH
  @create_badge_path = @ui_url + API_VER + "/" + CREATE_BADGE_PATH

  @timeout = 10 # seconds
end

Instance Method Details

#award_badge(user_id, badge_id, reason = "", resource = "") ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/userinfuser.rb', line 130

def award_badge(user_id, badge_id, reason="", resource="")
  params = { "apikey" => @api_key,
             "accountid" => @account,
             "userid" => user_id,
             "badgeid" => badge_id,
             "resource" => resource,
             "reason" => reason}

  result = nil
  begin
    if @sync_all
      result = post(@award_badge_path, params)
    else
      async_post(@award_badge_path, params)
      return true
    end

    log_debug "Received: #{result}"
  rescue Exception => e
    log_debug "Connection Error"
    raise e if @raise_exceptions
  end

  return parse_return(result)
end

#award_badge_points(user_id, badge_id, points_awarded, points_required, reason = "", resource = "") ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/userinfuser.rb', line 207

def award_badge_points(user_id, badge_id, points_awarded, points_required, reason="", resource="")
  params = { "apikey" => @api_key,
             "accountid" => @account,
             "userid" => user_id,
             "badgeid" => badge_id,
             "pointsawarded" => points_awarded,
             "pointsrequired" => points_required,
             "reason" => reason,
             "resource" => resource}

  result = nil
  begin
    if @sync_all
      result = post(@award_badge_points_path, params)
    else
      async_post(@award_badge_points_path, params)
      return true
    end

    log_debug "Received: #{result}"
  rescue Exception => e
    log_debug "Connection Error" 
    raise e if @raise_exceptions
  end

  return parse_return(result)
end

#award_points(user_id, points_awarded, reason = "") ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/userinfuser.rb', line 181

def award_points(user_id, points_awarded, reason="")
  params = { "apikey" => @api_key,
             "accountid" => @account,
             "userid" => user_id,
             "pointsawarded" => points_awarded,
             "reason" => reason
           }

  result = nil
  begin
    if @sync_all
      result = post(@award_points_path, params)
    else
      async_post(@award_points_path, params)
      return true
    end

    log_debug "Received #{result}" 
  rescue Exception => e
    log_debug "Connection Error"
    raise e if @raise_exceptions
  end

  return parse_return(result)
end

#get_user_data(id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/userinfuser.rb', line 87

def get_user_data(id)
  params = { "apikey" => @api_key,
             "userid" => id,
             "accountid" => @account
           }

  result = '{"status":"failed"}'

  begin
    result = post(@get_user_data_path, params)
    log_debug "Received #{result}"
  rescue Exception => e
    log_debug "Connection Error"
    raise e if @raise_exceptions
  end

  begin
    result = JSON.parse(result)
  rescue
    log_debug "Unable to parse return message"
  end

  return result
end

#get_widget(user_id, widget_type, height = 500, width = 300) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/userinfuser.rb', line 235

def get_widget(user_id, widget_type, height=500, width=300)
  raise UserInfuserUnknownWidget unless VALID_WIDGETS.include?(widget_type)
  
  user_id = UI_ANONYMOUS if user_id.nil?
  user_id = UI_ANONYMOUS if user_id.empty?
   
  userhash = Digest::SHA1.hexdigest(@account + '---' + user_id)
  prefetch_widget(widget_type, user_id)
  if widget_type == "notifier"
    return "<div style='z-index:9999; overflow: hidden; position: fixed; bottom: 0px; right: 10px;'><iframe style='border:none;' allowtransparency='true' height='#{height}px' width='#{width}px' scrolling='no' src='#{@widget_path}?widget=#{widget_type}&u=#{userhash}&height=#{height}&width=#{width}'>Sorry your browser does not support iframes!</iframe></div>"
  else
    return "<iframe border='0' z-index:9999; frameborder='0' height='#{height}px' width='#{width}px' allowtransparency='true' scrolling='no' src='#{@widget_path}?widget=#{widget_type}&u=#{userhash}&height=#{height}&width=#{width}'>Sorry your browser does not support iframes!</iframe>"
  end
end

#remove_badge(user_id, badge_id) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/userinfuser.rb', line 156

def remove_badge(user_id, badge_id)
  params = {"apikey" => @api_key,
             "accountid" => @account,
             "userid" => user_id,
             "badgeid" => badge_id
           }

  result = nil
  begin
    if @sync_all
      result = post(@remove_badge_path, params)
    else
      async_post(@remove_badge_path, params)
      return true
    end

    log_debug "Received: #{result}"
  rescue Exception => e
    log_debug "Connection Error"
    raise e if @raise_exceptions
  end

  return parse_return(result)
end

#update_user(id, name, link_to_profile, link_to_profile_image) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/userinfuser.rb', line 112

def update_user(id, name, link_to_profile, link_to_profile_image)
  params = { "apikey" => @api_key,
             "userid" => id,
             "accountid" => @account,
             "profile_name" => name,
             "profile_link" => link_to_profile,
             "profile_img" => link_to_profile_image
           }

  if @sync_all
    result = post(@update_user_path, params)
    return parse_return(result)
  else
    async_post(@update_user_path, params)
    return true
  end
end