rulesio 0.9.10 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ module RulesIO
2
+ class Base
3
+ # include Singleton
4
+ attr_accessor :token, :buffer
5
+
6
+ def initialize(token)
7
+ RulesIO.instance = self
8
+ puts "INSTANCE SET"
9
+ @token = token
10
+ @buffer = []
11
+ end
12
+
13
+ def send_event(event)
14
+ @buffer << prepare_event(event)
15
+ end
16
+
17
+ def flush
18
+ return if (events = @buffer).empty?
19
+ @buffer = []
20
+ RulesIO.queue.push(:payload => events, :token => @token)
21
+ end
22
+
23
+ private
24
+ def prepare_event(event)
25
+ event = event.with_indifferent_access
26
+
27
+ event[:_actor] = event[:_actor].to_s
28
+ event[:_timestamp] ||= Time.now.to_f
29
+
30
+ event
31
+ end
32
+ end
33
+ end
@@ -3,9 +3,10 @@
3
3
  # http://sharagoz.com/posts/1-rolling-your-own-exception-handler-in-rails-3
4
4
 
5
5
  require 'action_dispatch'
6
+ require 'rulesio/users'
6
7
 
7
8
  module RulesIO
8
- class Exceptions
9
+ class Exceptions < RulesIO::Users
9
10
  include RulesIO::Helpers
10
11
 
11
12
  def self.default_ignored_exceptions
@@ -19,7 +20,7 @@ module RulesIO
19
20
  def initialize(app, options={})
20
21
  @app, @options = app, options
21
22
  @options[:ignore_exceptions] ||= self.class.default_ignored_exceptions
22
- @options[:ignore_crawlers] ||= RulesIO.default_ignored_crawlers
23
+ @options[:ignore_crawlers] ||= default_ignored_crawlers
23
24
  @options[:ignore_if] ||= lambda { |env, e| false }
24
25
  @options[:token] ||= RulesIO.token
25
26
  @options[:custom_data] ||= lambda { |env| {} }
@@ -30,17 +31,18 @@ module RulesIO
30
31
  @app.call(env)
31
32
  rescue Exception => exception
32
33
  env['rulesio.exception'] = exception
33
- send_event_now event(env, exception), @options[:token], env unless should_be_ignored(env, exception)
34
+ send_event event(env, exception), env unless should_be_ignored(env, exception)
34
35
  raise exception
35
36
  end
36
37
  end
37
-
38
- private
39
- def send_event_now(event, token, env)
40
- prep = RulesIO.prepare_event(event, env)
41
- RulesIO.post_payload_to_token prep, token
38
+
39
+ def send_event(event, env)
40
+ prep = prepare_event(event, env)
41
+ RulesIO.post_payload_to_token prep, @options[:token]
42
42
  end
43
43
 
44
+
45
+ private
44
46
  def should_be_ignored(env, exception)
45
47
  ignored_exception(@options[:ignore_exceptions], exception) ||
46
48
  from_crawler(@options[:ignore_crawlers], env['HTTP_USER_AGENT']) ||
@@ -69,7 +71,7 @@ module RulesIO
69
71
  :file => fileline(exception),
70
72
  :backtrace => backtrace.join("\n")
71
73
  }.with_indifferent_access
72
- useractor = RulesIO.current_actor(env)
74
+ useractor = current_actor(env)
73
75
  event[:_xactor] = useractor if useractor
74
76
  event.merge!(@options[:custom_data].call(env))
75
77
  event
@@ -10,7 +10,7 @@ module RulesIO
10
10
  super(:rulesio, {:size => 1}.merge(RulesIO.queue_options)) do |msg|
11
11
  retries = 0
12
12
  begin
13
- RulesIO.post_payload_to_token msg[:payload], RulesIO.token
13
+ RulesIO.post_payload_to_token msg[:payload], msg[:token]
14
14
  rescue Exception => e
15
15
  if (retries += 1) % 6 == 5
16
16
  RulesIO.logger.warn "RulesIO having trouble sending events; #{retries} attempts so far."
@@ -4,7 +4,7 @@ module RulesIO
4
4
  class MemoryQueue
5
5
 
6
6
  def self.push(hash)
