class EventMachine::Smsified::Base

Attributes

auth[R]
base_uri[R]
destination_address[R]
sender_address[R]

Public Class Methods

new(options) click to toggle source

Intantiate a new class to work with OneAPI

@param [required, Hash] params to create the user @option params [required, String] :username username to authenticate with @option params [required, String] :password to authenticate with @option params [optional, String] :base_uri of an alternative location of SMSified @option params [optional, String] :destination_address to use with subscriptions @option params [optional, String] :sender_address to use with subscriptions @option params [optional, Boolean] :debug to turn on the HTTparty debugging to stdout @raise [ArgumentError] if :username is not passed as an option @raise [ArgumentError] if :password is not passed as an option

# File lib/em-smsified/base.rb, line 21
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?
  
  @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]
end

Public Instance Methods

delete(url, headers) { |response| ... } click to toggle source

HTTP DELETE’s a request @param [required, String] :url to GET from

# File lib/em-smsified/base.rb, line 55
def delete(url, headers)
  conn = create_connection_object(url)

  http = conn.delete(:head => add_authorization_to_header(headers, @auth))

  action = proc do
    response = Response.new(http.response.parsed, http)#.response.raw)
    yield response if block_given?
  end

  http.callback &action
  http.errback &action 
end
get(url, headers) { |response| ... } click to toggle source

HTTP GET’s a request @param [required, String] :url to GET from

# File lib/em-smsified/base.rb, line 37
def get(url, headers)
  conn = create_connection_object(url)

  http = conn.get(:head => add_authorization_to_header(headers, @auth))

  action = proc do
    response = Response.new(http.response.parsed, http)#.response.raw)
    yield response if block_given?
  end

  http.callback &action
  http.errback &action 
end
post(url, body, headers) { |response| ... } click to toggle source

HTTP POST’s a request @param [required, String] :url to GET from

# File lib/em-smsified/base.rb, line 73
def post(url, body, headers)
  conn = create_connection_object(url)

  http = conn.post(:body => body,
                   :head => add_authorization_to_header(headers, @auth))

  action = proc do
    response = Response.new(http.response.parsed, http)#.response.raw)
    yield response if block_given?
  end

  http.callback &action
  http.errback &action 
end

Private Instance Methods

add_authorization_to_header(headers, auth) click to toggle source
# File lib/em-smsified/base.rb, line 95
def add_authorization_to_header(headers, auth)
  return headers.merge({'Authorization' => [auth[:username], auth[:password]]})
end
create_connection_object(url) click to toggle source
# File lib/em-smsified/base.rb, line 89
def create_connection_object(url)
  conn = EM::HttpRequest.new(SMSIFIED_ONEAPI_PUBLIC_URI + url)
  conn.use JSONify
  return conn
end