external_services 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTA0OWIzM2UxMzM0ZjllMjVkYmNhNzJiZGIyNjgxNmUzY2YzYTg5NA==
4
+ OTkwOWFmMmQwMzBhNzRmOWQyYWMwNTIwOTc2YzU2MGRlMDlhOTMzNA==
5
5
  data.tar.gz: !binary |-
6
- MGY0MmY1ZmUzYzE3OGQ3MTIwZWIwZTAwYzRiZWE4MzQ1NjVlNTkzZg==
6
+ YTMwMTU4OGNhZWFlYzhmMWQ2ODg1NjFjNjkwMzhkNjdiY2M4NmEwYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTc3ZmQxN2JlZjVkODU3NjNkYTA1NmVhNWYzZmYxZDcyMWIzOThmM2RhMGI5
10
- NjU1OGQ4M2MyMzg0NjUzMGI0YTUzNzllMmM4ZTA2ZmNkZWRlYWIxMTAzZTc1
11
- ZmMxZGE2N2MzMWMwN2RiNTNlYmNjMGZkNjYxYjFmNGU0MzI4ODE=
9
+ ZjEyMWQ2YmY0NzAxNWI5ZDVjYmYwMGM3NTYxOTBjOTc3ZmE2YTYxZjIxM2Zj
10
+ NjczMjA4MjRiYzkxODYyYTE1MWU2ZDY4OWI0ZTVlMDliNjE2MTAwNjRmNjRi
11
+ ODE1MjhlYjZkMGY3YzBhYmMxZjY2MTU0NDk1NTNhNjI1NmEzNGM=
12
12
  data.tar.gz: !binary |-
13
- MDI5MWM4MTc0OTJhOTYyNzRhZTZiMDQ4NzdlNzA4YmRiYmM0YmEwYzAyZDQ2
14
- NWZiZGJjZWMwOTQwZTcyZTg5NTYyNDVhYzZmMGZiN2RlOTE1MGQyMzhlZGNh
15
- ODNlNzNlZWZhOWJjNDkzNDI3YWEyMzBmYWE2Njc2NWRkMmE4NDA=
13
+ NzU2OWI1Yzk4ZDBkYjQxMGYzZGE0ZTIzYTBlYzlhZGI3MzQyZDFhMDE2MmEz
14
+ Mzg2ZmU5YTEyYzQwNjU0ZWUzZDFkNWRiOTU3NmQ0ZWI1MmRkOTRlYTQ3NGRl
15
+ NTExZGE3M2Y1YjE5OTdlNGVmMzkzMWRiMzVhNDYzN2JhMTAwNzQ=
@@ -0,0 +1,38 @@
1
+ module ExternalServices
2
+ module Action
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ scope :processed, -> { where.not(processed_at: nil) }
7
+ scope :unprocessed, -> { where(processed_at: nil) }
8
+
9
+ after_commit :kick_active_job
10
+ end
11
+
12
+ def processed?
13
+ processed_at.present?
14
+ end
15
+
16
+ def set_processed!
17
+ update_attributes! processed_at: Time.now
18
+ end
19
+
20
+ def execute!
21
+ raise NotImplementedError
22
+ end
23
+
24
+ def kick_active_job
25
+ return if api_disabled?
26
+
27
+ job_class.set(queue: queue).perform_later(id)
28
+ end
29
+
30
+ module ClassMethods
31
+ def perform_unprocessed
32
+ Rails.logger.info "Running unprocessed #{self.class.name.demodulize} api actions..."
33
+
34
+ unprocessed.each(&:kick_active_job)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,9 @@
1
1
  module ExternalServices
2
2
  class ApiAction < ::ActiveRecord::Base
3
+ include ExternalServices::Action
4
+
5
+ attr_accessor :async
6
+
3
7
  self.table_name = :external_services_api_actions
4
8
 
5
9
  belongs_to :initiator, polymorphic: true
@@ -19,23 +23,11 @@ module ExternalServices
19
23
 
20
24
  before_create :calculate_signature
21
25
 
22
- def processed?
23
- processed_at.present?
24
- end
25
-
26
- def set_processed!
27
- update_attributes! processed_at: Time.now
28
- end
29
-
30
26
  def initiator_class
31
27
  # Need to use initiator object for STI in polymorphic.. But still will be bugs with deleted STI object
32
28
  initiator.try(:class) || initiator_type.constantize
33
29
  end
34
30
 
35
- def api_disabled?
36
- initiator_class.send(:"#{self.class.to_s.demodulize.underscore}_api_disabled")
37
- end
38
-
39
31
  def change_external_id?
40
32
  options['change_external_id']
41
33
  end
@@ -44,20 +36,8 @@ module ExternalServices
44
36
  "ExternalServices::#{self.class.to_s.demodulize}ApiJob".constantize
45
37
  end
