Class: DiscourseSubscriptionClient::Subscriptions
- Inherits:
-
Object
- Object
- DiscourseSubscriptionClient::Subscriptions
show all
- Defined in:
- lib/discourse_subscription_client/subscriptions.rb,
lib/discourse_subscription_client/subscriptions/result.rb,
lib/discourse_subscription_client/subscriptions/update_result.rb
Defined Under Namespace
Classes: Result, UpdateResult
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Subscriptions.
5
6
7
|
# File 'lib/discourse_subscription_client/subscriptions.rb', line 5
def initialize
@suppliers = SubscriptionClientSupplier.authorized
end
|
Class Method Details
.update ⇒ Object
9
10
11
|
# File 'lib/discourse_subscription_client/subscriptions.rb', line 9
def self.update
new.update
end
|
Instance Method Details
#data_matches_subscription(data, subscription) ⇒ Object
91
92
93
|
# File 'lib/discourse_subscription_client/subscriptions.rb', line 91
def data_matches_subscription(data, subscription)
data.required.all? { |k, v| subscription.send(k.to_s) == v }
end
|
#update ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/discourse_subscription_client/subscriptions.rb', line 13
def update
return unless SiteSetting.subscription_client_enabled
DiscourseSubscriptionClient::Resources.find_all
@result = DiscourseSubscriptionClient::Subscriptions::UpdateResult.new
if @suppliers.blank?
@result.no_suppliers
else
@suppliers.each do |supplier|
update_supplier(supplier)
end
end
if @result.errors.any?
@result.errors.each do |error|
Rails.logger.error "DiscourseSubscriptionClient::Subscriptions.update: #{error}"
end
end
if SiteSetting.subscription_client_verbose_logs && @result.infos.any?
@result.infos.each do |info|
Rails.logger.info "DiscourseSubscriptionClient::Subscriptions.update: #{info}"
end
end
DiscourseEvent.trigger(:subscription_client_subscriptions_updated, @result)
@result.errors.blank?
end
|
#update_supplier(supplier) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/discourse_subscription_client/subscriptions.rb', line 45
def update_supplier(supplier)
resources = supplier.resources
return unless resources.present?
request = DiscourseSubscriptionClient::Request.new(:supplier, supplier.id)
= { "User-Api-Key" => supplier.api_key }
url = "#{supplier.url}/subscription-server/user-subscriptions"
response = request.perform(url, headers: , body: { resources: resources.map(&:name) })
return (supplier.deactivate_all_subscriptions! && @result.connection_error(supplier)) if response.nil?
subscription_data = @result.retrieve_subscriptions(supplier, response)
return supplier.deactivate_all_subscriptions! if subscription_data.blank?
supplier.subscriptions.each do |subscription|
has_match = false
subscription_data.each do |data|
if data_matches_subscription(data, subscription)
data.subscription = subscription
has_match = true
end
end
subscription.deactivate! unless has_match
end
subscription_data.each do |data|
if data.subscription.present?
data.subscription.update(subscribed: true)
data.subscription.touch
@result.updated_subscription(supplier, subscription_ids: data.required)
else
subscription = SubscriptionClientSubscription.create!(data.create.merge(subscribed: true))
if subscription
@result.created_subscription(supplier, subscription_ids: data.required)
else
@result.failed_to_create_subscription(supplier, subscription_ids: data.required)
end
end
data.resource.update(data.resource_data) if data.resource.present? && data.resource_data.present?
end
end
|