Class: UnifonicIntegration::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/unifonic_integration/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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

#configObject

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 send_message(body, recipient)
  validate_recipient(recipient)
  endpoint = '/messages?async=false'
  set_headers
  set_message_params(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 send_scheduled_message(body, recipient, time_scheduled)
  validate_recipient(recipient)
  endpoint = '/messages/scheduledmessages?async=false'
  set_headers
  set_scheduled_message_params(body, recipient, time_scheduled)
  response = self.class.post(endpoint, headers: @headers, body: @params)
  puts response.body
  return response.body
end

#set_headersObject



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 set_message_params(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 set_scheduled_message_params(body, recipient, time_scheduled)
  set_message_params(body, recipient)
  @params.merge!(TimeScheduled: time_scheduled)
end

#validate_configurationObject



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