46
38
 
47
- def kick_active_job
48
- return if api_disabled?
49
-
50
- job_class.set(queue: queue).perform_later(id)
51
- end
52
-
53
- def self.perform_unprocessed
54
- Rails.logger.info "Running unprocessed #{self.class.name.demodulize} api actions..."
55
-
56
- unprocessed.each(&:kick_active_job)
57
- end
58
-
59
- def execute!
60
- raise NotImplementedError
39
+ def api_disabled?
40
+ initiator_class.send(:"#{self.class.to_s.demodulize.underscore}_api_disabled")
61
41
  end
62
42
 
63
43
  protected
@@ -83,5 +63,16 @@ module ExternalServices
83
63
  def path_format_correctness
84
64
  errors.add(:path, :invalid) if path =~ %r{//}
85
65
  end
66
+
67
+ def async
68
+ @async.nil? ? true : @async
69
+ end
70
+
71
+ private
72
+
73
+ def create_or_update(*args)
74
+ return true unless async
75
+ super
76
+ end
86
77
  end
87
78
  end
@@ -129,11 +129,6 @@ module ExternalServices
129
129
  end
130
130
  protected :"#{name}_on_destroy"
131
131
 
132
- define_method :"#{name}_on_revive" do
133
- public_send(service_assoc).on_subject_revive(self) unless only_api_actions
134
- end
135
- protected :"#{name}_on_revive"
136
-
137
132
  protected def halt_on_external_services_syncing
138
133
  if external_services_syncing?
139
134
  errors.add :base, :external_services_sync_in_process
@@ -188,10 +183,10 @@ module ExternalServices
188
183
  def define_external_service_helper_methods(name, _options = {})
189
184
  ## subject class methods
190
185
  helpers_class_module = Module.new do
191
- define_method :"with_#{name}_api_for" do |synced: [], for_syncing: [], &b|
192
- return if ([synced] + [for_syncing]).flatten.select(&:"#{name}_api_disabled").any?
193
186
 
187
+ define_method :"with_#{name}_api_for" do |synced: [], for_syncing: [], &b|
194
188
  unsynced = [synced].flatten.select { |o| o.send("#{name}_id").nil? }
189
+
195
190
  if unsynced.any?
196
191
  objects = unsynced.map { |o| "#{o.class.name} (id=#{o.id})" }.join(', ')
197
192
  raise "[#{name}] Trying to work with an unsynced objects: #{objects}"
@@ -226,6 +221,19 @@ module ExternalServices
226
221
  send :"#{name}_api_disabled=", old
227
222
  end
228
223
  end
224
+
225
+ define_method :"find_all_by_#{name}_ids" do |ids|
226
+ conditions = { external_services: { type: get_service_class(name).name, external_id: ids } }
227
+ includes(:"#{name}_service").where(conditions)
228
+ end
229
+
230
+ define_method :"#{name}_synced" do
231
+ includes(:"#{name}_service").where.not(external_services: { external_id: [nil, ''] })
232
+ end
233
+
234
+ define_method :"not_#{name}_synced" do
235
+ includes(:"#{name}_service").where(external_services: { external_id: [nil, ''] })
236
+ end
229
237
  end
230
238
 
231
239
  ## subject methods
@@ -253,18 +261,26 @@ module ExternalServices
253
261
  path = args[:path] || send(:"#{name}_api_path")
254
262
  data = args[:data] || send(:"#{name}_api_data")
255
263
  options = args[:options] || {}
264
+ async = args[:async].nil? ? true : args[:async]
256
265
 
257
266
  options[:change_external_id] = true if options[:change_external_id].nil?
258
267
 
259
- "ExternalServices::ApiActions::#{name.to_s.camelize}".constantize.create!(
268
+ action = "ExternalServices::ApiActions::#{name.to_s.camelize}".constantize.new(
260
269
  initiator: self,
261
270
  name: args[:name] || self.class.send(:"#{name}_api_name"),
262
271
  method: method,
263
272
  path: path,
264
273
  data: data,
265
274
  queue: args[:queue],
266
- options: options
275
+ options: options,
276
+ async: async
267
277
  )
278
+
279
+ if async
280
+ action.save!
281
+ else
282
+ action.execute!
283
+ end
268
284
  end
269
285
  end
270
286
 
@@ -1,3 +1,3 @@
1
1
  module ExternalServices
2
- VERSION = '0.1.7'.freeze
2
+ VERSION = '0.1.8'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: external_services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Gnuskov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2016-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -75,6 +75,7 @@ files:
75
75
  - README.md
76
76
  - Rakefile
77
77
  - app/jobs/external_services/api_job.rb
78
+ - app/models/concerns/external_services/action.rb
78
79
  - app/models/external_services/api_action.rb
79
80
  - app/models/external_services/service.rb
80
81
  - bin/rails