resource_subscriber 0.1.0 → 0.1.1

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: 4a9dae7ba936613f12121ad3a9f9410d0d2ab58b
4
- data.tar.gz: b326e9c13469a191400a661e301a5021210aeb07
3
+ metadata.gz: 4160073b08655c15bb99cf337f8ca84e7966a781
4
+ data.tar.gz: 94ce92345da4ae62c2cbded29c88f5b3e36c2535
5
5
  SHA512:
6
- metadata.gz: 42ac1ee74dadc403cd5f9ec61ebd107fc19bd02f80c748cbd3dd1f7e7be55d0310d007ca4e6ab399a83206fec0743820f23c6e8c46fdf92891aa8e4a669597b9
7
- data.tar.gz: 4541121bb3043aa70baf9b98045ec50dfb802bf859ab626059f518333827cd589a94cc2cf71a1a3afe1cd927bbc06f72719c761bef273cb7b8d99fa0d270ce26
6
+ metadata.gz: fc3f96119816c0246cc07f85659608987dc54f7f1ab2581d0b6ee1b765a8231bf2dd55cce0b8c4b97a14edff6d2e582d7be3ba08ee678c97ceee8a6bb5aa8757
7
+ data.tar.gz: 38adff74a43d8c8350c5e5900672ff2f175ae9d5b62b3b4a64bda4c359023a7fa9e031d2a87df5baff369045f3b4772814cb9dc22505ddba5b44cd1bdba0bb7b
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /vendor/
@@ -1,24 +1,31 @@
1
1
  require "resource_subscriber/version"
2
2
  require 'active_support/all'
3
3
  require 'action_subscriber'
4
- require 'trax_core'
5
4
 
6
5
  module ResourceSubscriber
7
6
  extend ::ActiveSupport::Autoload
8
7
 
9
8
  autoload :AutoInject
10
9
  autoload :Changes
10
+ autoload :Configuration
11
11
  autoload :Message
12
12
  autoload :Middlewares
13
13
  autoload :Railtie
14
14
  autoload :Resourceful
15
15
  autoload :Publishable
16
16
  autoload :Publisher
17
- autoload :PublisherConfig
18
17
 
19
18
  def self.load_railtie
20
19
  ::ResourceSubscriber::Railtie
21
20
  end
22
21
 
22
+ def self.configuration
23
+ @configuration ||= ::ResourceSubscriber::Configuration.with_defaults
24
+ end
25
+
26
+ def self.configure
27
+ yield(self.configuration) if block_given?
28
+ end
29
+
23
30
  load_railtie if defined?(Rails)
24
31
  end
@@ -0,0 +1,10 @@
1
+ module ResourceSubscriber
2
+ class Configuration < ::ActiveSupport::OrderedOptions
3
+ def self.with_defaults
4
+ new.tap do |c|
5
+ c.exchange = 'events'
6
+ c.publishing_options = { :content_type => 'application/json' }.freeze
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,7 +1,23 @@
1
1
  module ResourceSubscriber
2
- class Message < ::Trax::Core::Types::Struct
3
- string :resource_type
4
- json :resource
5
- json :changes
2
+ class Message
3
+ include ActiveModel::Serializers::JSON
4
+
5
+ attr_accessor :resource_type, :resource, :changes
6
+
7
+ def initialize(resource)
8
+ @resource_type = resource.class.base_class.name
9
+ @changes = resource.changes.blank? ? resource.previous_changes : resource.changes
10
+ @resource = resource.attributes
11
+ end
12
+
13
+ def attributes=(hash)
14
+ hash.each do |key, value|
15
+ send("#{key}=", value)
16
+ end
17
+ end
18
+
19
+ def attributes
20
+ instance_values
21
+ end
6
22
  end
7
23
  end
@@ -8,13 +8,14 @@ module ResourceSubscriber
8
8
  after_commit :publish_destroyed, :on => :destroy
9
9
 
10
10
  self.class_attribute :resource_publisher
11
- self.resource_publisher = ::ResourceSubscriber::Publisher.new(model: self, config: ::ResourceSubscriber::PublisherConfig.new)
11
+
12
+ configuration = ::ResourceSubscriber.configuration.deep_dup
13
+ self.resource_publisher = ::ResourceSubscriber::Publisher.new(model: self, config: configuration)
12
14
  end
13
15
 
14
16
  module ClassMethods
15
- def publish_to(routing_key, exchange:'events')
16
- self.resource_publisher.config.exchange = exchange
17
- self.resource_publisher.config.routing_key = routing_key
17
+ def publish_to(routing_key, &block)
18
+ self.resource_publisher.publish_as(routing_key, &block)
18
19
  end
19
20
  end
20
21
 
@@ -4,23 +4,14 @@ module ResourceSubscriber
4
4
 
5
5
  required_dependencies :model, :config
6
6
 
7
- def resource_base_class_name
8
- @resource_base_class_name ||= model.base_class.name
9
- end
10
-
11
7
  def publish_resource_message(action, resource)
12
- message = ::ResourceSubscriber::Message.new(
13
- :resource => resource.attributes,
14
- :resource_type => resource_base_class_name,
15
- :changes => resource.previous_changes
16
- ).to_json
17
-
18
- ::ActionSubscriber::Publisher.publish_async("#{self.config.routing_key}.#{action}", message, self.config.exchange, :content_type => 'application/json')
8
+ message = ::ResourceSubscriber::Message.new(resource)
9
+ ::ActionSubscriber::Publisher.publish_async("#{self.config.routing_key}.#{action}", message.to_json, self.config.exchange, self.config.publishing_options)
19
10
  end
20
11
 
21
- def publish_as(routing_key, exchange:'events')
22
- self.config[:exchange] = exchange
23
- self.config[:routing_key] = routing_key
12
+ def publish_as(routing_key)
13
+ yield(self.config) if block_given?
14
+ self.config.routing_key = routing_key
24
15
  end
25
16
  end
26
17
  end
@@ -1,3 +1,3 @@
1
1
  module ResourceSubscriber
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -21,7 +21,6 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency "activesupport"
23
23
  spec.add_dependency "action_subscriber"
24
- spec.add_dependency "trax_core"
25
24
 
26
25
  spec.add_development_dependency "bundler", "~> 1.11"
27
26
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resource_subscriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Ayre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-17 00:00:00.000000000 Z
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: trax_core
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -113,13 +99,13 @@ files:
113
99
  - lib/resource_subscriber.rb
114
100
  - lib/resource_subscriber/auto_inject.rb
115
101
  - lib/resource_subscriber/changes.rb
102
+ - lib/resource_subscriber/configuration.rb
116
103
  - lib/resource_subscriber/message.rb
117
104
  - lib/resource_subscriber/middlewares.rb
118
105
  - lib/resource_subscriber/middlewares/has_changes.rb
119
106
  - lib/resource_subscriber/middlewares/resourceful.rb
120
107
  - lib/resource_subscriber/publishable.rb
121
108
  - lib/resource_subscriber/publisher.rb
122
- - lib/resource_subscriber/publisher_config.rb
123
109
  - lib/resource_subscriber/railtie.rb
124
110
  - lib/resource_subscriber/resourceful.rb
125
111
  - lib/resource_subscriber/version.rb
@@ -1,6 +0,0 @@
1
- module ResourceSubscriber
2
- class PublisherConfig < ::Trax::Core::Types::Struct
3
- string :exchange, :default => "events"
4
- string :routing_key, :default => ""
5
- end
6
- end