Class: Chikka::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chikka.rb

Constant Summary collapse

SMSAPI_PATH =
'/smsapi/request'
DEFAULT_PARAMS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



25
26
27
# File 'lib/chikka.rb', line 25

def client_id
  @client_id
end

#httpObject

Returns the value of attribute http.



25
26
27
# File 'lib/chikka.rb', line 25

def http
  @http
end

#secret_keyObject

Returns the value of attribute secret_key.



25
26
27
# File 'lib/chikka.rb', line 25

def secret_key
  @secret_key
end

#shortcodeObject

Returns the value of attribute shortcode.



25
26
27
# File 'lib/chikka.rb', line 25

def shortcode
  @shortcode
end

Instance Method Details

#send_message(params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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