Class: IActionable::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/riaction/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.



11
12
13
14
15
16
17
# File 'lib/riaction/iactionable/api.rb', line 11

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
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Class Method Details

.init_settings(values) ⇒ Object



19
20
21
22
23
# File 'lib/riaction/iactionable/api.rb', line 19

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

.settingsObject



25
26
27
# File 'lib/riaction/iactionable/api.rb', line 25

def self.settings
  @@settings
end

Instance Method Details

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



55
56
57
# File 'lib/riaction/iactionable/api.rb', line 55

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



48
49
50
51
52
# File 'lib/riaction/iactionable/api.rb', line 48

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(:display_name => display_name) unless display_name.blank?
  request.post
end

#get_achievementsObject



100
101
102
103
104
105
# File 'lib/riaction/iactionable/api.rb', line 100

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

#get_challengesObject



132
133
134
135
136
137
# File 'lib/riaction/iactionable/api.rb', line 132

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

#get_goalsObject



164
165
166
167
168
169
# File 'lib/riaction/iactionable/api.rb', line 164

def get_goals()
  response = @connection.request.with_app_key.to("/goals").get
  response.map{|goal_json| IActionable::Objects::Goal.new(goal_json)}
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 =



175
176
177
178
179
180
181
182
183
# File 'lib/riaction/iactionable/api.rb', line 175

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?
  response = request.get
  IActionable::Objects::LeaderboardReport.new(response)
end

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

Achievement API calls =



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/riaction/iactionable/api.rb', line 79

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")
    response = request.get
    response.map{|achievement_json| IActionable::Objects::Achievement.new(achievement_json)}
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/achievements/Available")
    response = request.get
    response.map{|achievement_json| IActionable::Objects::Achievement.new(achievement_json)}
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/achievements")
    response = request.get
    {
      :available => response["Available"].map{|achievement_json| IActionable::Objects::Achievement.new(achievement_json)},
      :completed => response["Completed"].map{|achievement_json| IActionable::Objects::Achievement.new(achievement_json)}
    }
  end
end

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

Challenges API calls =



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/riaction/iactionable/api.rb', line 111

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")
    response = request.get
    response.map{|challenge_json| IActionable::Objects::Challenge.new(challenge_json)}
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/challenges/Available")
    response = request.get
    response.map{|challenge_json| IActionable::Objects::Challenge.new(challenge_json)}
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/challenges")
    response = request.get
    {
      :available => response["Available"].map{|challenge_json| IActionable::Objects::Challenge.new(challenge_json)},
      :completed => response["Completed"].map{|challenge_json| IActionable::Objects::Challenge.new(challenge_json)}
    }
  end
end

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

Goals API calls =



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/riaction/iactionable/api.rb', line 143

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")
    response = request.get
    response.map{|goal_json| IActionable::Objects::Goal.new(goal_json)}
  when :available
    request.to("/#{profile_type}/#{id_type}/#{id}/goals/Available")
    response = request.get
    response.map{|goal_json| IActionable::Objects::Goal.new(goal_json)}
  else
    request.to("/#{profile_type}/#{id_type}/#{id}/goals")
    response = request.get
    {
      :available => response["Available"].map{|goal_json| IActionable::Objects::Goal.new(goal_json)},
      :completed => response["Completed"].map{|goal_json| IActionable::Objects::Goal.new(goal_json)}
    }
  end
end

#get_profile_notifications(profile_type, id_type, id) ⇒ Object

Profile Notifications API calls =



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/riaction/iactionable/api.rb', line 189

def get_profile_notifications(profile_type, id_type, id)
  response = @connection.request.with_app_key.to("/#{profile_type}/#{id_type}/#{id}/notifications").get
  {
    :achievements => {
      :available => response["Achievements"]["Available"].map{|a| IActionable::Objects::Achievement.new(a)},
      :completed => response["Achievements"]["Completed"].map{|a| IActionable::Objects::Achievement.new(a)}
    },
    :challenges => {
      :available => response["Challenges"]["Available"].map{|c| IActionable::Objects::Challenge.new(c)},
      :completed => response["Challenges"]["Completed"].map{|c| IActionable::Objects::Challenge.new(c)}
    },
    :goals => {
      :available => response["Goals"]["Available"].map{|g| IActionable::Objects::Goal.new(g)},
      :completed => response["Goals"]["Completed"].map{|g| IActionable::Objects::Goal.new(g)}
    },
    :levels => response["Levels"].map{|l| IActionable::Objects::Level.new(l)},
    :points => response["Points"].map{|p| IActionable::Objects::ProfilePoints.new(p)}
  }
end

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

Points API calls =



63
64
65
66
# File 'lib/riaction/iactionable/api.rb', line 63

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
  IActionable::Objects::ProfilePoints.new(response)
end

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

Profile API calls =



41
42
43
44
45
46
# File 'lib/riaction/iactionable/api.rb', line 41

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(:achievement_count => achievement_count) unless achievement_count.blank?
  response = request.get
  IActionable::Objects::ProfileSummary.new(response)
end

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

Event Logging =



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

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

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



68
69
70
71
72
73
# File 'lib/riaction/iactionable/api.rb', line 68

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?
  response = request.post
  IActionable::Objects::ProfilePoints.new(response)
end