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
# File 'lib/unifonic_integration/client.rb', line 12

def initialize
  @config = Configuration.new
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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/unifonic_integration/client.rb', line 16

def send_message(body, recipient)

  unless validate_recipient(recipient)
    puts "Invalid recipient phone number format. Please provide a valid international format."
    return
  end

  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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/unifonic_integration/client.rb', line 36

def send_scheduled_message(body, recipient, time_scheduled)
  unless validate_recipient(recipient)
    puts "Invalid recipient phone number format. Please provide a valid international format."
    return
  end

  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



50
51
52
53
54
# File 'lib/unifonic_integration/client.rb', line 50

def set_headers
  @headers = {
    Accept: 'application/json',
  }
end

#set_message_params(body, recipient) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/unifonic_integration/client.rb', line 61

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



56
57
58
59
# File 'lib/unifonic_integration/client.rb', line 56

def set_scheduled_message_params(body, recipient, time_scheduled)
  set_message_params(body, recipient)
  @params.merge!(TimeScheduled: time_scheduled)
end

#validate_recipient(recipient) ⇒ Object



31
32
33
34
# File 'lib/unifonic_integration/client.rb', line 31

def validate_recipient(recipient)
  cleaned_number = recipient.to_s.gsub(/^(\+|00)/, '')
  cleaned_number =~ /\A\d+\z/
end