Class: Hermes::TwilioProvider

Inherits:
Provider
  • Object
show all
Defined in:
lib/providers/twilio/twilio_provider.rb

Instance Attribute Summary

Attributes inherited from Provider

#credentials, #defaults, #deliverer, #weight

Instance Method Summary collapse

Methods inherited from Provider

#initialize, #message_failure, #message_success, #provider_name, required_credentials

Methods included from Extractors

#complex_extract, #extract_from, #extract_html, #extract_text, #extract_to

Constructor Details

This class inherits a constructor from Hermes::Provider

Instance Method Details

#clientObject



35
36
37
# File 'lib/providers/twilio/twilio_provider.rb', line 35

def client
  Twilio::REST::Client.new(self.credentials[:account_sid], self.credentials[:auth_token])
end

#payload(rails_message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/providers/twilio/twilio_provider.rb', line 23

def payload(rails_message)
  payload = {
    to: extract_to(rails_message).full_number,
    from: extract_from(rails_message),
    body: extract_text(rails_message),
  }

  payload[:status_callback] = rails_message.twilio_status_callback if rails_message.twilio_status_callback

  return payload
end

#send_message(rails_message) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/providers/twilio/twilio_provider.rb', line 5

def send_message(rails_message)
  payload = payload(rails_message)

  if self.deliverer.should_deliver?
    result = self.client..messages.create(payload)

    # set the sid onto the rails message as the message id, used for tracking
    rails_message[:message_id] = result.sid
  else
    # rails message still needs a fake sid as if it succeeded
    rails_message[:message_id] = SecureRandom.uuid
  end

  self.message_success(rails_message)
rescue Exception => e
  self.message_failure(rails_message, e)
end