web-connect 0.4.14 → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0091a07c1eed416170772b0b372bf7a67d07f3b
4
- data.tar.gz: e8b5a76d60a63a5905d5789c0fcb7fbc930c008e
3
+ metadata.gz: 453fceed0cd58899614f957a73dc0b6c51241e74
4
+ data.tar.gz: ba10cbdbf8878af2c85aaeb3041794801fa47c49
5
5
  SHA512:
6
- metadata.gz: 56f5c259161af9445688c69243e03ceb03960bcda139c0bae524417dd27720da9e31760a36b9518ef277439f0778109230f3e61276adf497c6678d8141202fb3
7
- data.tar.gz: 2bbc7b22019d05b8e344174bc20bed10c44389049bfb1855c9a4406ca6143bad5012a029ceed9653a98b755139855c5fefdaa43b505756d0f31a7b4ca950febd
6
+ metadata.gz: 816141179af1d0e93461e4d585f835c2ebd00ffe38c67bcb5cc41202e59bfe5163876493324dadf6d3c99cadb03cef627c76fb28523668a0a335ba8cf8d2548e
7
+ data.tar.gz: 2b625cb75d8f2a9580a66cd8beb2ce90b8308b54764c54f93fc80d211d07c866c4e2695fa4fb3cf0c0ab806d4fd9c2d03df9dba487437dc71d54434faa17f977
data/README.md CHANGED
@@ -62,6 +62,12 @@ Netfira::WebConnect.configure do |c|
62
62
 
63
63
  # Session lifetime (omit to disable sessions)
64
64
  c.session_lifetime = 5.days
65
+
66
+ # Send new-data notifications via HTTP. Options are :none, :sync, and :async.
67
+ c.http_notifications = :async
68
+
69
+ # AMQP server to use for downstream communications.
70
+ c.amqp = 'amqps://admin:Passw0rd@localhost:5672'
65
71
  end
