Class: IActionable::Api

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

Constant Summary collapse

@@settings =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



13
14
15
16
17
18
19
20
# File 'lib/iactionable/api.rb', line 13

def initialize
  if @@settings
    @connection = IActionable::Connection.new(@@settings)
  else
    raise IActionable::ConfigError.new("IActionable::Api cannot be initialized without credentials being set in IActionable::Api.init_settings()")
  end
  @wrap_in_object = true
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/iactionable/api.rb', line 8

def connection
  @connection
end

#wrap_in_objectObject (readonly)

Returns the value of attribute wrap_in_object.



9
10
11
# File 'lib/iactionable/api.rb', line 9

def wrap_in_object
  @wrap_in_object
end

Class Method Details

.init_settings(values) ⇒ Object



22
23
24
25
26
# File 'lib/iactionable/api.rb', line 22

def self.init_settings(values)
  @@settings = IActionable::Settings.new(values)
rescue IActionable::ConfigError => e
  raise e
end

.settingsObject



28
29
30
# File 'lib/iactionable/api.rb', line 28

def self.settings
  @@settings
end

Instance Method Details

#add_profile_identifier(profile_type, id_type, id, alt_id_type, alt_id) ⇒ Object



66
67
68
# File 'lib/iactionable/api.rb', line 66

def add_profile_identifier(profile_type, id_type, id, alt_id_type, alt_id)
  @connection.request.with_app_key.with_api_key.to("/#{profile_type}/#{id_type}/#{id}/identifiers/#{alt_id_type}/#{alt_id}").post
end

#create_profile(profile_type, id_type, id, display_name = nil) ⇒ Object Also known as: update_profile



59
60
61
62
63
# File 'lib/iactionable/api.rb', line 59

def create_profile(profile_type, id_type, id, display_name = nil)
  request = @connection.request.with_app_key.with_api_key.to("/#{profile_type}/#{id_type}/#{id}")
  request.with_params(:displayName => display_name) unless display_name.blank?
  request.post
end

#get_achievementsObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/iactionable/api.rb', line 115

def get_achievements()
  response = @connection.request.with_app_key.to("/achievements").get
  if @wrap_in_object
    response.map{|achievement_json| IActionable::Objects::Achievement.new(achievement_json)}
  else
    response
  end
rescue NoMethodError => e
  []
end

#get_challengesObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/iactionable/api.rb', line 148

def get_challenges()
  response = @connection.request.with_app_key.to("/challenges").get
  if @wrap_in_object
    response.map{|challenge_json| IActionable::Objects::Challenge.new(challenge_json)}
  else
    response
  end
rescue NoMethodError => e
  []
end

#get_goalsObject



181
182
183
184
185
186
187
188
189
190
# File 'lib/iactionable/api.rb', line 181

def get_goals()
  response = @connection.request.with_app_key.to("/goals").get
  if @wrap_in_object
    response.map{|goal_json| IActionable::Objects::Goal.new(goal_json)}
  else
    response
  end
rescue NoMethodError => e
  []
end

#get_leaderboard(profile_type, point_type, leaderboard, page_number = nil, page_count = nil, id = nil, id_type = nil) ⇒ Object

Leaderboard API calls =



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/iactionable/api.rb', line 196

def get_leaderboard(profile_type, point_type, leaderboard, page_number=nil, page_count=nil, id=nil, id_type=nil)
  request = @connection.request.with_app_key.to("/#{profile_type}/leaderboards/points/#{point_type}/#{leaderboard}")
  request.with_params(:pageNumber => page_number) unless page_number.blank?
  request.with_params(:pageCount => page_count) unless page_count.blank?
  request.with_params(:id => id) unless id.blank? || id_type.blank?
  request.with_params(:idType => id_type) unless id.blank? || id_type.blank?
  
  if @wrap_in_object
    IActionable::Objects::LeaderboardReport.new(request.get)
  else
    request.get
  end
end

#get_profile_achievements(profile_type, id_type, id, filter_type = nil) ⇒ Object

Achievement API calls =



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/iactionable/api.rb', line 97