7
- RulesIO.post_payload_to_token hash[:payload], RulesIO.token
7
+ RulesIO.post_payload_to_token hash[:payload], hash[:token]
8
8
  end
9
9
 
10
10
  end
@@ -0,0 +1,115 @@
1
+ module RulesIO
2
+ class Rack < Base
3
+ attr_accessor :filter_parameters, :controller_data, :config_options
4
+
5
+ #cattr_accessor :config_options
6
+ @@config_options = {}
7
+
8
+ def self.config_options
9
+ @@config_options
10
+ end
11
+
12
+ def self.config_options=(opts)
13
+ @@config_options = opts
14
+ end
15
+
16
+ def default_ignored_crawlers
17
+ %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot NewRelicPinger CopperEgg Pingdom UptimeRobot)
18
+ end
19
+
20
+ def initialize(app, options={})
21
+ self.class.config_options = @config_options = (options || {})
22
+ RulesIO.instance = self
23
+ @app = app
24
+ @buffer = []
25
+ @filter_parameters ||= defined?(Rails) ? Rails.application.config.filter_parameters : []
26
+ @token = options[:token]
27
+ @controller_data = options[:controller_data] || '{}'
28
+ end
29
+
30
+ def call(env)
31
+ @buffer = []
32
+ request = ::Rack::Request.new(env)
33
+ env['rulesio.request_url'] = request.url
34
+ env['rulesio.request_method'] = request.request_method
35
+ @app.call(env)
36
+ ensure
37
+ flush(env)
38
+ end
39
+
40
+ def send_event(event)
41
+ @buffer << event
42
+ end
43
+
44
+ def flush(env={})
45
+ return if (events = @buffer).empty?
46
+ @buffer = []
47
+ RulesIO.queue.push(:token => @token, :payload => events.map {|event| prepare_event(event, env)})
48
+ end
49
+
50
+ private
51
+ def prepare_event(event, env)
52
+ event = event.with_indifferent_access
53
+
54
+ current_user = current_user(env) rescue nil
55
+ actor = current_actor(env) rescue nil
56
+
57
+ if controller = env['action_controller.instance']
58
+ begin
59
+ data = if RulesIO.controller_data.is_a?(String)
60
+ controller.instance_eval(RulesIO.controller_data)
61
+ elsif RulesIO.controller_data.is_a?(Proc) && !RulesIO.controller_data.lambda?
62
+ controller.instance_eval(&RulesIO.controller_data)
63
+ else
64
+ {}
65
+ end
66
+ event = data.with_indifferent_access.merge(event)
67
+ rescue Exception => e
68
+ RulesIO.logger.warn "RulesIO having trouble with controller_data: #{e}"
69
+ end
70
+ end
71
+
72
+ event[:_actor] = actor || 'anonymous' unless event[:_actor].present?
73
+ event[:rails_env] = Rails.env if defined?(Rails)
74
+
75
+ unless env.empty?
76
+ env['rack.input'].rewind
77
+ request = defined?(Rails) ? ActionDispatch::Request.new(env) : ::Rack::Request.new(env)
78
+ params = request.params
79
+ action = page_event_name(request, params)
80
+
81
+ event[:_domain] = 'JSON' if event[:_domain] == 'pageview' && params['format'] == 'json'
82
+ event[:_domain] = 'XML' if event[:_domain] == 'pageview' && params['format'] == 'xml'
83
+ event[:_name] ||= action
84
+ event[:_from] ||= current_user.email if current_user && current_user.respond_to?(:email) && current_user.email != event[:_actor]
85
+ event[:action] = action
86
+ event[:request_url] = env['rulesio.request_url']
87
+ event[:request_method] = env['rulesio.request_method']
88
+ event[:user_agent] = request.user_agent
89
+ event[:referer_url] = request.referer
90
+ event[:session] = request.session
91
+ parameter_filter = ::ActionDispatch::Http::ParameterFilter.new(filter_parameters)
92
+ event[:params] = parameter_filter.filter(params)
93
+ end
94
+
95
+ event.reject! {|k, v| v.to_s.blank?}
96
+ event
97
+ end
98
+
99
+ def current_user(env)
100
+ if controller = env['action_controller.instance']
101
+ controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
102
+ end
103
+ rescue
104
+ nil
105
+ end
106
+
107
+ def page_event_name(request, params)
108
+ if params && params['controller']
109
+ "#{params['controller']}##{params['action']}"
110
+ else
111
+ request.path.gsub('/', '-')[1..-1]
112
+ end
113
+ end
114
+ end
115
+ end
@@ -19,7 +19,7 @@ module RulesIO
19
19
  end
