class Telesms::Incoming

This class represents an incoming message.

Attributes

body[RW]

@return [String] The body of the message.

mail[R]

@return [Mail::Message] The mail message created from the params.

Public Class Methods

new(params) click to toggle source

This method creates a new incoming message and parses it.

@param [Hash] params

The params from the mail received.

@return [Incoming]

# File lib/telesms/incoming.rb, line 30
def initialize(params)
  @mail = Mail.new(params)
  parse
end
receive(params) click to toggle source

This method receives a mail param and parses it.

@param [Hash] params

The params from the mail received.

@return [Hash]

# File lib/telesms/incoming.rb, line 20
def self.receive(params)
  self.new(params).parse
end

Public Instance Methods

clean_body!() click to toggle source

This method cleans the body that was received.

@return [Boolean]

# File lib/telesms/incoming.rb, line 86
def clean_body!
  body        = @mail.parts.last.body.decoded if @mail.multipart?
  body        ||= @mail.body.to_s
  body        = body.gsub(/<[^>]*>/, '')
  body        = body.gsub("_____________________________________________________________\n\n", '')
  body        = body.gsub(/\n/, '').gsub(/\s+/, ' ')
  body        = body[0, body.index(original_message_regex) || body.length].to_s
  body        = body.split(/\n\s*\n/m, 2)[1] || body if body.match(/\n/)
  body        = body.to_s.gsub(/\n/, '').strip
  @body       = body[0,160]
  return true
end
from() click to toggle source

This method gets the number that is sending the message.

@return [String]

# File lib/telesms/incoming.rb, line 63
def from
  @mail.from.first.to_s.match(/(.*)\@/)[1] rescue nil
end
gateway_host() click to toggle source

This method gets the gateway host from the FROM field.

@return [String]

# File lib/telesms/incoming.rb, line 70
def gateway_host
  @mail.from.first.to_s.match(/\@(.*)/)[1] rescue nil
end
original_message_regex() click to toggle source

This method is the original message regex.

@return [Regexp]

# File lib/telesms/incoming.rb, line 102
def original_message_regex
  /(-----Original Message-----)/
end
parse() click to toggle source

This method parses the received mail param.

@return [Hash]

* +from+:     The number who is sending the message.
* +to+:       Who the message is for.
* +body+:     The clean body.
* +provider+: Name of the provider.
# File lib/telesms/incoming.rb, line 42
def parse
  clean_body!

  {
    from:     from,
    to:       to,
    body:     body,
    provider: provider
  }
end
provider() click to toggle source

This method gets the provider name from the FROM field.

@return [String]

# File lib/telesms/incoming.rb, line 77
def provider
  Base.gateways.select do |name,gateways|
    [gateways[:sms], gateways[:mms]].include? gateway_host
  end.keys.first
end
to() click to toggle source

This method gets the receiver of the message.

@return [String]

# File lib/telesms/incoming.rb, line 56
def to
  @mail.to.first
end