Class: Merb::SmsController

Inherits:
AbstractController
  • Object
show all
Defined in:
lib/merb/sms_controller.rb

Overview

The SMS controller lets you send text messages seamlessly from within merb to get started add a dependency to your new merb application then do:

$ rake signupto:setup

This then generates a configuration file where you must fill it in with your signup.to account details. These are global configurations for the applications.

Next, generate a new sms controller like so:

$ script/generate signupto_controller Demo my_method some_thing another

In the above command, “Demo” is the name of the class, and the others are actions within our controller. Once you have your newly created class, you can call the sms methods from your main controllers.

def index

send_sms(ExampleMessenger, :some_method, { :from => '02UK', :to => '4445265485' })

end

send_sms(KlassYouWantToCall, :method_on_class, {})

paramater 3 is an options hash, of which valid options are: :to => “recipeitns phone number” (string, -) :from => ‘from’ that will appear on recipient mobile (string, 12)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, controller = nil) ⇒ SmsController

You can initialize an SmsController with a series of parameters that can be used by methods in the class. You can also pass in a controller object, which will be available to the SmsController methods as base_controller.



42
43
44
45
46
47
# File 'lib/merb/sms_controller.rb', line 42

def initialize(params = {}, controller = nil)
  @params = params
  @base_controller = controller
  @session = (controller && controller.session) || {}
  super
end

Instance Attribute Details

#base_controllerObject (readonly)

Returns the value of attribute base_controller.



36
37
38
# File 'lib/merb/sms_controller.rb', line 36

def base_controller
  @base_controller
end

#mailObject

Returns the value of attribute mail.



35
36
37
# File 'lib/merb/sms_controller.rb', line 35

def mail
  @mail
end

#mailerObject

Returns the value of attribute mailer.



35
36
37
# File 'lib/merb/sms_controller.rb', line 35

def mailer
  @mailer
end

#paramsObject

Returns the value of attribute params.



35
36
37
# File 'lib/merb/sms_controller.rb', line 35

def params
  @params
end

#sessionObject (readonly)

Returns the value of attribute session.



36
37
38
# File 'lib/merb/sms_controller.rb', line 36

def session
  @session
end

Instance Method Details

#dispatch_and_deliver(method, sms_params) ⇒ Object

Builds the message and account objects from the global configs and merges them with the paramates passed in from the main application controller send_sms call.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/merb/sms_controller.rb', line 58

def dispatch_and_deliver(method, sms_params)
  @method = method
  # run the method in the subclass which in turn calls
  # the render_sms method defined above
  dispatch(method)
  # take ther result and merge it with the options passed 
  # in from the application controller subclass
  @message_options.merge!(sms_params)
  sms_message = ::Signupto::Message.new(@message_options[:to], @message_options[:body], @message_options[:from])
  global_config = Merb::Plugins.config[:merb_signupto]
   = ::Signupto::Account.new(global_config[:account_hash], global_config[:customer_number])
  if sms_message
    begin 
      self.class._sms_klass.new(, sms_message)
    rescue => e
      puts "[signupto] ERROR: #{e}"
      MERB_LOGGER.error(e)
    end
  end
end

#render_sms(options = @method) ⇒ Object



49
50
51
52
53
# File 'lib/merb/sms_controller.rb', line 49

def render_sms(options = @method)
  @_missing_templates = false # used to make sure that at least one template was found
  value = render({:format=>:text, :clean_context=>true, :text => @method, :layout => :none})
  @message_options = {:body => value}
end