20
20
 
21
21
  def middleware(middleware, &block)
22
- @middlewares[middleware] = MiddlewareConfigurator.apply(&block)
22
+ @middlewares[middleware] = MiddlewareConfigurator.apply(self, &block)
23
23
  end
24
24
 
25
25
  def queue(queue, options)
@@ -38,13 +38,25 @@ module RulesIO
38
38
  def girl_friday_options(options)
39
39
  @girl_friday_options = options
40
40
  end
41
+
42
+ def configuration
43
+ {
44
+ :webhook_url => @webhook_url,
45
+ :disable_sending_events => @disable,
46
+ :token => @token,
47
+ :queue => @queue,
48
+ :queue_options => @queue_options,
49
+ :controller_data => @controller_data
50
+ }
51
+ end
41
52
  end
42
53
 
43
54
  class MiddlewareConfigurator
44
- attr_accessor :configuration
55
+ attr_accessor :configuration, :root
45
56
 
46
- def self.apply(&block)
57
+ def self.apply(root, &block)
47
58
  x = new
59
+ x.root = root
48
60
  x.configure(&block) if block_given?
49
61
  x
50
62
  end
@@ -77,17 +89,16 @@ module RulesIO
77
89
  if File.exists?(filename)
78
90
  RulesIO::RailsConfigurator.new.instance_eval do
79
91
  eval IO.read(filename), binding, filename.to_s, 1
92
+ RulesIO.logger = Rails.logger
93
+ RulesIO.webhook_url = @webhook_url || 'https://www.rules.io/events/'
94
+ RulesIO.queue = @queue || RulesIO::MemoryQueue
95
+ RulesIO.queue_options = @queue_options || {}
96
+ RulesIO.disable_sending_events = @disable_sending_events || false
80
97
  if defined?(::Rails.configuration) && ::Rails.configuration.respond_to?(:middleware)
81
- ::Rails.configuration.middleware.insert 0, 'RulesIO::Rack',
82
- :webhook_url => @webhook_url,
83
- :disable_sending_events => @disable,
84
- :token => @token,
85
- :queue => @queue,
86
- :queue_options => @queue_options,
87
- :controller_data => @controller_data
88
- ::Rails.configuration.middleware.use('RulesIO::Users', @middlewares[:users].configuration) if @middlewares.has_key?(:users)
89
- ::Rails.configuration.middleware.use('RulesIO::Users', @middlewares[:pageviews].configuration) if @middlewares.has_key?(:pageviews)
90
- ::Rails.configuration.middleware.use('RulesIO::Exceptions', @middlewares[:exceptions].configuration) if @middlewares.has_key?(:exceptions)
98
+ ::Rails.configuration.middleware.insert 0, 'RulesIO::Rack', configuration
99
+ ::Rails.configuration.middleware.use('RulesIO::Users', configuration.merge(@middlewares[:users].configuration)) if @middlewares.has_key?(:users)
100
+ ::Rails.configuration.middleware.use('RulesIO::Users', configuration.merge(@middlewares[:pageviews].configuration)) if @middlewares.has_key?(:pageviews)
101
+ ::Rails.configuration.middleware.use('RulesIO::Exceptions', configuration.merge(@middlewares[:exceptions].configuration)) if @middlewares.has_key?(:exceptions)
91
102
  end
92
103
  end
93
104
  else
data/lib/rulesio/users.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'action_dispatch'
2
2
 
3
3
  module RulesIO
4
- class Users
5
- include RulesIO::Helpers
6
-
4
+ class Users < RulesIO::Rack
5
+ include Helpers
7
6
  def initialize(app, options={})
8
- @app, @options = app, options
9
- @options[:ignore_crawlers] ||= RulesIO.default_ignored_crawlers
7
+ @app, @options = app, RulesIO::Rack.config_options.merge(options)
8
+ super(@app, @options)
9
+ @options[:ignore_crawlers] ||= default_ignored_crawlers
10
10
  @options[:ignore_if] ||= Proc.new { |env| false }