def get_profile_achievements(profile_type, id_type, id, filter_type = nil)
  request = @connection.request.with_app_key
  case filter_type
  when :completed
    request.to("/#{profile_type}/#{id_type}/#{id}/achievements/Completed")
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/achievements/Available")
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/achievements")
  end
  
  if @wrap_in_object
    IActionable::Objects::ProfileAchievements.new(request.get)
  else
    request.get
  end
end

#get_profile_challenges(profile_type, id_type, id, filter_type = nil) ⇒ Object

Challenges API calls =



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/iactionable/api.rb', line 130

def get_profile_challenges(profile_type, id_type, id, filter_type = nil)
  request = @connection.request.with_app_key
  case filter_type
  when :completed
    request.to("/#{profile_type}/#{id_type}/#{id}/challenges/Completed")
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/challenges/Available")
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/challenges")
  end
  
  if @wrap_in_object
    IActionable::Objects::ProfileChallenges.new(request.get)
  else
    request.get
  end
end

#get_profile_goals(profile_type, id_type, id, filter_type = nil) ⇒ Object

Goals API calls =



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/iactionable/api.rb', line 163

def get_profile_goals(profile_type, id_type, id, filter_type = nil)
  request = @connection.request.with_app_key
  case filter_type
  when :completed
    request.to("/#{profile_type}/#{id_type}/#{id}/goals/Completed")
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/goals/Available")
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/goals")
  end
  
  if @wrap_in_object
    IActionable::Objects::ProfileGoals.new(request.get)
  else
    request.get
  end
end

#get_profile_notifications(profile_type, id_type, id) ⇒ Object

Profile Notifications API calls =



214
215
216
217
218
219
220
221
222
# File 'lib/iactionable/api.rb', line 214

def get_profile_notifications(profile_type, id_type, id)
  response = @connection.request.with_app_key.to("/#{profile_type}/#{id_type}/#{id}/notifications").get

  if @wrap_in_object
    IActionable::Objects::ProfileNotifications.new(response)
  else
    response
  end
end

#get_profile_points(profile_type, id_type, id, point_type) ⇒ Object

Points API calls =



74
75
76
77
78
79
80
81
# File 'lib/iactionable/api.rb', line 74

def get_profile_points(profile_type, id_type, id, point_type)
  response = @connection.request.with_app_key.to("/#{profile_type}/#{id_type}/#{id}/points/#{point_type}").get
  if @wrap_in_object
    IActionable::Objects::ProfilePoints.new(response)
  else
    response
  end
end

#get_profile_summary(profile_type, id_type, id, achievement_count = nil) ⇒ Object

Profile API calls =



49
50
51
52
53
54
55
56
57
# File 'lib/iactionable/api.rb', line 49

def get_profile_summary(profile_type, id_type, id, achievement_count = nil)
  request = @connection.request.with_app_key.to("/#{profile_type}/#{id_type}/#{id}")
  request.with_params(:achievementCount => achievement_count) unless achievement_count.blank?
  if @wrap_in_object
    IActionable::Objects::ProfileSummary.new(request.get)
  else
    request.get
  end
end

#log_event(profile_type, id_type, id, event_key, event_attrs = {}) ⇒ Object

Event Logging =



41
42
43
# File 'lib/iactionable/api.rb', line 41

def log_event(profile_type, id_type, id, event_key, event_attrs = {})
  response = @connection.request.with_app_key.with_api_key.to("/#{profile_type}/#{id_type}/#{id}/events/#{event_key}").with_params(event_attrs).post
end

#set_object_wrapping(bool) ⇒ Object



32
33
34
35
# File 'lib/iactionable/api.rb', line 32

def set_object_wrapping(bool)
  @wrap_in_object = !!bool
  self
end

#update_profile_points(profile_type, id_type, id, point_type, amount, reason = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/iactionable/api.rb', line 83

def update_profile_points(profile_type, id_type, id, point_type, amount, reason = nil)
  request = @connection.request.with_app_key.with_api_key.to("/#{profile_type}/#{id_type}/#{id}/points/#{point_type}").with_params(:value => amount)
  request.with_params(:description => reason) unless reason.blank?
  if @wrap_in_object
    IActionable::Objects::ProfilePoints.new(request.post)
  else
    request.post
  end
end