Class: Gringotts::Delivery

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gringotts/delivery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strategyObject (readonly)

Returns the value of attribute strategy


7
8
9
# File 'app/models/gringotts/delivery.rb', line 7

def strategy
  @strategy
end

Instance Method Details

#deliver!Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/gringotts/delivery.rb', line 63

def deliver!
  success = false
  
  begin        
    # set the phone_number_override setting in config/gringotts.yml
    # that way, you won't be sending codes to actual people!
    # note: spec tests use a stub class with an empty deliver method
    # other note: this line contributes to indetermanistic tests
    # sometimes the Gringotts::Config is loaded before this line is called, sometimes not
    @strategy.deliver! unless (Gringotts::Config.delivery && Gringotts::Config.delivery['enabled'] == false)
    
    # only update delivered_at (success indicator) if we didn't bomb
    self.delivered_at = Time.now
    success = true
  rescue Exception => e
    # some error sending -- log message for later analysis
    self.error_message = e.message
  ensure
    self.save!  
  end
  
  return success
end

#initialize_delivery_strategyObject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/gringotts/delivery.rb', line 41

def initialize_delivery_strategy
  @strategy = nil
  valid = true
  
  # set a default delivery strategy if not initialized with one
  # TODO: can read the default strategy from Gringotts::Config for flexibility
  self.strategy_class ||= "Gringotts::DeliveryStrategies::TwilioSMSStrategy"

  begin
    @strategy = strategy_class.constantize.new(delivery: self)
  rescue Exception => e
    self.errors[:delivery_strategy] = "Requires a valid Delivery Strategy [#{e.message}]"
    valid = false
  end
  
  return valid
end

#snapshot_values_from_vaultObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/gringotts/delivery.rb', line 26

def snapshot_values_from_vault
  # these values are temporal -- if we check later, they may have changed
  # therefore, save a copy of them as part of this object for posterity's sake
  
  # record which code we're actually going to send
  self.code = self.vault.recent_code
  
  # record the phone number that we're actually going to send to
  self.phone_number = self.vault.phone_number

  # since we're not technically validating things (just setting defaults)
  # always return true so as not to block the callback chain
  return true
end

#successful?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'app/models/gringotts/delivery.rb', line 59

def successful?
  return !self.delivered_at.nil? && self.error_message.nil?
end

#verify_vaultObject


19
20
21
22
23
24
# File 'app/models/gringotts/delivery.rb', line 19

def verify_vault
  if self.vault.nil?
    self.errors[:defaults] = "Must have valid Gringotts::Vault"
    return false
  end
end