Class: IntegrationWrapper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/integration_wrapper/base.rb

Constant Summary collapse

MODE_ONLINE =

2 modes

"online"
MODE_OFFLINE =
"offline"
@@mode =

class methods

Rails.env.eql?("production") ? MODE_ONLINE : MODE_OFFLINE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



25
26
27
# File 'lib/integration_wrapper/base.rb', line 25

def initialize(params = {})
  #@mode = Rails.env.eql?("production") ? MODE_ONLINE : MODE_OFFLINE
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

If an action comes in, say :foo, then call the foo_online class method if mode is online, and foo_offline if mode is offline.

def self.perform_action action_name, options = {}
  self.send("#{action_name}_#{@@mode}", options)
end


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/integration_wrapper/base.rb', line 43

def method_missing sym, *args, &block
  # First check if the method we're trying to call is available.
  name = "#{sym}_#{@@mode}"
  # Now send
  begin
    self.send name, *args, &block
  rescue NoMethodError => e      
    puts "Error trying to call method #{name}. Maybe you forgot to add it? I need all methods to be suffixed with either #{IntegrationWrapper::MODE_ONLINE} or #{IntegrationWrapper::MODE_OFFLINE}. Example: foo_online. Methods: #{methods.inspect}. Error was: #{e}."
    puts "Trace:"
    puts e.backtrace
    raise e
  end
end

Class Method Details

.modeObject



29
30
31
# File 'lib/integration_wrapper/base.rb', line 29

def self.mode
  @@mode
end

.mode=(thing) ⇒ Object



33
34
35
36
# File 'lib/integration_wrapper/base.rb', line 33

def self.mode= thing
  raise "Mode must be MODE_ONLINE or MODE_OFFLINE" unless thing.eql?(MODE_ONLINE) || thing.eql?(MODE_OFFLINE)
  @@mode = thing
end