11
11
  @options[:ignore_if_controller] ||= 'false'
12
12
  @options[:custom_data] ||= Proc.new { |env| {} }
@@ -73,5 +73,34 @@ module RulesIO
73
73
  event
74
74
  end
75
75
 
76
+ def current_actor(env)
77
+ if controller = env['action_controller.instance']
78
+ begin
79
+ data = if @controller_data.is_a?(String)
80
+ controller.instance_eval(@controller_data)
81
+ elsif @controller_data.is_a?(Proc) && !@controller_data.lambda?
82
+ controller.instance_eval(&@controller_data)
83
+ else
84
+ {}
85
+ end
86
+ data = data.with_indifferent_access
87
+ return data[:_actor] if data[:_actor]
88
+ rescue Exception => e
89
+ puts e.message
90
+ puts e.backtrace.join("\n")
91
+ end
92
+
93
+ begin
94
+ user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
95
+ [:to_param, :id].each do |method|
96
+ return user.send(method) if user && user.respond_to?(method)
97
+ end
98
+ rescue Exception => e
99
+ puts e.message
100
+ puts e.backtrace.join("\n")
101
+ end
102
+ end
103
+ nil
104
+ end
76
105
  end
77
106
  end
@@ -1,3 +1,3 @@
1
1
  module RulesIO
2
- VERSION = '0.9.10'
2
+ VERSION = '0.10.0'
3
3
  end
data/lib/rulesio.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'rulesio/version'
2
2
  require 'rulesio/helpers'
3
- require 'rulesio/exceptions'
3
+ require 'rulesio/base'
4
+ require 'rulesio/rack'
4
5
  require 'rulesio/users'
6
+ require 'rulesio/exceptions'
5
7
  require 'rulesio/girl_friday_queue'
6
8
  require 'rulesio/memory_queue'
7
9
  require 'net/http'
@@ -12,21 +14,16 @@ require 'active_support/core_ext/hash/indifferent_access'
12
14
  require 'action_dispatch/http/parameter_filter'
13
15
 
14
16
  module RulesIO
15
- mattr_accessor :filter_parameters, :buffer, :token, :webhook_url, :queue, :queue_options, :controller_data, :logger, :disable_sending_events
16
-
17
- def self.default_ignored_crawlers
18
- %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot NewRelicPinger CopperEgg Pingdom UptimeRobot)
19
- end
17
+ mattr_accessor :webhook_url, :queue, :queue_options, :logger, :disable_sending_events, :instance
20
18
 
21
- def self.send_event(event)
22
- buffer << event
19
+ @logger = Logger.new(STDOUT)
20
+
21
+ def self.send_event(*args)
22
+ instance.send_event(*args)
23
23
  end
24
-
25
- def self.flush(env={})
26
- return if (events = RulesIO.buffer).empty?
27
- RulesIO.buffer = []
28
- RulesIO.queue.push(:payload => events.map {|event| RulesIO.prepare_event(event, env)})
29
- # RulesIO.post_payload_to_token events.to_json, RulesIO.token
24
+
25
+ def self.flush
26
+ instance.flush
30
27
  end
31
28
 
32
29
  def self.post_payload_to_token(payload, token)
@@ -41,127 +38,6 @@ module RulesIO
41
38
  http.request(req)
42
39
  end
43
40
  end
