Class: Smsified::OneAPI

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Helpers
Defined in:
lib/smsified/oneapi.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OneAPI

Intantiate a new class to work with OneAPI

Examples:

one_api = OneAPI.new :username => 'user', :password => '123'

Parameters:

  • params (required, Hash)

    to create the user

Raises:

  • (ArgumentError)

    if :username is not passed as an option

  • (ArgumentError)

    if :password is not passed as an option



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smsified/oneapi.rb', line 25

def initialize(options)
  raise ArgumentError, 'an options Hash is required' if !options.instance_of?(Hash)
  raise ArgumentError, ':username required' if options[:username].nil?
  raise ArgumentError, ':password required' if options[:password].nil?
  
  self.class.debug_output $stdout if options[:debug]
  self.class.base_uri options[:base_uri] || SMSIFIED_ONEAPI_PUBLIC_URI
  @auth = { :username => options[:username], :password => options[:password] }
  
  @destination_address = options[:destination_address]
  @sender_address      = options[:sender_address]
  
  @subscriptions = Subscriptions.new(options)
  @reporting     = Reporting.new(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Dispatches method calls to other objects for subscriptions and reporting



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/smsified/oneapi.rb', line 75

def method_missing(method, *args)
  if method.to_s.match /subscription/
    if args.size == 2
      @subscriptions.send method, args[0], args[1]
    else
      @subscriptions.send method, args[0]
    end
  else
    if method == :delivery_status || method == :retrieve_sms || method == :search_sms
      @reporting.send method, args[0]
    else
      raise RuntimeError, 'Unknown method'
    end
  end
end

Instance Method Details

#send_sms(options) ⇒ Object

Send an SMS to one or more addresses

Examples:

one_api.send_sms :address => '14155551212', :message => 'Hi there!', :sender_address => '13035551212'
one_api.send_sms :address => ['14155551212', '13035551212'], :message => 'Hi there!', :sender_address => '13035551212'

Parameters:

  • params (required, Hash)

    to send an sms

Returns:

  • (Object)

    A Response Object with http and data instance methods

Raises:

  • (ArgumentError)

    if :sender_address is not passed as an option when not passed on object creation

  • (ArgumentError)

    if :address is not provided as an option

  • (ArgumentError)

    if :message is not provided as an option



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smsified/oneapi.rb', line 56

def send_sms(options)
  raise ArgumentError, 'an options Hash is required' if !options.instance_of?(Hash)
  raise ArgumentError, ':sender_address is required' if options[:sender_address].nil? && @sender_address.nil?
  raise ArgumentError, ':address is required' if options[:address].nil?
  raise ArgumentError, ':message is required' if options[:message].nil?
  
  options[:sender_address] = options[:sender_address] || @sender_address
  query_options = options.clone
  query_options.delete(:sender_address)
  query_options = camelcase_keys(query_options)

  Response.new self.class.post("/smsmessaging/outbound/#{options[:sender_address]}/requests",
                               :body       => build_query_string(query_options),
                               :basic_auth => @auth,
                               :headers    => SMSIFIED_HTTP_HEADERS)
end