active_spy 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd704eba64b7ad9be006ae4589345b8b9ff82506
4
- data.tar.gz: 71563505e25eefc826a221fb41d38bb12ee64bf7
3
+ metadata.gz: 60c31bd6d9203cda5c3e771fe53fd1963562ba19
4
+ data.tar.gz: 3fa992ecbb2b3b5d255776df58890f0eccc4cafd
5
5
  SHA512:
6
- metadata.gz: e864cca0551ed4fe9d75a1ae201edbb6e8136aced51e727d1fa69eba2b0ace9959e9cccb673f68b41fced069639dc1e6aa0bab3a469e8644cec77bd078e12c24
7
- data.tar.gz: 14686a64308ae92bcb4ab7058ff14b6e38b5b2a64e08e0192a92d1ae425ad4c662eb063fc511e9dc6c0b740ab48654e88a89cdab5a9307a9d65822f8c7fecc0f
6
+ metadata.gz: 524b7de465121d72f2157620b84795cef249deb1d62d07bbb166287fa190401a211dbae46b46a86a76207636a1e171dbc6d078b39c2fa7dc2312669c8299424a
7
+ data.tar.gz: 8bf369915690be1c13311c59572b30cbd7579bdd1d49ca09719c93c43ee7187c3548e92d7410a4138a3063df6dd5ecf892f9f96408e8ed2cf387135a447b195e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.3
1
+ 1.2.0
data/active_spy.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: active_spy 1.1.3 ruby lib
5
+ # stub: active_spy 1.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "active_spy"
9
- s.version = "1.1.3"
9
+ s.version = "1.2.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Douglas Camata"]
14
- s.date = "2014-07-25"
14
+ s.date = "2014-07-29"
15
15
  s.description = " Watch for a method call in any class and run before/after callbacks.\n You can even watch your Rails models for events (like create, update,\n destroy), send these events to a event-runner instance and it redirect these\n events to other apps that are subscrived for them. This gem also provides\n classes that you can use to process the received events too.\n"
16
16
  s.email = "d.camata@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
39
39
  "lib/active_spy/rails/listener.rb",
40
40
  "lib/active_spy/rails/railtie.rb",
41
41
  "lib/active_spy/rails/spy.rb",
42
+ "lib/active_spy/rails/validation.rb",
42
43
  "lib/active_spy/spy/spy.rb",
43
44
  "lib/active_spy/spy/spy_list.rb",
44
45
  "lib/rails/generators/active_spy/install/install_generator.rb",
@@ -1,4 +1,7 @@
1
1
  module ActiveSpy
2
+ # Controller to handle notifications request coming from an event-runner
3
+ # instance.
4
+ #
2
5
  class NotificationsController < ActionController::Base
3
6
  def handle
4
7
  request.format = 'application/json'
@@ -43,6 +43,22 @@ module ActiveSpy
43
43
  @event_host
44
44
  end
45
45
 
46
+ # Set if the gem is in development mode or not
47
+ #
48
+ # @param [Boolean] development moded state to set
49
+ #
50
+ # @return [Boolean] development moded state to set
51
+ def development_mode(mode = nil)
52
+ @development_mode = mode unless mode.nil?
53
+ @development_mode
54
+ end
55
+
56
+ # Imperative method to set development mode.
57
+ #
58
+ def development_mode!
59
+ @development_mode = true
60
+ end
61
+
46
62
  # Set the default event-runner port
47
63
  #
48
64
  # @param [String] port to set
@@ -1,5 +1,6 @@
1
1
  require 'rest-client'
2
2
  require 'singleton'
3
+ require 'json'
3
4
 
4
5
  module ActiveSpy
5
6
  # Module to hold Rails specific classes and helpers.
@@ -40,13 +41,19 @@ module ActiveSpy
40
41
  # after callback is not explicitly defined.
41
42
  #
42
43
  def method_missing(method, *_args, &_block)
44
+ event_json = { event: get_request_params(method) }.to_json
45
+ ActiveSpy::Rails::Validation::Event.new(event_json).validate!
46
+ send_event_request(event_json) unless ActiveSpy::Configuration.development_mode
47
+ remove_is_new_method(@object)
48
+ end
49
+
50
+ # Sends the event request to the configured event-runner instance.
51
+ #
52
+ def send_event_request(event_json)
43
53
  host = ActiveSpy::Configuration.event_host
44
54
  port = ActiveSpy::Configuration.event_port
45
-
46
- event_json = { event: get_request_params(method) }.to_json
47
55
  RestClient.post "#{host}:#{port}/events", event_json,
48
56
  content_type: :json
49
- remove_is_new_method(@object)
50
57
  end
51
58
 
52
59
  # Get the event request params for a given +method+.
@@ -107,8 +107,8 @@ module ActiveSpy
107
107
  def add_hooks(hooks_to_add)
