Class: Hermes::Provider

Inherits:
Object
  • Object
show all
Includes:
Extractors
Defined in:
lib/providers/provider.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extractors

#complex_extract, #extract_from, #extract_html, #extract_text, #extract_to

Constructor Details

#initialize(deliverer, options = {}) ⇒ Provider

Returns a new instance of Provider.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/providers/provider.rb', line 15

def initialize(deliverer, options = {})
  options.symbolize_keys!

  @deliverer = deliverer
  @defaults = options[:defaults]
  @credentials = (options[:credentials] || {}).symbolize_keys
  @weight = options[:weight].to_i

  if self.class._required_credentials.try(:any?)
    # provider defines required credentials, let's make sure to check we have everything we need
    if !((@credentials.keys & self.class._required_credentials) == self.class._required_credentials)
      # we're missing something, raise here for hard failure
      raise(InsufficientCredentialsError, "Credentials passed:#{@credentials.keys} do not satisfy all required:#{self.class._required_credentials}")
    end
  end

  unless @weight >= 0
    # provider weights need to be 0 (disabled), or greater than 0 to show as active
    raise(InvalidWeightError, "Provider name:#{provider_name} has invalid weight:#{@weight}")
  end
end

Class Attribute Details

._required_credentialsObject

Returns the value of attribute _required_credentials.



6
7
8
# File 'lib/providers/provider.rb', line 6

def _required_credentials
  @_required_credentials
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



13
14
15
# File 'lib/providers/provider.rb', line 13

def credentials
  @credentials
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



13
14
15
# File 'lib/providers/provider.rb', line 13

def defaults
  @defaults
end

#delivererObject (readonly)

Returns the value of attribute deliverer.



13
14
15
# File 'lib/providers/provider.rb', line 13

def deliverer
  @deliverer
end

#weightObject (readonly)

Returns the value of attribute weight.



13
14
15
# File 'lib/providers/provider.rb', line 13

def weight
  @weight
end

Class Method Details

.required_credentials(*args) ⇒ Object



8
9
10
# File 'lib/providers/provider.rb', line 8

def required_credentials(*args)
  self._required_credentials  = args.to_a
end

Instance Method Details

#message_failure(rails_message, exception) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/providers/provider.rb', line 50

def message_failure(rails_message, exception)
  Utils.log_and_puts "--- MESSAGE SEND FAILURE ---"
  Utils.log_and_puts exception.message
  Utils.log_and_puts exception.backtrace.join("\n")
  Utils.log_and_puts "--- MESSAGE SEND FAILURE ---"
  self.deliverer.handle_failure(self.class)
end

#message_success(rails_message) ⇒ Object



45
46
47
48
# File 'lib/providers/provider.rb', line 45

def message_success(rails_message)
  ActionMailer::Base.deliveries << rails_message if self.deliverer.test_mode?
  self.deliverer.handle_success(self.class.name)
end

#provider_nameObject



37
38
39
# File 'lib/providers/provider.rb', line 37

def provider_name
  self.class.name.demodulize.underscore.gsub('_provider', '')
end

#send_message(rails_message) ⇒ Object



41
42
43
# File 'lib/providers/provider.rb', line 41

def send_message(rails_message)
  raise "this is an abstract method and must be defined in the subclass"
end