Module: MetaSms

Defined in:
lib/meta_sms.rb,
lib/meta_sms/utility.rb,
lib/meta_sms/version.rb,
lib/meta_sms/sms_logging.rb,
lib/meta_sms/configuration.rb,
lib/meta_sms/meta_sms_error.rb,
lib/meta_sms/providers/smsbox.rb,
lib/meta_sms/sms_provider_selector.rb,
lib/meta_sms/providers/i_sms_provider.rb,
lib/meta_sms/providers/provider_utility.rb,
lib/generators/meta_sms/install_generator.rb,
lib/generators/meta_sms/migrations_for_logger_generator.rb

Overview

Note:

Here error from sms_logging is not handled.

This module is use to send sms through sms providers as sms box. It contains a class method #send_sms which takes a hash as argument. #send_sms will send sms, log sms if logging is enabled ((in options or config.logging) and migrations_for_logger is generated and migrated.) Some inportant classes of module MetaSms are:

> Config

Config is for containg the config variables required in the process of sending sms and logging results in db.

> SmsProviderSelector

MetaSms#send_sms uses SmsProviderSelector class to select the provider from the given lists of provider in the gem on the basis of the configuration variable, sms_provider_name.

> ISmsProvider

It acts as an interface for all the providers. So, in order to use sms provider class, we shall create an object of sms provider, which in turn use the constructor of ISmsProvider. This constructor takes options as an argument. options is a hash in which :message_text and :mobile_number is the required keys. Other optional keys can be :logging, :metadata. Here, :message_text will be the message sen to the user with mobile number as :mobile_number. :logging represents whether logging of this message is required in the database or not. There is a config variable logging, which also determines the same. So, logging will be true, if any of these value is true. :metadata can be any json data that client wants to save in the db along with the log.

> Smsbox

Smsbox is the sms provider class. It will have a method send_sms, which overrides the method of its parent class, ISmsProvider. Smsbox.send_sms will send message if required options and required configuration variable are present else, it will raise error. MetaSms#send_sms will rescue the error, write the log and then throw the rescued error.

> SmsLogging

This is an active record responsible to log data into database. It will save the sms log into a sms_loggings table. SmsLogging#log_sms is the method which does saving job. This method will raise StandardError if config.logging or options is true and you have not executed, ‘rails g meta_sms:migrations_for_logger’.

Author:

  • Shobhit Dixit

Defined Under Namespace

Classes: Config, ISmsProvider, InstallGenerator, MetaSmsError, MigrationsForLoggerGenerator, ProviderUtility, SmsLogging, SmsProviderSelector, Smsbox, Utility

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



3
4
5
# File 'lib/meta_sms/configuration.rb', line 3

def config
  @config
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



6
7
8
9
# File 'lib/meta_sms/configuration.rb', line 6

def self.configure
  self.config ||= Config.new
  yield(config)
end

.send_sms(options) ⇒ Type

Note:

here message_text and mobile_number are required params and others are optional

This class method is use to send sms through this gem.

Examples:

{:message_text* => “Some text message”, :mobile_number* => 7894561237, :logging => boolean, :metadata =>json }

Parameters:

  • options (Hash)

Returns:

  • (Type)

    description of returned object

Author:

  • Shobhit Dixit



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/meta_sms.rb', line 50

def self.send_sms(options)
  provider_class = SmsProviderSelector.new.provider_class
  pc = provider_class.new(options)
  error = nil
  begin
    result = pc.send_sms
  rescue MetaSmsError => meta_sms_error
    error = meta_sms_error
  rescue SecurityError => security_error
    error = security_error
  end
  # This will raise an standard error if no table is present and logging is true.
  SmsLogging.log_sms(result, options, error) if Utility.logging?(options[:logging])
  raise error if error.present?
  result
end