Class: OrangePushSMS::Message

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

Constant Summary collapse

BASE_URL =
'https://api.orangesmspro.sn:8443/api/xml'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Returns a new instance of Message.



9
10
11
12
13
14
# File 'lib/orange_push_sms/message.rb', line 9

def initialize(options = {})
  @recipients = options.fetch(:recipients, [])
  @content    = options.fetch(:content, '')
  @signature  = options.fetch(:signature, 'Aphia')
  @subject    = options.fetch(:subject, '')
end

Instance Method Details

#configObject



16
17
18
# File 'lib/orange_push_sms/message.rb', line 16

def config
  OrangePushSMS.configuration
end

#digestObject



24
25
26
27
28
29
# File 'lib/orange_push_sms/message.rb', line 24

def digest
  d = OpenSSL::Digest.new('sha1')
  OpenSSL::HMAC.hexdigest d,
                          config.secret,
                          "#{config.token}#{to_xml}#{timestamp}"
end

#sendObject



47
48
49
50
51
52
53
54
# File 'lib/orange_push_sms/message.rb', line 47

def send
  uri = "#{BASE_URL}?token=#{config.token}&key=#{digest}&timestamp=#{timestamp}"
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  xml = Nokogiri::XML HTTP.basic_auth(user: config.user, pass: config.token)
                          .post(uri, body: to_xml, ssl_context: ctx)
  puts xml
end

#timestampObject



20
21
22
# File 'lib/orange_push_sms/message.rb', line 20

def timestamp
  @timestamp ||= Time.now.to_i
end

#to_xmlObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/orange_push_sms/message.rb', line 31

def to_xml
  @builder ||= Nokogiri::XML::Builder.new do |xml|
    xml.session do
      xml.type 1
      xml.content @content
      xml.recipients do
        @recipients.each do |recipient|
          xml.recipient recipient
        end
      end
      xml.signature @signature
      xml.subject @subject
    end
  end.to_xml
end