Class: VoicecomSms::Provider

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

Constant Summary collapse

STATUS =
{
  undefined: 0,
  success: 1,
  failure: 2
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProvider

Returns a new instance of Provider.



15
16
17
18
19
# File 'lib/voicecom_sms/provider.rb', line 15

def initialize
  check_config
  @request = VoicecomSms::Request.new
  @response = nil
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



13
14
15
# File 'lib/voicecom_sms/provider.rb', line 13

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



13
14
15
# File 'lib/voicecom_sms/provider.rb', line 13

def response
  @response
end

Instance Method Details

#normalize_number(number) ⇒ Object



56
57
58
59
# File 'lib/voicecom_sms/provider.rb', line 56

def normalize_number(number)
  number.gsub!(/\s+/, '')
  number
end

#send_sms(number, text, &block) ⇒ Object

Send the text message to the number

Parameters:

  • number

    String Only digits, country code included. example: for Bulgaria (+359) and mobile number 899947121 you should use ‘359899947121’

  • text

    String No idea what characters is supported by voicecom - have to experiment. UTF-8 with latin works for sure.

Returns:

  • VoicecomSms::Message - you can take the status field from there, and have the id of the transaction.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/voicecom_sms/provider.rb', line 33

def send_sms(number, text, &block)
  message = VoicecomSms::Message.create({text: text, number: normalize_number(number), status: STATUS[:undefined]})

  message_id = if block_given?
    yield(message.id)
  else
    message.id
  end

  make_request(message, number, text, message_id)

  if @request.error or !@request.sent?
    message.update_attributes(response: @request.error, status: STATUS[:failure])
  else
    @response = VoicecomSms::Response.new(@request.raw_response)
    status = @response.success? ? STATUS[:success] : STATUS[:failure]

    message.update_attributes(response: @response.inspect, status: status, response_received_at: Time.current)
  end

  message
end