108
108
  hooks_to_add.each do |hook|
109
109
  hook = {
110
- 'hook'=> {
111
- 'class'=> hook['class'],
110
+ 'hook' => {
111
+ 'class' => hook['class'],
112
112
  'post_path' => ActiveSpy::Engine.routes.url_helpers.notifications_path(hook['post_class'].downcase),
113
113
  }
114
114
  }
@@ -24,13 +24,13 @@ module ActiveSpy
24
24
  # Set the external class of the model that we are listening to.
25
25
  #
26
26
  def self.external_class(klass)
27
- @@external_classes = [klass]
27
+ @external_classes = [klass]
28
28
  end
29
29
 
30
30
  # Set the external classes which we are listening to.
31
31
  #
32
32
  def self.external_classes(*classes)
33
- @@external_classes = classes
33
+ @external_classes = classes
34
34
  end
35
35
 
36
36
  # Convert the listener class into one or more hook hashes
@@ -38,7 +38,7 @@ module ActiveSpy
38
38
  def self.to_hook
39
39
  hooks = []
40
40
  hook_classes.each do |hook_class|
41
- hooks << { 'class' => hook_class, 'post_class'=> name.split('Listener')[0] }
41
+ hooks << { 'class' => hook_class, 'post_class' => name.split('Listener')[0] }
42
42
  end
43
43
  hooks
44
44
  end
@@ -46,7 +46,7 @@ module ActiveSpy
46
46
  # Get the classes that we are listegnig to.
47
47
  #
48
48
  def self.hook_classes
49
- return @@external_classes if defined? @@external_classes
49
+ return @external_classes if defined? @external_classes
50
50
  [name.split('Listener')[0]]
51
51
  end
52
52
 
@@ -34,17 +34,15 @@ module ActiveSpy
34
34
  # instead, it will be returned.
35
35
  #
36
36
  def dynamically_define_method_or_call_block(abstract_name, method_name)
37
- if method_name
38
- if abstract_name == method_name
39
- attr_accessor method_name
40
- else
41
- define_method method_name do
42
- send(:instance_variable_get, "@#{abstract_name}")
43
- end
44
-
45
- define_method "#{method_name}=" do |new_value|
46
- send(:instance_variable_set, "@#{abstract_name}", new_value)
47
- end
37
+ return unless method_name
38
+ if abstract_name == method_name
39
+ attr_accessor method_name
40
+ else
41
+ define_method method_name do
42
+ send(:instance_variable_get, "@#{abstract_name}")
43
+ end
44
+ define_method "#{method_name}=" do |new_value|
45
+ send(:instance_variable_set, "@#{abstract_name}", new_value)
48
46
  end
49
47
  end
50
48
  end
@@ -0,0 +1,87 @@
1
+ require 'json'
2
+
3
+ module ActiveSpy
4
+ module Rails
5
+ # Module to hold classes that are intended to validate something.
6
+ #
7
+ module Validation
8
+ # Class responsible to validate event that are send to event-runner
9
+ # instances.
10
+ #
11
+ class Event
12
+
13
+ def initialize(event_json)
14
+ @event = JSON.load(event_json)
15
+ end
16
+
17
+ # Validates the +event_json+ provided in the initializer
18
+ #
19
+ # (see #initialize)
20
+ def validate!
21
+ check_actor_key(@event['actor'])
22
+ check_realm_key(@event['realm'])
23
+ end
24
+
25
+ private
26
+
27
+ # Check if actor is valid.
28
+ #
29
+ def check_actor_key(actor)
30
+ fail ActorNotPresent if actor.nil?
31
+ errors = get_errors_from_hash(actor, :actor)
32
+ fail InvalidActor, errors unless errors.empty?
33
+ end
34
+
35
+ # Check if realm is valid.
36
+ #
37
+ def check_realm_key(realm)
38
+ fail RealmNotPresent if realm.nil?
39
+ errors = get_errors_from_hash(realm, :realm)
40
+ fail InvalidRealm, errors unless errors.empty?
41
+ end
42
+
43
+ # Get the error from +data+ regarding +hash_type+ checking if the
44
+ # required keys are being provided.
45
+ #
46
+ def get_errors_from_hash(data, hash_type)
47
+ keys = hash_type == :actor ? required_actor_keys : required_realm_keys
48
+ keys.map do |key|
49
+ "#{key} should not be empty." if data[key].nil?
50
+ end.compact.join(' ')
51
+ end
52
+
53
+ # Required keys for an actor to be valid.
54
+ #
55
+ def required_actor_keys
56
+ %w[id class login url avatar_url]
57
+ end
58
+
59
+ # Required keys for realm to be valid.
60
+ #
61
+ def required_realm_keys
62
+ %w[id class name url]
63
+ end
64
+ end
65
+
66
+ # Error when actor is not present.
67
+ #
68
+ class ActorNotPresent < RuntimeError
69
+ end
70
+
71
+ # Error when the actor is invalid.
72
+ #
73
+ class InvalidActor < RuntimeError
74
+ end
75
+
76
+ # Error when realm is not present.
77
+ #
78
+ class RealmNotPresent < RuntimeError
79
+ end
80
+
81
+ # Error when the realm is invalid.
82
+ #
83
+ class InvalidRealm < RuntimeError
84
+ end
85
+ end
86
+ end
87
+ end
@@ -62,7 +62,6 @@ module ActiveSpy
62
62
  def patch(klass, method)
63
63
  old_method = nil
64
64
  ActiveSupport::Inflector.constantize(klass).class_eval do
65
-
66
65
  old_method = instance_method(method)
67
66
  define_method method do |*args, &block|
68
67
  send(:invoke_callback, method, :before)
data/lib/active_spy.rb CHANGED
@@ -2,13 +2,17 @@ require 'active_spy/configuration'
2
2
  require 'active_spy/base'
3
3
  require 'active_spy/spy/spy'
4
4
  require 'active_spy/spy/spy_list'
5
- require 'active_spy/rails/base' if defined?(Rails)
6
- require 'active_spy/rails/spy' if defined?(Rails)
7
- require 'active_spy/rails/railtie' if defined?(Rails)
8
- require 'active_spy/rails/engine' if defined?(Rails)
9
- require 'active_spy/rails/engine' if defined?(Rails)
10
- require 'active_spy/rails/hook_list' if defined?(Rails)
11
- require 'active_spy/rails/listener'
5
+
6
+ if defined?(Rails)
7
+ require 'active_spy/rails/base'
8
+ require 'active_spy/rails/spy'
9
+ require 'active_spy/rails/railtie'
10
+ require 'active_spy/rails/engine'
11
+ require 'active_spy/rails/engine'
12
+ require 'active_spy/rails/hook_list'
13
+ require 'active_spy/rails/listener'
14
+ require 'active_spy/rails/validation'
15
+ end
12
16
 
13
17
  # Base module for the gem
14
18
  #
@@ -29,11 +33,11 @@ module ActiveSpy
29
33
  def self.register_service
30
34
  host = ActiveSpy::Configuration.event_host
31
35
  port = ActiveSpy::Configuration.event_port
32
- @@base_url = "#{host}:#{port}/services"
36
+ @base_url = "#{host}:#{port}/services"
33
37
 
34
38
  return if self.service_registered?
35
39
  service = { service: ActiveSpy::Configuration.settings }.to_json
36
- RestClient.post(@@base_url, service, content_type: :json)
40
+ RestClient.post(@base_url, service, content_type: :json)
37
41
  end
38
42
 
39
43
  # @!method self.service_registered?
@@ -43,7 +47,7 @@ module ActiveSpy
43
47
  def self.service_registered?
44
48
  name = ActiveSpy::Configuration.name
45
49
  begin
46
- r = RestClient.get "#{@@base_url}/#{name.downcase.gsub(' ', '-').strip}"
50
+ RestClient.get "#{@base_url}/#{name.downcase.gsub(' ', '-').strip}"
47
51
  rescue RestClient::ResourceNotFound
48
52
  return false
49
53
  else
@@ -12,7 +12,7 @@ module ActiveSpy
12
12
  # The source for templates
13
13
  #
14
14
  def self.source_root
15
- @@_active_spy_source_root ||= File.expand_path("../templates", __FILE__)
15
+ @@_active_spy_source_root ||= File.expand_path('../templates', __FILE__)
16
16
  end
17
17
 
18
18
  # Creates a config file based in the +active_spy.yml+ file.
@@ -26,7 +26,7 @@ module ActiveSpy
26
26
  def inject_config_into_environment
27
27
  content = File.read(File.join(@@_active_spy_source_root, 'initializer.rb'))
28
28
 
29
- File.open("config/environment.rb", "a+") do |f|
29
+ File.open('config/environment.rb', 'a+') do |f|
30
30
  f << content unless f.read.include?(content)
31
31
  end
32
32
  end
@@ -5,4 +5,3 @@ if Rails.env.production?
5
5
  ActiveSpy.register_service
6
6
  ActiveSpy::Rails::HookList.register
7
7
  end
8
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_spy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Camata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-25 00:00:00.000000000 Z
11
+ date: 2014-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -225,6 +225,7 @@ files:
225
225
  - lib/active_spy/rails/listener.rb
226
226
  - lib/active_spy/rails/railtie.rb
227
227
  - lib/active_spy/rails/spy.rb
228
+ - lib/active_spy/rails/validation.rb
228
229
  - lib/active_spy/spy/spy.rb
229
230
  - lib/active_spy/spy/spy_list.rb
230
231
  - lib/rails/generators/active_spy/install/install_generator.rb