Class: Txtlocal::Message

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

Constant Summary collapse

API_ENDPOINT =
URI.parse("https://www.txtlocal.com/sendsmspost.php")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, recipients = nil, options = nil) ⇒ Message

Returns a new instance of Message.



14
15
16
17
18
# File 'lib/txtlocal/message.rb', line 14

def initialize(message=nil, recipients=nil, options=nil)
  self.body = message if message
  self.recipients = recipients if recipients
  self.options = options if options
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/txtlocal/message.rb', line 8

def body
  @body
end

#fromObject

Returns the value of attribute from.



10
11
12
# File 'lib/txtlocal/message.rb', line 10

def from
  @from
end

#recipientsObject

Returns the value of attribute recipients.



9
10
11
# File 'lib/txtlocal/message.rb', line 9

def recipients
  @recipients
end

#responseObject

Returns the value of attribute response.



12
13
14
# File 'lib/txtlocal/message.rb', line 12

def response
  @response
end

Instance Method Details

#add_recipient(recipient) ⇒ Object



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

def add_recipient(recipient)
  recipient = recipient.gsub(/\s/, '')
  recipient = case recipient
    when /^447\d{9}$/
      recipient
    when /^(?:\+447|07)(\d{9})$/
      "447#{$1}"
    else
      return
  end
  recipients << recipient
end

#options=(options) ⇒ Object



58
59
60
# File 'lib/txtlocal/message.rb', line 58

def options=(options)
  self.from = options[:from] if options.has_key?(:from)
end

#send!Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/txtlocal/message.rb', line 74

def send!
  http = Net::HTTP.new(API_ENDPOINT.host, API_ENDPOINT.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Post.new(API_ENDPOINT.path)
  req.set_form_data(:json => 1,
                    :test => Txtlocal.config.testing? ? 1 : 0,
                    :from => from,
                    :message => body,
                    :selectednums => recipients.join(","),
                    :uname => Txtlocal.config.username,
                    :pword => Txtlocal.config.password)
  result = http.start { |http| http.request(req) }
  self.response = result
end