class Chikka::Client

Constants

DEFAULT_PARAMS
SMSAPI_PATH

Attributes

client_id[RW]
http[RW]
secret_key[RW]
shortcode[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/chikka.rb, line 28
def initialize(options = {})
  @client_id= options.fetch(:client_id) { ENV.fetch('CHIKKA_CLIENT_ID') }
  @secret_key = options.fetch(:secret_key) { ENV.fetch('CHIKKA_SECRET_KEY') }
  @shortcode = options.fetch(:shortcode) { ENV.fetch('CHIKKA_SHORTCODE') }
  @mask = options.fetch(:mask) { ENV.fetch('CHIKKA_MASK') } if ENV.has_key?('CHIKKA_MASK') || options.has_key?(:mask)

  @host = options.fetch(:host) { 'post.chikka.com' }
  @http = Net::HTTP.new(@host, Net::HTTP.https_default_port)
  @http.use_ssl = true

  DEFAULT_PARAMS[:client_id] = @client_id
  DEFAULT_PARAMS[:secret_key] = @secret_key
  DEFAULT_PARAMS[:shortcode] = @shortcode
  DEFAULT_PARAMS[:mask] = @mask
end

Public Instance Methods

send_message(params = {}) click to toggle source
# File lib/chikka.rb, line 44
def send_message(params = {})
  params[:message_id] = params.fetch(:message_id) { generate_message_id }

  message_type = "SEND"
  if params[:request_id]
    message_type = "REPLY"
    params[:request_cost] = params.fetch(:resquest_cost) { "FREE" }
  end

  post_params = DEFAULT_PARAMS.merge({
    message_type: message_type
  }.merge(params))

  body = URI.encode_www_form(post_params)
  parse(@http.post(SMSAPI_PATH, body, {'Content-Type' => 'application/x-www-form-urlencoded'}), params[:message_id])
end

Private Instance Methods

generate_message_id() click to toggle source
# File lib/chikka.rb, line 62
def generate_message_id
  SecureRandom.hex
end
parse(http_response, message_id) click to toggle source
# File lib/chikka.rb, line 66
def parse(http_response, message_id)
  response_obj = Response.new(http_response, message_id)
  case response_obj.status
  when 200
    response_obj
  when 401
    raise AuthenticationError.new(message= response_obj.description)
  when 400
    raise BadRequestError.new(message = response_obj.description)
  else
    raise Error.new(message=response_obj.description)
  end
end