riaction 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +14 -0
- data/lib/riaction/constants.rb +11 -0
- data/lib/riaction/crud_event_callback.rb +28 -0
- data/lib/riaction/event_performer.rb +43 -0
- data/lib/riaction/profile_creation_callback.rb +10 -0
- data/lib/riaction/profile_creator.rb +33 -0
- data/lib/riaction/riaction.rb +211 -303
- data/lib/riaction/version.rb +1 -1
- data/lib/tasks/riaction.rake +4 -2
- data/spec/riaction_spec.rb +75 -7
- metadata +21 -16
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# CHANGELOG #
|
2
2
|
|
3
|
+
## 0.3.0 ##
|
4
|
+
|
5
|
+
* re-organized module and got rid of a memory leak that showed up in Rails apps running with cache\_classes set to false
|
6
|
+
|
7
|
+
## 0.2.1 ##
|
8
|
+
|
9
|
+
* Fixed problematic load order of the IActionable objects
|
10
|
+
|
11
|
+
|
12
|
+
## 0.2.0 ##
|
13
|
+
|
14
|
+
* Generator added to create YAML file for IActionable credentials
|
15
|
+
|
16
|
+
|
3
17
|
## 0.1.1 ##
|
4
18
|
|
5
19
|
* Rake task `riaction:rails:list:achievements` produces formatted output of all achievements defined on IActionable
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "resque"
|
2
|
+
require 'riaction/event_performer'
|
3
|
+
|
4
|
+
module Riaction
|
5
|
+
class CrudEventCallback
|
6
|
+
def initialize(event_name)
|
7
|
+
@event_name = event_name
|
8
|
+
end
|
9
|
+
|
10
|
+
def after_create(record)
|
11
|
+
if record.riaction_log_event?(@event_name)
|
12
|
+
Resque.enqueue(::Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_update(record)
|
17
|
+
if record.riaction_log_event?(@event_name)
|
18
|
+
Resque.enqueue(::Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def after_destroy(record)
|
23
|
+
if record.riaction_log_event?(@event_name)
|
24
|
+
Resque.enqueue(::Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'riaction/iactionable/api'
|
2
|
+
require 'riaction/constants'
|
3
|
+
require "resque"
|
4
|
+
|
5
|
+
module Riaction
|
6
|
+
class EventPerformer
|
7
|
+
@queue = :riaction_event_logger
|
8
|
+
|
9
|
+
def self.perform(event_name, klass_name, id, attempt=0)
|
10
|
+
iactionable_api = IActionable::Api.new
|
11
|
+
|
12
|
+
event_object = klass_name.constantize.find_by_id!(id)
|
13
|
+
event_details = event_object.riaction_event(event_name)
|
14
|
+
profile_keys = event_details[:profile].riaction_profile_keys
|
15
|
+
|
16
|
+
# assert the profile exists, and if not, create it
|
17
|
+
unless begin
|
18
|
+
!!iactionable_api.get_profile_summary(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
19
|
+
rescue IActionable::Error::BadRequest => e
|
20
|
+
false
|
21
|
+
end
|
22
|
+
iactionable_api.create_profile(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
23
|
+
end
|
24
|
+
|
25
|
+
# Log the event
|
26
|
+
iactionable_api.log_event(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id], event_details[:key], event_details[:params])
|
27
|
+
rescue ActiveRecord::RecordNotFound => e
|
28
|
+
# event_object no longer exists; no means to recover
|
29
|
+
rescue IActionable::Error::BadRequest => e
|
30
|
+
# Log event should never throw this as of IActionable API v3
|
31
|
+
rescue NoMethodError => e
|
32
|
+
raise NoEventDefined.new
|
33
|
+
rescue IActionable::Error::Internal => e
|
34
|
+
# upon an intenal error from IActionable, retry some set number of times by requeueing the task through Resque
|
35
|
+
# after the max number of attempts, re-raise
|
36
|
+
if attempt < ::Riaction::Constants.retry_attempts_for_internal_error
|
37
|
+
Resque.enqueue(self, event_name, klass_name, id, attempt+1)
|
38
|
+
else
|
39
|
+
raise e
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'riaction/iactionable/api'
|
2
|
+
require 'riaction/constants'
|
3
|
+
require "resque"
|
4
|
+
|
5
|
+
module Riaction
|
6
|
+
class ProfileCreator
|
7
|
+
@queue = :riaction_profile_creator
|
8
|
+
|
9
|
+
def self.perform(klass_name, id, attempt=0)
|
10
|
+
if klass_name.constantize.riaction_profile?
|
11
|
+
iactionable_api = IActionable::Api.new
|
12
|
+
profile_object = klass_name.constantize.find_by_id!(id)
|
13
|
+
profile_keys = profile_object.riaction_profile_keys
|
14
|
+
iactionable_api.create_profile(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
15
|
+
else
|
16
|
+
raise NoProfileDefined.new
|
17
|
+
end
|
18
|
+
rescue ActiveRecord::RecordNotFound => e
|
19
|
+
# event_object no longer exists; no means to recover
|
20
|
+
rescue IActionable::Error::BadRequest => e
|
21
|
+
# This should only be thrown if the profile type names specified in the model don't match what's on IActionable's dashboard
|
22
|
+
raise e
|
23
|
+
rescue IActionable::Error::Internal => e
|
24
|
+
# upon an intenal error from IActionable, retry some set number of times by requeueing the task through Resque
|
25
|
+
# after the max number of attempts, re-raise
|
26
|
+
if attempt < ::Riaction::Constants.retry_attempts_for_internal_error
|
27
|
+
Resque.enqueue(self, klass_name, id, attempt+1)
|
28
|
+
else
|
29
|
+
raise e
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/riaction/riaction.rb
CHANGED
@@ -1,388 +1,296 @@
|
|
1
|
-
require 'riaction/iactionable/api
|
2
|
-
|
1
|
+
require 'riaction/iactionable/api'
|
3
2
|
require "active_support"
|
4
3
|
require "active_record"
|
5
|
-
require
|
4
|
+
require 'riaction/event_performer'
|
5
|
+
require 'riaction/profile_creator'
|
6
|
+
require 'riaction/profile_creation_callback'
|
7
|
+
require 'riaction/crud_event_callback'
|
6
8
|
|
7
9
|
module Riaction
|
8
|
-
PROFILE_CLASSES = []
|
9
|
-
EVENT_LOGGING_CLASSES = []
|
10
10
|
|
11
11
|
class NoEventDefined < StandardError; end
|
12
12
|
class NoProfileDefined < StandardError; end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def self.crud_actions
|
19
|
-
Set.new [:create, :update, :destroy]
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.retry_attempts_for_internal_error
|
23
|
-
3
|
24
|
-
end
|
25
|
-
|
26
|
-
class ProfileCreator
|
27
|
-
@queue = :riaction_profile_creator
|
28
|
-
|
29
|
-
def self.perform(klass_name, id, attempt=0)
|
30
|
-
if klass_name.constantize.riaction_profile?
|
31
|
-
iactionable_api = IActionable::Api.new
|
32
|
-
profile_object = klass_name.constantize.find_by_id!(id)
|
33
|
-
profile_keys = profile_object.riaction_profile_keys
|
34
|
-
iactionable_api.create_profile(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
35
|
-
else
|
36
|
-
raise NoProfileDefined.new
|
37
|
-
end
|
38
|
-
rescue ActiveRecord::RecordNotFound => e
|
39
|
-
# event_object no longer exists; no means to recover
|
40
|
-
rescue IActionable::Error::BadRequest => e
|
41
|
-
# This should only be thrown if the profile type names specified in the model don't match what's on IActionable's dashboard
|
42
|
-
raise e
|
43
|
-
rescue IActionable::Error::Internal => e
|
44
|
-
# upon an intenal error from IActionable, retry some set number of times by requeueing the task through Resque
|
45
|
-
# after the max number of attempts, re-raise
|
46
|
-
if attempt < Riaction.retry_attempts_for_internal_error
|
47
|
-
Resque.enqueue(Riaction::ProfileCreator, klass_name, id, attempt+1)
|
48
|
-
else
|
49
|
-
raise e
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class EventPerformer
|
55
|
-
@queue = :riaction_event_logger
|
56
|
-
|
57
|
-
def self.perform(event_name, klass_name, id, attempt=0)
|
58
|
-
iactionable_api = IActionable::Api.new
|
59
|
-
|
60
|
-
event_object = klass_name.constantize.find_by_id!(id)
|
61
|
-
event_details = event_object.riaction_event(event_name)
|
62
|
-
profile_keys = event_details[:profile].riaction_profile_keys
|
63
|
-
|
64
|
-
# assert the profile exists, and if not, create it
|
65
|
-
unless begin
|
66
|
-
!!iactionable_api.get_profile_summary(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
67
|
-
rescue IActionable::Error::BadRequest => e
|
68
|
-
false
|
69
|
-
end
|
70
|
-
iactionable_api.create_profile(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id])
|
71
|
-
end
|
72
|
-
|
73
|
-
# Log the event
|
74
|
-
iactionable_api.log_event(profile_keys[:profile_type], profile_keys[:id_type], profile_keys[:id], event_details[:key], event_details[:params])
|
75
|
-
rescue ActiveRecord::RecordNotFound => e
|
76
|
-
# event_object no longer exists; no means to recover
|
77
|
-
rescue IActionable::Error::BadRequest => e
|
78
|
-
# Log event should never throw this as of IActionable API v3
|
79
|
-
rescue NoMethodError => e
|
80
|
-
raise NoEventDefined.new
|
81
|
-
rescue IActionable::Error::Internal => e
|
82
|
-
# upon an intenal error from IActionable, retry some set number of times by requeueing the task through Resque
|
83
|
-
# after the max number of attempts, re-raise
|
84
|
-
if attempt < Riaction.retry_attempts_for_internal_error
|
85
|
-
Resque.enqueue(Riaction::EventPerformer, event_name, klass_name, id, attempt+1)
|
86
|
-
else
|
87
|
-
raise e
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
class CrudEventCallback
|
93
|
-
def initialize(event_name)
|
94
|
-
@event_name = event_name
|
95
|
-
end
|
96
|
-
|
97
|
-
def after_create(record)
|
98
|
-
if record.riaction_log_event?(@event_name)
|
99
|
-
Resque.enqueue(Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def after_update(record)
|
104
|
-
if record.riaction_log_event?(@event_name)
|
105
|
-
Resque.enqueue(Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
106
|
-
end
|
107
|
-
end
|
13
|
+
|
14
|
+
module Riaction
|
15
|
+
PROFILE_CLASSES = []
|
16
|
+
EVENT_LOGGING_CLASSES = []
|
108
17
|
|
109
|
-
def after_destroy(record)
|
110
|
-
if record.riaction_log_event?(@event_name)
|
111
|
-
Resque.enqueue(Riaction::EventPerformer, @event_name, record.class.base_class.to_s, record.id)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
class ProfileCreationCallback
|
117
|
-
def after_create(record)
|
118
|
-
Resque.enqueue(Riaction::ProfileCreator, record.class.base_class.to_s, record.id)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
module ClassMethods
|
123
18
|
def riaction(object_type, opts)
|
124
19
|
if object_type == :profile
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
20
|
+
unless riaction_profile?
|
21
|
+
(PROFILE_CLASSES << self.to_s).uniq!
|
22
|
+
define_profile(opts.delete(:type), opts)
|
23
|
+
include Riaction::ProfileInstanceMethods
|
24
|
+
send :after_create, ::Riaction::ProfileCreationCallback.new
|
25
|
+
end
|
129
26
|
elsif object_type == :event
|
130
|
-
|
131
|
-
|
132
|
-
|
27
|
+
unless riaction_defines_events?
|
28
|
+
make_events_definable
|
29
|
+
end
|
30
|
+
unless riaction_defines_event?(opts[:name])
|
31
|
+
(EVENT_LOGGING_CLASSES << self.to_s).uniq!
|
32
|
+
define_event(opts[:name], opts[:trigger], opts[:profile], opts[:params], opts[:if])
|
33
|
+
include Riaction::EventInstanceMethods
|
34
|
+
end
|
133
35
|
end
|
134
36
|
end
|
135
37
|
|
136
|
-
def
|
38
|
+
def make_events_definable
|
137
39
|
class << self
|
138
40
|
def riaction_events
|
139
|
-
|
41
|
+
@riaction_events ||= {}
|
140
42
|
end
|
141
|
-
end
|
142
43
|
|
143
|
-
|
44
|
+
def riaction_defines_events?
|
45
|
+
true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_event(name, trigger, profile, params = {}, guard = nil)
|
144
51
|
trigger = name unless trigger
|
145
|
-
|
52
|
+
|
146
53
|
# store the event
|
147
|
-
|
148
|
-
|
149
|
-
|
54
|
+
riaction_events.store(name, {:trigger => trigger, :profile => profile, :params => params, :guard => guard})
|
55
|
+
|
150
56
|
# Create the callback or the means to trigger it
|
151
|
-
if Riaction.crud_actions.include? trigger
|
152
|
-
send "after_#{trigger}".to_sym, Riaction::CrudEventCallback.new(name)
|
153
|
-
|
57
|
+
if ::Riaction::Constants.crud_actions.include? trigger
|
58
|
+
send "after_#{trigger}".to_sym, ::Riaction::CrudEventCallback.new(name)
|
59
|
+
|
154
60
|
define_method("trigger_#{name}!") do
|
155
61
|
if self.riaction_log_event?(name)
|
156
|
-
Resque.enqueue(Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
|
62
|
+
Resque.enqueue(::Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
|
157
63
|
end
|
158
64
|
end
|
159
65
|
else
|
160
66
|
define_method("trigger_#{trigger}!") do
|
161
67
|
if self.riaction_log_event?(name)
|
162
|
-
Resque.enqueue(Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
|
68
|
+
Resque.enqueue(::Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
|
163
69
|
end
|
164
70
|
end
|
165
71
|
end
|
166
72
|
end
|
167
|
-
|
73
|
+
|
168
74
|
def define_profile(type, fields)
|
169
75
|
class << self
|
170
76
|
def riaction_profiles
|
171
|
-
|
77
|
+
@riaction_profiles ||= {}
|
172
78
|
end
|
173
|
-
end
|
174
79
|
|
175
|
-
profiles = riaction_profiles
|
176
|
-
|
177
|
-
# store the profile
|
178
|
-
profiles[type] = fields
|
179
|
-
class_variable_set(:@@riaction_profiles, profiles)
|
180
|
-
|
181
|
-
class << self
|
182
80
|
def riaction_profile?
|
183
81
|
true
|
184
82
|
end
|
185
83
|
end
|
186
|
-
end
|
187
84
|
|
85
|
+
# store the profile
|
86
|
+
riaction_profiles.store(type, fields)
|
87
|
+
end
|
88
|
+
|
188
89
|
def riaction_profile?
|
189
90
|
false
|
190
91
|
end
|
191
|
-
|
92
|
+
|
93
|
+
def riaction_defines_events?
|
94
|
+
false
|
95
|
+
end
|
96
|
+
|
97
|
+
def riaction_defines_event?(event_name)
|
98
|
+
if riaction_defines_events?
|
99
|
+
riaction_events[event_name].present?
|
100
|
+
else
|
101
|
+
false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
# end
|
192
105
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
106
|
+
module EventInstanceMethods
|
107
|
+
def riaction_event(event_name)
|
108
|
+
event = self.class.riaction_events.fetch(event_name.to_sym)
|
109
|
+
profile = riaction_event_profile(event[:profile])
|
110
|
+
|
111
|
+
unless profile.class.respond_to?(:riaction_profile?) && profile.class.riaction_profile?
|
112
|
+
raise TypeError.new("Object defined for #{self.class} on event #{event_name} as a profile must itself declare riaction(:profile ...)")
|
113
|
+
end
|
114
|
+
|
115
|
+
params = riaction_event_params(event[:params])
|
116
|
+
|
117
|
+
raise TypeError.new("Params defined for #{self.class} on event #{event_name} must be a hash") unless params.kind_of? Hash
|
197
118
|
|
198
|
-
|
199
|
-
|
200
|
-
|
119
|
+
{
|
120
|
+
:key => event_name.to_sym,
|
121
|
+
:profile => profile,
|
122
|
+
:params => params
|
123
|
+
}
|
124
|
+
rescue KeyError => e
|
125
|
+
raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
|
201
126
|
end
|
202
|
-
params = riaction_event_params(event[:params])
|
203
|
-
raise TypeError.new("Params defined for #{self.class} on event #{event_name} must be a hash") unless params.kind_of? Hash
|
204
|
-
|
205
|
-
{
|
206
|
-
:key => event_name.to_sym,
|
207
|
-
:profile => profile,
|
208
|
-
:params => params
|
209
|
-
}
|
210
|
-
rescue KeyError => e
|
211
|
-
raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
|
212
|
-
end
|
213
127
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
guard = event[:guard]
|
128
|
+
def riaction_log_event?(event_name)
|
129
|
+
event = self.class.riaction_events.fetch(event_name.to_sym)
|
130
|
+
guard = event[:guard]
|
218
131
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
132
|
+
case guard
|
133
|
+
when NilClass
|
134
|
+
true
|
135
|
+
when Symbol
|
136
|
+
self.send guard
|
137
|
+
when Proc
|
138
|
+
guard.call self
|
139
|
+
else
|
140
|
+
true
|
141
|
+
end
|
142
|
+
rescue KeyError => e
|
143
|
+
raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
|
228
144
|
end
|
229
|
-
rescue KeyError => e
|
230
|
-
raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
|
231
|
-
end
|
232
145
|
|
233
|
-
|
146
|
+
private
|
234
147
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
148
|
+
def riaction_event_profile(profile)
|
149
|
+
if profile == :self
|
150
|
+
self
|
151
|
+
elsif profile.kind_of? Symbol
|
152
|
+
self.send(profile)
|
153
|
+
elsif profile.kind_of? Proc
|
154
|
+
profile.call
|
155
|
+
else
|
156
|
+
nil
|
157
|
+
end
|
158
|
+
rescue Exception => e
|
159
|
+
raise
|
244
160
|
end
|
245
|
-
rescue Exception => e
|
246
|
-
raise
|
247
|
-
end
|
248
161
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
162
|
+
def riaction_event_params(params)
|
163
|
+
if params.kind_of? Symbol
|
164
|
+
self.send(params)
|
165
|
+
elsif params.kind_of? Proc
|
166
|
+
params.call
|
167
|
+
elsif params.kind_of? Hash
|
168
|
+
resolved_params = {}
|
169
|
+
params.each_pair do |key, value|
|
170
|
+
resolved_params[key] = self.respond_to?(value) ? self.send(value) : value
|
171
|
+
end
|
172
|
+
resolved_params
|
173
|
+
else
|
174
|
+
{}
|
258
175
|
end
|
259
|
-
|
260
|
-
|
261
|
-
{}
|
176
|
+
rescue Exception => e
|
177
|
+
raise
|
262
178
|
end
|
263
|
-
rescue Exception => e
|
264
|
-
raise
|
265
179
|
end
|
266
|
-
end
|
267
180
|
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
ids = profiles.first[1]
|
278
|
-
end
|
181
|
+
module ProfileInstanceMethods
|
182
|
+
def riaction_profile_keys(profile_type=nil, id_type=nil)
|
183
|
+
if self.class.riaction_profiles.size > 0
|
184
|
+
if profile_type && self.class.riaction_profiles.has_key?(profile_type)
|
185
|
+
ids = self.class.riaction_profiles.fetch(profile_type)
|
186
|
+
else
|
187
|
+
profile_type = self.class.riaction_profiles.first[0]
|
188
|
+
ids = self.class.riaction_profiles.first[1]
|
189
|
+
end
|
279
190
|
|
280
|
-
|
281
|
-
|
191
|
+
if id_type && ids.has_key?(id_type)
|
192
|
+
{:profile_type => profile_type.to_s, :id_type => id_type.to_s, :id => self.send(ids.fetch(id_type)).to_s}
|
193
|
+
else
|
194
|
+
{:profile_type => profile_type.to_s, :id_type => ids.first[0].to_s, :id => self.send(ids.first[1]).to_s}
|
195
|
+
end
|
282
196
|
else
|
283
|
-
{
|
197
|
+
{}
|
284
198
|
end
|
285
|
-
|
199
|
+
rescue KeyError, NoMethodError => e
|
286
200
|
{}
|
287
201
|
end
|
288
|
-
rescue KeyError, NoMethodError => e
|
289
|
-
{}
|
290
|
-
end
|
291
202
|
|
292
|
-
|
293
|
-
|
294
|
-
|
203
|
+
#################
|
204
|
+
# API wrappers #
|
205
|
+
#################
|
295
206
|
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
207
|
+
def riaction_profile_summary(achievement_count=nil)
|
208
|
+
keys = riaction_profile_keys
|
209
|
+
unless keys.empty?
|
210
|
+
@iactionable_api ||= IActionable::Api.new
|
211
|
+
@iactionable_api.get_profile_summary(keys[:profile_type], keys[:id_type], keys[:id], achievement_count)
|
212
|
+
else
|
213
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
214
|
+
end
|
215
|
+
rescue IActionable::Error::BadRequest => e
|
216
|
+
nil
|
303
217
|
end
|
304
|
-
rescue IActionable::Error::BadRequest => e
|
305
|
-
nil
|
306
|
-
end
|
307
218
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
219
|
+
def riaction_create_profile
|
220
|
+
keys = riaction_profile_keys
|
221
|
+
unless keys.empty?
|
222
|
+
existing = riaction_profile_summary
|
223
|
+
unless existing
|
224
|
+
@iactionable_api ||= IActionable::Api.new
|
225
|
+
@iactionable_api.create_profile(keys[:profile_type], keys[:id_type], keys[:id])
|
226
|
+
else
|
227
|
+
existing
|
228
|
+
end
|
315
229
|
else
|
316
|
-
|
230
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
317
231
|
end
|
318
|
-
else
|
319
|
-
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
320
232
|
end
|
321
|
-
end
|
322
233
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
234
|
+
def riaction_update_profile(new_id_type)
|
235
|
+
old_keys = riaction_profile_keys
|
236
|
+
new_keys = riaction_profile_keys(old_keys[:profile_type], new_id_type)
|
237
|
+
unless old_keys.empty? || new_keys.empty?
|
238
|
+
@iactionable_api ||= IActionable::Api.new
|
239
|
+
@iactionable_api.add_profile_identifier(old_keys[:profile_type], old_keys[:id_type], old_keys[:id], new_keys[:id_type], new_keys[:id])
|
240
|
+
else
|
241
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
242
|
+
end
|
331
243
|
end
|
332
|
-
end
|
333
244
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
245
|
+
def riaction_profile_achievements(filter_type=nil)
|
246
|
+
keys = riaction_profile_keys
|
247
|
+
unless keys.empty?
|
248
|
+
@iactionable_api ||= IActionable::Api.new
|
249
|
+
@iactionable_api.get_profile_achievements(keys[:profile_type], keys[:id_type], keys[:id], filter_type)
|
250
|
+
else
|
251
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
252
|
+
end
|
253
|
+
rescue IActionable::Error::BadRequest => e
|
254
|
+
nil
|
341
255
|
end
|
342
|
-
rescue IActionable::Error::BadRequest => e
|
343
|
-
nil
|
344
|
-
end
|
345
256
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
257
|
+
def riaction_profile_challenges(filter_type=nil)
|
258
|
+
keys = riaction_profile_keys
|
259
|
+
unless keys.empty?
|
260
|
+
@iactionable_api ||= IActionable::Api.new
|
261
|
+
@iactionable_api.get_profile_challenges(keys[:profile_type], keys[:id_type], keys[:id], filter_type)
|
262
|
+
else
|
263
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
264
|
+
end
|
265
|
+
rescue IActionable::Error::BadRequest => e
|
266
|
+
nil
|
353
267
|
end
|
354
|
-
rescue IActionable::Error::BadRequest => e
|
355
|
-
nil
|
356
|
-
end
|
357
268
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
269
|
+
def riaction_profile_goals(filter_type=nil)
|
270
|
+
keys = riaction_profile_keys
|
271
|
+
unless keys.empty?
|
272
|
+
@iactionable_api ||= IActionable::Api.new
|
273
|
+
@iactionable_api.get_profile_goals(keys[:profile_type], keys[:id_type], keys[:id], filter_type)
|
274
|
+
else
|
275
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
276
|
+
end
|
277
|
+
rescue IActionable::Error::BadRequest => e
|
278
|
+
nil
|
365
279
|
end
|
366
|
-
rescue IActionable::Error::BadRequest => e
|
367
|
-
nil
|
368
|
-
end
|
369
280
|
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
281
|
+
def riaction_profile_notifications
|
282
|
+
keys = riaction_profile_keys
|
283
|
+
unless keys.empty?
|
284
|
+
@iactionable_api ||= IActionable::Api.new
|
285
|
+
@iactionable_api.get_profile_notifications(keys[:profile_type], keys[:id_type], keys[:id])
|
286
|
+
else
|
287
|
+
raise NoProfileDefined.new("Class #{self.class} does not adequately define itself as an IActionable profile")
|
288
|
+
end
|
289
|
+
rescue IActionable::Error::BadRequest => e
|
290
|
+
nil
|
377
291
|
end
|
378
|
-
rescue IActionable::Error::BadRequest => e
|
379
|
-
nil
|
380
292
|
end
|
381
293
|
end
|
382
294
|
end
|
383
295
|
|
384
|
-
|
385
|
-
ActiveRecord::Base.send(:include, Riaction)
|
386
|
-
rescue NameError => e
|
387
|
-
#
|
388
|
-
end
|
296
|
+
ActiveRecord::Base.extend ::Riaction::Riaction
|
data/lib/riaction/version.rb
CHANGED
data/lib/tasks/riaction.rake
CHANGED
@@ -7,7 +7,8 @@ namespace 'riaction' do
|
|
7
7
|
require rbfile
|
8
8
|
end
|
9
9
|
|
10
|
-
Riaction::EVENT_LOGGING_CLASSES.each do |
|
10
|
+
Riaction::EVENT_LOGGING_CLASSES.each do |class_name|
|
11
|
+
klass = class_name.constantize
|
11
12
|
puts "#{klass} defines the following events:"
|
12
13
|
klass.riaction_events.each_pair do |name, deets|
|
13
14
|
puts " :#{name}:"
|
@@ -75,7 +76,8 @@ namespace 'riaction' do
|
|
75
76
|
require rbfile
|
76
77
|
end
|
77
78
|
|
78
|
-
Riaction::PROFILE_CLASSES.each do |
|
79
|
+
Riaction::PROFILE_CLASSES.each do |class_name|
|
80
|
+
klass = class_name.constantize
|
79
81
|
begin
|
80
82
|
klass.all.each do |obj|
|
81
83
|
declaration = klass.riaction_profiles.first
|
data/spec/riaction_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper.rb'
|
|
3
3
|
describe Riaction do
|
4
4
|
class RiactionTestBase
|
5
5
|
extend ActiveModel::Callbacks
|
6
|
-
|
6
|
+
extend Riaction::Riaction
|
7
7
|
|
8
8
|
define_model_callbacks :create, :update, :destroy
|
9
9
|
|
@@ -34,6 +34,74 @@ describe Riaction do
|
|
34
34
|
Resque.stub!(:enqueue)
|
35
35
|
end
|
36
36
|
|
37
|
+
describe "using riaction" do
|
38
|
+
describe "to define a profile" do
|
39
|
+
before do
|
40
|
+
class RiactionClass < RiactionTestBase
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "the first time" do
|
45
|
+
it "should set up the profile info" do
|
46
|
+
RiactionClass.riaction_profile?.should be_false
|
47
|
+
RiactionClass.class_eval do
|
48
|
+
riaction :profile, :type => :user, :custom => :id
|
49
|
+
end
|
50
|
+
RiactionClass.riaction_profile?.should be_true
|
51
|
+
hash_including(:custom => :id).should == RiactionClass.riaction_profiles[:user]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "a second time" do
|
56
|
+
before do
|
57
|
+
RiactionClass.class_eval do
|
58
|
+
riaction :profile, :type => :user, :custom => :id
|
59
|
+
end
|
60
|
+
end
|
61
|
+
it "should not try to invoke any internal config methods" do
|
62
|
+
RiactionClass.should_not_receive(:define_profile)
|
63
|
+
RiactionClass.should_not_receive(:include)
|
64
|
+
RiactionClass.should_not_receive(:after_create)
|
65
|
+
RiactionClass.class_eval do
|
66
|
+
riaction :profile, :type => :user, :custom => :id
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "to define an event" do
|
73
|
+
before do
|
74
|
+
class RiactionClass < RiactionTestBase
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "the first time" do
|
79
|
+
it "should set up the event info" do
|
80
|
+
RiactionClass.riaction_defines_event?(:create_profile).should be_false
|
81
|
+
RiactionClass.class_eval do
|
82
|
+
riaction :event, :name => :create_profile, :trigger => :create, :profile => :self, :params => {:foo => "bar"}
|
83
|
+
end
|
84
|
+
RiactionClass.riaction_defines_event?(:create_profile).should be_true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "a second time" do
|
89
|
+
before do
|
90
|
+
RiactionClass.class_eval do
|
91
|
+
riaction :event, :name => :create_profile, :trigger => :create, :profile => :self, :params => {:foo => "bar"}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
it "should not try to invoke any internal config methods" do
|
95
|
+
RiactionClass.should_not_receive(:define_event)
|
96
|
+
RiactionClass.should_not_receive(:include)
|
97
|
+
RiactionClass.class_eval do
|
98
|
+
riaction :event, :name => :create_profile, :trigger => :create, :profile => :self, :params => {:foo => "bar"}
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
37
105
|
describe "logging events" do
|
38
106
|
class MyClass < RiactionTestBase
|
39
107
|
riaction :profile, :type => :user, :custom => :id
|
@@ -96,12 +164,12 @@ describe Riaction do
|
|
96
164
|
end
|
97
165
|
|
98
166
|
it "should re-schedule the task some defined number of times before re-raising again on the last attempt" do
|
99
|
-
Resque.should_receive(:enqueue).exactly(Riaction.retry_attempts_for_internal_error).times.with(Riaction::EventPerformer, :create_profile, "MyClass", @instance.id, instance_of(Fixnum))
|
167
|
+
Resque.should_receive(:enqueue).exactly(Riaction::Constants.retry_attempts_for_internal_error).times.with(Riaction::EventPerformer, :create_profile, "MyClass", @instance.id, instance_of(Fixnum))
|
100
168
|
|
101
|
-
(Riaction.retry_attempts_for_internal_error).times do |i|
|
169
|
+
(Riaction::Constants.retry_attempts_for_internal_error).times do |i|
|
102
170
|
lambda { Riaction::EventPerformer.perform(:create_profile, "MyClass", @bad_instance.id, i) }.should_not raise_error
|
103
171
|
end
|
104
|
-
lambda { Riaction::EventPerformer.perform(:create_profile, "MyClass", @bad_instance.id, Riaction.retry_attempts_for_internal_error) }.should raise_error(IActionable::Error::Internal)
|
172
|
+
lambda { Riaction::EventPerformer.perform(:create_profile, "MyClass", @bad_instance.id, Riaction::Constants.retry_attempts_for_internal_error) }.should raise_error(IActionable::Error::Internal)
|
105
173
|
end
|
106
174
|
end
|
107
175
|
end
|
@@ -147,12 +215,12 @@ describe Riaction do
|
|
147
215
|
end
|
148
216
|
|
149
217
|
it "should re-schedule the task some defined number of times before re-raising again on the last attempt" do
|
150
|
-
Resque.should_receive(:enqueue).exactly(Riaction.retry_attempts_for_internal_error).times.with(Riaction::ProfileCreator, "MyClass", @instance.id, instance_of(Fixnum))
|
218
|
+
Resque.should_receive(:enqueue).exactly(Riaction::Constants.retry_attempts_for_internal_error).times.with(Riaction::ProfileCreator, "MyClass", @instance.id, instance_of(Fixnum))
|
151
219
|
|
152
|
-
(Riaction.retry_attempts_for_internal_error).times do |i|
|
220
|
+
(Riaction::Constants.retry_attempts_for_internal_error).times do |i|
|
153
221
|
lambda { Riaction::ProfileCreator.perform("MyClass", @instance.id, i) }.should_not raise_error
|
154
222
|
end
|
155
|
-
lambda { Riaction::ProfileCreator.perform("MyClass", @instance.id, Riaction.retry_attempts_for_internal_error) }.should raise_error(IActionable::Error::Internal)
|
223
|
+
lambda { Riaction::ProfileCreator.perform("MyClass", @instance.id, Riaction::Constants.retry_attempts_for_internal_error) }.should raise_error(IActionable::Error::Internal)
|
156
224
|
end
|
157
225
|
end
|
158
226
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riaction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-12-02 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153241880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153241880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &2153241460 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2153241460
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: faraday
|
38
|
-
requirement: &
|
38
|
+
requirement: &2153240920 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2153240920
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: faraday-stack
|
49
|
-
requirement: &
|
49
|
+
requirement: &2153235960 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2153235960
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: activerecord
|
60
|
-
requirement: &
|
60
|
+
requirement: &2153235400 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 3.0.0
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2153235400
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activesupport
|
71
|
-
requirement: &
|
71
|
+
requirement: &2153234880 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 3.0.0
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2153234880
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: resque
|
82
|
-
requirement: &
|
82
|
+
requirement: &2153234500 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :runtime
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2153234500
|
91
91
|
description: Wrapper for IActionable's restful API and an "acts-as" style interface
|
92
92
|
for models to behave as profiles and drive game events.
|
93
93
|
email:
|
@@ -105,6 +105,9 @@ files:
|
|
105
105
|
- lib/generators/riaction/riaction_generator.rb
|
106
106
|
- lib/generators/riaction/templates/i_actionable.yml
|
107
107
|
- lib/riaction.rb
|
108
|
+
- lib/riaction/constants.rb
|
109
|
+
- lib/riaction/crud_event_callback.rb
|
110
|
+
- lib/riaction/event_performer.rb
|
108
111
|
- lib/riaction/iactionable/api.rb
|
109
112
|
- lib/riaction/iactionable/connection.rb
|
110
113
|
- lib/riaction/iactionable/error.rb
|
@@ -125,6 +128,8 @@ files:
|
|
125
128
|
- lib/riaction/iactionable/objects/profile_summary.rb
|
126
129
|
- lib/riaction/iactionable/objects/progress.rb
|
127
130
|
- lib/riaction/iactionable/settings.rb
|
131
|
+
- lib/riaction/profile_creation_callback.rb
|
132
|
+
- lib/riaction/profile_creator.rb
|
128
133
|
- lib/riaction/railtie.rb
|
129
134
|
- lib/riaction/riaction.rb
|
130
135
|
- lib/riaction/version.rb
|