Class: Riaction::ProfileCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/riaction/profile_creator.rb

Class Method Summary collapse

Class Method Details

.perform(klass_name, id, attempt = 0) ⇒ Object



8
9
10
11
12
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
# File 'lib/riaction/profile_creator.rb', line 8

def self.perform(klass_name, id, attempt=0)
  if klass_name.constantize.riactionary? && 
    klass_name.constantize.riaction_profile? &&
    klass_name.constantize.riaction_profile_types_defined > 0
    record = klass_name.constantize.find_by_id!(id)
    iactionable_api = IActionable::Api.new
    record.riaction_profile_keys.each_pair do |profile_type, ids|
      identifiers = ids.to_a
      first_defined = identifiers.shift
      iactionable_api.create_profile(profile_type.to_s, first_defined.first.to_s, first_defined.last.to_s, record.riaction_set_profile(profile_type).riaction_profile_display_name )
      identifiers.each do |identifier|
        iactionable_api.add_profile_identifier(profile_type.to_s, first_defined.first.to_s, first_defined.last.to_s, identifier.first.to_s, identifier.last.to_s)
      end
    end
  else
    raise ::Riaction::RuntimeError.new("#{klass_name} does not define any riaction profiles")
  end
rescue ActiveRecord::RecordNotFound => e
  # event_object no longer exists; no means to recover
rescue IActionable::Error::BadRequest => e
  # This should only be thrown if the profile type names specified in the model don't match what's on IActionable's dashboard 
  raise e
rescue IActionable::Error::Internal, Faraday::Error::TimeoutError, Timeout::Error => e
  # upon an intenal error from IActionable, retry some set number of times by requeueing the task through Resque 
  # after the max number of attempts, re-raise
  if attempt < ::Riaction::Constants.retry_attempts_for_internal_error
    Resque.enqueue(self, klass_name, id, attempt+1)
  else
    raise e
  end
end