66
72
  ```
67
73
 
@@ -0,0 +1,52 @@
1
+ require 'bunny'
2
+
3
+ module Netfira::WebConnect
4
+ module AMQP; end
5
+ class << AMQP
6
+
7
+ def connect!
8
+ disconnect!
9
+ @connection = Bunny.new(Netfira::WebConnect.amqp_uri)
10
+ @connection.start
11
+ @channel = @connection.create_channel
12
+ @exchanges = {}
13
+ end
14
+
15
+ def connected?
16
+ !!@connection
17
+ end
18
+
19
+ def disconnect!
20
+ return unless connected?
21
+ @connection.stop
22
+ @connection = nil
23
+ @channel = nil
24
+ @exchanges = nil
25
+ end
26
+
27
+ def exchange_for_shop(shop)
28
+ @exchanges && @exchanges[shop.id || 0] ||= make_exchange_for_shop(shop)
29
+ end
30
+
31
+ def queue_name_for_shop(shop)
32
+ "shops.#{shop.id || 0}"
33
+ end
34
+
35
+ private
36
+
37
+ def make_exchange_for_shop(shop)
38
+
39
+ # Each shop will have its own exchange, and a queue bound to that exchange.
40
+ # Their names will be the same.
41
+ name = queue_name_for_shop(shop)
42
+
43
+ # Make an exchange
44
+ @connection.direct(name).tap do |exchange|
45
+
46
+ # Make a queue and bind it to the created exchange
47
+ @channel.queue(name, auto_delete: true).bind exchange, routing_key: name
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -4,7 +4,7 @@ module Netfira::WebConnect
4
4
  attr_accessor :db_table_prefix, :file_store, :authenticator, :custom_fields,
5
5
  :time_zone, :materialize_when_db_changed, :session_lifetime, :paranoia
6
6
 
7
- attr_reader :logger, :db
7
+ attr_reader :logger, :db, :http_notifications, :amqp
8
8
 
9
9
  def initialize(defaults = nil)
10
10
  defaults.each{ |k, v| __send__ :"#{k}=", v } if defaults
@@ -29,5 +29,20 @@ module Netfira::WebConnect
29
29
  Model.logger = logger if logger
30
30
  end
31
31
 
32
+ def http_notifications=(value)
33
+ allowed = %i[none sync async]
34
+ raise "Valid values for `http_notifications` are #{allowed.map { |x| ":#{x}" }.join ', '}" unless allowed.include? value
35
+ @http_notifications = value
36
+ end
37
+
38
+ def amqp=(value)
39
+ return if value == @amqp
40
+ allowed = %w[amqp amqps]
41
+ uri = URI(value)
42
+ raise "Scheme must be one of [ #{allowed.join ' | '} ]" unless allowed.include? uri.scheme
43
+ @amqp = value
44
+ AMQP.connect!
45
+ end
46
+
32
47
  end
33
48
  end
@@ -1,3 +1,5 @@
1
+ require 'net/https'
2
+
1
3
  module Netfira::WebConnect
2
4
  class Model::Record
3
5
 
@@ -20,12 +22,39 @@ module Netfira::WebConnect
20
22
  def send!
21
23
  sent!
22
24
  save!
23
- #TODO: send notification to client(s)
25
+ send_notifications!
24
26
  self
25
27
  end
26
28
 
27
29
  private
28
30
 
31
+ def send_notifications!
32
+ send_http_notification!
33
+ send_amqp_notification!
34
+ end
35
+
36
+ def send_http_notification!
37
+ case Netfira::WebConnect.http_notifications
38
+ when :sync then send_http_notification_now!
39
+ when :async then Thread.new { send_http_notification_now! }
40
+ else return
41
+ end
42
+ end
43
+
44
+ def send_http_notification_now!
45
+ uri_string = shop.settings['notify']
46
+ if uri_string && !uri_string.empty?
47
+ uri = URI(uri_string)
48
+ Net::HTTP.get_response uri if %w[http https].include? uri.scheme
49
+ end
50
+ end
51
+
52
+ AMQP_MESSAGE_TTL = 60 * 1000 # Messages should live for 60 seconds
53
+
54
+ def send_amqp_notification!
55
+ shop.amqp_publish '', type: 'fetch', expiration: AMQP_MESSAGE_TTL
56
+ end
57
+
29
58
  end
30
59
 
31
60
  end
@@ -27,6 +27,18 @@ module Netfira::WebConnect
27
27
  relation_class.with_deleted.find_by_origin_ids(args.merge shop: self).first
28
28
  end
29
29
 
30
+ def amqp_exchange
31
+ AMQP.exchange_for_shop self
32
+ end
33
+
34
+ def amqp_queue
35
+ AMQP.queue_name_for_shop self
36
+ end
37
+
38
+ def amqp_publish(message, **opts)
39
+ amqp_exchange && amqp_exchange.publish(message, **opts.merge(routing_key: amqp_queue))
40
+ end
41
+
30
42
  end
31
43
  end
32
44
 
@@ -2,6 +2,8 @@ class Netfira::WebConnect::RackApp
2
2
  module Action::Version1
3
3
  class Info < Action
4
4
  def call
5
+ brokers = []
6
+ brokers << {uri: Netfira::WebConnect.amqp_uri, queues: [shop.amqp_queue]} if Netfira::WebConnect.amqp_uri
5
7
  {
6
8
  info: {
7
9
  apiVersion: Action.latest_version,
@@ -9,7 +11,8 @@ class Netfira::WebConnect::RackApp
9
11
  schema: Netfira::WebConnect.schema,
10
12
  customFields: false,
11
13
  acceptableRequestTypes: env['WC_ACCEPTABLE_REQUEST_TYPES'] || ['unpacked'],
12
- locale: shop.locale
14
+ locale: shop.locale,
15
+ messageBrokers: brokers
13
16
  }
14
17
  }
15
18
  end
@@ -16,7 +16,17 @@ module Netfira::WebConnect
16
16
  raise NotFound unless Class === klass && klass < Model::Record
17
17
  scope = klass.where shop_id: shop.id
18
18
  method = delete_handler_exists? ? :destroy_all : :delete_all
19
- scope.__send__ method
19
+ event_args = [
20
+ [:purge, shop, klass],
21
+ [:"purge_#{path.first.underscore.pluralize}", shop]
22
+ ]
23
+ event_args.each { |args| dispatch_event [:before, :on], *args }
24
+ dispatch_event(:around, *event_args.first) do
25
+ dispatch_event(:around, *event_args.last) do
26
+ scope.__send__ method
27
+ end
28
+ end
29
+ event_args.each { |args| dispatch_event :after, *args }
20
30
  {}
21
31
  end
22
32
 
@@ -1,6 +1,6 @@
1
1
  module Netfira
2
2
  module WebConnect
3
- VERSION = '0.4.14'
3
+ VERSION = '0.4.15'
4
4
  PLATFORM_AND_VERSION = 'Rack/' << VERSION
5
5
  end
6
6
  end
@@ -70,6 +70,14 @@ module Netfira
70
70
  !!@config.paranoia
71
71
  end
72
72
 
73
+ def self.http_notifications
74
+ @config.http_notifications || :none
75
+ end
76
+
77
+ def self.amqp_uri
78
+ @config.amqp
79
+ end
80
+
73
81
  private
74
82
 
75
83
  def self.null_logger
@@ -89,3 +97,4 @@ require_relative 'web_connect/models'
89
97
  require_relative 'web_connect/components'
90
98
  require_relative 'web_connect/configuration'
91
99
  require_relative 'web_connect/schema'
100
+ require_relative 'web_connect/amqp'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.14
4
+ version: 0.4.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-23 00:00:00.000000000 Z
12
+ date: 2014-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
97
  version: '2.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: bunny
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '1.4'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.4'
98
112
  - !ruby/object:Gem::Dependency
99
113
  name: mime-types
100
114
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +135,7 @@ files:
121
135
  - README.md
122
136
  - lib/netfira.rb
123
137
  - lib/netfira/web_connect.rb
138
+ - lib/netfira/web_connect/amqp.rb
124
139
  - lib/netfira/web_connect/components.rb
125
140
  - lib/netfira/web_connect/components/checksum.rb
126
141
  - lib/netfira/web_connect/components/guid.rb