Class: PayCallSms::IncomingMessageParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_call_sms/incoming_message_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ IncomingMessageParser

Create new sms sender with given gateway



6
7
8
9
# File 'lib/pay_call_sms/incoming_message_parser.rb', line 6

def initialize(options={})
  @options = options
  @logger = Logging.logger[self.class]
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/pay_call_sms/incoming_message_parser.rb', line 3

def logger
  @logger
end

Instance Method Details

#from_http_push_params(params) ⇒ Object

params will look something like the following: msgId - uniq id of the message sender - the phone that have sent the message recipient - the virtual phone number that received the message (at gateway operator) segments - number of segments content - text of the message



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pay_call_sms/incoming_message_parser.rb', line 17

def from_http_push_params(params)
  %w(msgId  sender  recipient  content).each do |p|
    raise ArgumentError.new("Missing http parameter #{p}. Parameters were: #{params.inspect}") if params[p].blank?
  end

  logger.debug "Parsing http push reply xml: \n#{params['IncomingXML']}"
  parse_reply_values_hash(
    phone: params['sender'],
    reply_to_phone: params['recipient'],
    text: params['content'],
    message_id: params['msgId'],
    received_at: params['receivedTime'],
  )
end

#parse_reply_values_hash(values) ⇒ Object

This method receives sms reply values Hash and tries to type cast it’s values Method returns object with the following attributes:

  • phone - the phone that sent the sms (from which sms reply was received)

  • text - contents of the message that were received

  • reply_to_phone - the phone to sms which reply was sent (gateway phone number)

  • received_at - when the sms was received (as reported by gateway server)

  • message_id - uniq message id generated from phone,reply_to_phone and received_at timestamp



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pay_call_sms/incoming_message_parser.rb', line 41

def parse_reply_values_hash(values)
  logger.debug "Parsing reply_values_hash: #{values.inspect}"
  [:message_id, :phone, :text, :reply_to_phone].each do |key|
    raise ArgumentError.new("Missing sms reply values key #{key}. Values were: #{values.inspect}") if values[key].blank?
  end

  values[:phone] = PhoneNumberUtils.ensure_country_code(values[:phone])
  values[:reply_to_phone] = PhoneNumberUtils.ensure_country_code(values[:reply_to_phone])

  if values[:received_at].is_a?(String)
    begin
      Time.use_zone(@options[:time_zone] || Time.zone || 'Jerusalem') do
        values[:received_at] = DateTime.strptime(values[:received_at], '%Y-%m-%d %H:%M:%S')
        values[:received_at] = Time.zone.parse(values[:received_at].strftime('%Y-%m-%d %H:%M:%S')) #convert to ActiveSupport::TimeWithZone
      end
    rescue Exception => e
      raise ArgumentError.new("received_at could not be converted to date. received_at was: #{values[:received_at]}")
    end
  else
    values[:received_at] = Time.now
  end
  OpenStruct.new(values)
end