Class: UnifonicIntegration::Client
- Inherits:
-
Object
- Object
- UnifonicIntegration::Client
- Includes:
- HTTParty
- Defined in:
- lib/unifonic_integration/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #send_message(body, recipient) ⇒ Object
- #send_scheduled_message(body, recipient, time_scheduled) ⇒ Object
- #set_headers ⇒ Object
- #set_message_params(body, recipient) ⇒ Object
- #set_scheduled_message_params(body, recipient, time_scheduled) ⇒ Object
- #validate_configuration ⇒ Object
- #validate_recipient(recipient) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
12 13 14 15 |
# File 'lib/unifonic_integration/client.rb', line 12 def initialize @config = UnifonicIntegration.configuration validate_configuration end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/unifonic_integration/client.rb', line 10 def config @config end |
Instance Method Details
#send_message(body, recipient) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/unifonic_integration/client.rb', line 17 def (body, recipient) validate_recipient(recipient) endpoint = '/messages?async=false' set_headers (body, recipient) response = self.class.post(endpoint, headers: @headers, body: @params) puts response.body return response.body end |
#send_scheduled_message(body, recipient, time_scheduled) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/unifonic_integration/client.rb', line 33 def (body, recipient, time_scheduled) validate_recipient(recipient) endpoint = '/messages/scheduledmessages?async=false' set_headers (body, recipient, time_scheduled) response = self.class.post(endpoint, headers: @headers, body: @params) puts response.body return response.body end |
#set_headers ⇒ Object
43 44 45 46 47 |
# File 'lib/unifonic_integration/client.rb', line 43 def set_headers @headers = { Accept: 'application/json', } end |
#set_message_params(body, recipient) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/unifonic_integration/client.rb', line 54 def (body, recipient) @params = { AppSid: @config.app_sid, SenderID: @config.sender_id, Body: body, Recipient: recipient } end |
#set_scheduled_message_params(body, recipient, time_scheduled) ⇒ Object
49 50 51 52 |
# File 'lib/unifonic_integration/client.rb', line 49 def (body, recipient, time_scheduled) (body, recipient) @params.merge!(TimeScheduled: time_scheduled) end |
#validate_configuration ⇒ Object
62 63 64 65 66 |
# File 'lib/unifonic_integration/client.rb', line 62 def validate_configuration unless @config.app_sid && @config.sender_id raise ConfigurationError, "UnifonicIntegration gem not properly configured. Please set app_sid and sender_id." end end |
#validate_recipient(recipient) ⇒ Object
27 28 29 30 31 |
# File 'lib/unifonic_integration/client.rb', line 27 def validate_recipient(recipient) if recipient.to_s.start_with?('+', '00') raise ArgumentError, "Phone number should not start with '+' or '00'." end end |