44
-
45
- def self.current_user(env)
46
- if controller = env['action_controller.instance']
47
- controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
48
- end
49
- rescue
50
- nil
51
- end
52
-
53
- def self.current_actor(env)
54
- if controller = env['action_controller.instance']
55
- begin
56
- data = if RulesIO.controller_data.is_a?(String)
57
- controller.instance_eval(RulesIO.controller_data)
58
- elsif RulesIO.controller_data.is_a?(Proc) && !RulesIO.controller_data.lambda?
59
- controller.instance_eval(&RulesIO.controller_data)
60
- else
61
- {}
62
- end
63
- data = data.with_indifferent_access
64
- return data[:_actor] if data[:_actor]
65
- rescue Exception => e
66
- puts e.message
67
- puts e.backtrace.join("\n")
68
- end
69
-
70
- begin
71
- user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
72
- [:to_param, :id].each do |method|
73
- return user.send(method) if user && user.respond_to?(method)
74
- end
75
- rescue Exception => e
76
- puts e.message
77
- puts e.backtrace.join("\n")
78
- end
79
- end
80
- nil
81
- end
82
-
83
- private
84
- def self.page_event_name(request, params)
85
- if params && params['controller']
86
- "#{params['controller']}##{params['action']}"
87
- else
88
- request.path.gsub('/', '-')[1..-1]
89
- end
90
- end
91
-
92
- def self.prepare_event(event, env)
93
- event = event.with_indifferent_access
94
-
95
- current_user = current_user(env)
96
- actor = current_actor(env)
97
-
98
- if controller = env['action_controller.instance']
99
- begin
100
- data = if RulesIO.controller_data.is_a?(String)
101
- controller.instance_eval(RulesIO.controller_data)
102
- elsif RulesIO.controller_data.is_a?(Proc) && !RulesIO.controller_data.lambda?
103
- controller.instance_eval(&RulesIO.controller_data)
104
- else
105
- {}
106
- end
107
- event = data.with_indifferent_access.merge(event)
108
- rescue Exception => e
109
- RulesIO.logger.warn "RulesIO having trouble with controller_data: #{e}"
110
- end
111
- end
112
-
113
- event[:_actor] = actor || 'anonymous' unless event[:_actor].present?
114
- event[:_timestamp] ||= Time.now.to_f
115
- event[:rails_env] = Rails.env if defined?(Rails)
116
-
117
- unless env.empty?
118
- env['rack.input'].rewind
119
- request = defined?(Rails) ? ActionDispatch::Request.new(env) : ::Rack::Request.new(env)
120
- params = request.params
121
- action = page_event_name(request, params)
122
-
123
- event[:_domain] = 'JSON' if event[:_domain] == 'pageview' && params['format'] == 'json'
124
- event[:_domain] = 'XML' if event[:_domain] == 'pageview' && params['format'] == 'xml'
125
- event[:_name] ||= action
126
- event[:_from] ||= current_user.email if current_user && current_user.respond_to?(:email) && current_user.email != event[:_actor]
127
- event[:action] = action
128
- event[:request_url] = env['rulesio.request_url']
129
- event[:request_method] = env['rulesio.request_method']
130
- event[:user_agent] = request.user_agent
131
- event[:referer_url] = request.referer
132
- event[:session] = request.session
133
- parameter_filter = ::ActionDispatch::Http::ParameterFilter.new(RulesIO.filter_parameters)
134
- event[:params] = parameter_filter.filter(params)
135
- end
136
-
137
- event.reject! {|k, v| v.to_s.blank?}
138
- event
139
- end
140
-
141
- class Rack
142
- def initialize(app, options={})
143
- @app = app
144
- RulesIO.logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
145
- RulesIO.webhook_url = options[:webhook_url] || 'https://www.rules.io/events/'
146
- RulesIO.buffer = []
147
- RulesIO.filter_parameters = defined?(Rails) ? Rails.application.config.filter_parameters : []
148
- RulesIO.token = options[:token]
149
- RulesIO.queue = options[:queue] || RulesIO::MemoryQueue
150
- RulesIO.queue_options = options[:queue_options] || {}
151
- RulesIO.controller_data = options[:controller_data] || '{}'
152
- RulesIO.disable_sending_events = options[:disable_sending_events] || false
153
- end
154
-
155
- def call(env)
156
- RulesIO.buffer = []
157
- request = ::Rack::Request.new(env)
158
- env['rulesio.request_url'] = request.url
159
- env['rulesio.request_method'] = request.request_method
160
- @app.call(env)
161
- ensure
162
- RulesIO.flush(env)
163
- end
164
- end
165
41
  end
166
42
 
167
43
  require 'rulesio/railtie' if defined?(Rails)
@@ -5,7 +5,7 @@ require "rulesio/girl_friday_queue"
5
5
  MockController = Class.new
6
6
 
7
7
  class TestConfiguration < Test::Unit::TestCase
8
- REDIS = Object.new
8
+ REDIS = APP = Object.new
9
9
 
10
10
  STRING_CONFIGURATION = <<-CONFIG
11
11
  token 'FOOF'
@@ -30,23 +30,23 @@ class TestConfiguration < Test::Unit::TestCase
30
30
  PROC_CONFIGURATION = <<-CONFIG
31
31
  token 'FOOF'
32
32
  queue RulesIO::GirlFridayQueue, :store => GirlFriday::Store::Redis, :store_config => { :pool => TestConfiguration::REDIS }
33
- controller_data Proc.new { {:_actor => self.current_user.try(:email) || 'anonymous'} }
33
+ controller_data proc { {:_actor => self.current_user.try(:email) || 'anonymous'} }
34
34
 
35
35
  middleware :users do
36
- ignore_if_controller Proc.new { self.is_a?(MockController) && ["create", "recent"].include?(params[:action]) }
36
+ ignore_if_controller proc { self.is_a?(MockController) && ["create", "recent"].include?(params[:action]) }
37
37
  end
38
38
  CONFIG
39
39
 
40
40
  setup do
41
- Rails.expects(:logger)
42
- Rails.expects(:application).returns(mock(:config => mock(:filter_parameters => [])))
41
+ Rails.stubs(:logger)
42
+ Rails.stubs(:application).returns(stub(:config => stub(:filter_parameters => [])))
43
43
  @rc = RulesIO::RailsConfigurator.new
44
44
  end
45
45
 
46
46
  test "general configuration works" do
47
- user_middleware = @rc.instance_eval do
47
+ rack_middleware, user_middleware = @rc.instance_eval do
48
48
  eval TestConfiguration::STRING_CONFIGURATION, binding, __FILE__, __LINE__
49
- RulesIO::Rack.new(Object.new,
49
+ a = RulesIO::Rack.new(APP,
50
50
  :webhook_url => @webhook_url,
51
51
  :disable_sending_events => @disable,
52
52
  :token => @token,
@@ -54,18 +54,18 @@ class TestConfiguration < Test::Unit::TestCase
54
54
  :queue_options => @queue_options,
55
55
  :controller_data => @controller_data
56
56
  )
57
- RulesIO::Users.new(Object.new, @middlewares[:users].configuration)
57
+ b = RulesIO::Users.new(APP, @middlewares[:users].configuration)
58
+ [a,b]
58
59
  end
59
60
  assert_not_nil @rc.middlewares
60
- assert_equal 'FOOF', RulesIO.token
61
- assert_equal RulesIO::GirlFridayQueue, RulesIO.queue
61
+ assert_equal 'FOOF', rack_middleware.token
62
62
  assert_equal 'self.is_a?(MockController) && ["create", "recent"].include?(params[:action])', user_middleware.instance_variable_get(:@options)[:ignore_if_controller]
63
63
  end
64
64
 
65
65
  test "configuring blocks as strings works" do
66
- user_middleware = @rc.instance_eval do
66
+ rack_middleware, user_middleware = @rc.instance_eval do
67
67
  eval TestConfiguration::STRING_CONFIGURATION, binding, __FILE__, __LINE__
68
- RulesIO::Rack.new(Object.new,
68
+ a = RulesIO::Rack.new(APP,
69
69
  :webhook_url => @webhook_url,
70
70
  :disable_sending_events => @disable,
71
71
  :token => @token,
@@ -73,7 +73,8 @@ class TestConfiguration < Test::Unit::TestCase
73
73
  :queue_options => @queue_options,
74
74
  :controller_data => @controller_data
75
75
  )
76
- RulesIO::Users.new(Object.new, @middlewares[:users].configuration)
76
+ b = RulesIO::Users.new(APP, @middlewares[:users].configuration)
77
+ [a,b]
77
78
  end
78
79
 
79
80
  assert @rc.middlewares[:users].configuration[:ignore_if_controller].is_a? String
@@ -83,14 +84,14 @@ class TestConfiguration < Test::Unit::TestCase
83
84
  stub_controller_instance = stub(:current_user => stub(:email => 'email@example.com'), :params => {:action => 'update'})
84
85
  stub_controller_instance.expects(:is_a?).with(MockController).returns(true)
85
86
  env = {'action_controller.instance' => stub_controller_instance}
86
- assert_equal 'email@example.com', RulesIO.current_actor(env)
87
+ assert_equal 'email@example.com', user_middleware.send(:current_actor, env)
87
88
  assert_equal false, user_middleware.send(:should_be_ignored, env)
88
89
  end
89
90
 
90
91
  test "configuring blocks as lambdas DOES NOT work" do
91
- user_middleware = @rc.instance_eval do
92
+ rack_middleware, user_middleware = @rc.instance_eval do
92
93
  eval TestConfiguration::BLOCK_CONFIGURATION
93
- RulesIO::Rack.new(Object.new,
94
+ a = RulesIO::Rack.new(APP,
94
95
  :webhook_url => @webhook_url,
95
96
  :disable_sending_events => @disable,
96
97
  :token => @token,
@@ -98,7 +99,8 @@ class TestConfiguration < Test::Unit::TestCase
98
99
  :queue_options => @queue_options,
99
100
  :controller_data => @controller_data
100
101
  )
101
- RulesIO::Users.new(Object.new, @middlewares[:users].configuration)
102
+ b = RulesIO::Users.new(APP, @middlewares[:users].configuration)
103
+ [a,b]
102
104
  end
103
105
 
104
106
  assert @rc.middlewares[:users].configuration[:ignore_if_controller].is_a? Proc
@@ -111,14 +113,14 @@ class TestConfiguration < Test::Unit::TestCase
111
113
  stub_controller_instance.expects(:is_a?).never
112
114
  env = {'action_controller.instance' => stub_controller_instance}
113
115
  # returns nil, because lambda doesn't work right with instance_eval
114
- assert_equal nil, RulesIO.current_actor(env)
116
+ assert_equal nil, user_middleware.send(:current_actor, env)
115
117
  assert_equal false, user_middleware.send(:should_be_ignored, env)
116
118
  end
117
119
 
118
120
  test "configuring blocks as Procs works" do
119
121
  user_middleware = @rc.instance_eval do
120
122
  eval TestConfiguration::PROC_CONFIGURATION, binding, __FILE__, __LINE__
121
- RulesIO::Rack.new(Object.new,
123
+ RulesIO::Rack.new(APP,
122
124
  :webhook_url => @webhook_url,
123
125
  :disable_sending_events => @disable,
124
126
  :token => @token,
@@ -126,7 +128,7 @@ class TestConfiguration < Test::Unit::TestCase
126
128
  :queue_options => @queue_options,
127
129
  :controller_data => @controller_data
128
130
  )
129
- RulesIO::Users.new(Object.new, @middlewares[:users].configuration)
131
+ RulesIO::Users.new(APP, @middlewares[:users].configuration)
130
132
  end
131
133
 
132
134
  assert @rc.middlewares[:users].configuration[:ignore_if_controller].is_a? Proc
@@ -137,7 +139,7 @@ class TestConfiguration < Test::Unit::TestCase
137
139
  stub_controller_instance = stub(:current_user => stub(:email => 'email@example.com'), :params => {:action => 'update'})
138
140
  stub_controller_instance.expects(:is_a?).with(MockController).returns(true)
139
141
  env = {'action_controller.instance' => stub_controller_instance}
140
- assert_equal 'email@example.com', RulesIO.current_actor(env)
142
+ assert_equal 'email@example.com', user_middleware.send(:current_actor, env)
141
143
  assert_equal false, user_middleware.send(:should_be_ignored, env)
142
144
  end
143
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulesio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.10.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-07 00:00:00.000000000 Z
13
+ date: 2013-02-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -155,10 +155,12 @@ files:
155
155
  - Rakefile
156
156
  - lib/rulesio.rb
157
157
  - lib/rulesio/active_record_extension.rb
158
+ - lib/rulesio/base.rb
158
159
  - lib/rulesio/exceptions.rb
159
160
  - lib/rulesio/girl_friday_queue.rb
160
161
  - lib/rulesio/helpers.rb
161
162
  - lib/rulesio/memory_queue.rb
163
+ - lib/rulesio/rack.rb
162
164
  - lib/rulesio/railtie.rb
163
165
  - lib/rulesio/users.rb
164
166
  - lib/rulesio/version.rb