Sha256: c564c71bc438994c02e609b8012e691f6be5b470978b2a56fdb867648528118d

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

module Twsms2
  module Formatter
    def match_string(rule, string)
      match_data = rule.match(string)
      match_data.nil? ? nil : match_data[1]
    end

    def format_send_message_info(original_info)
      new_info = {
        access_success: false,
        message_id: nil,
        error: nil
      }

      code_text  = match_string(/<code>(?<code>\w+)<\/code>/, original_info)
      message_id_text = match_string(/<msgid>(?<message_id>\d+)<\/msgid>/, original_info)

      new_info[:access_success] = !code_text.nil? && !message_id_text.nil? && code_text == '00000'

      if new_info[:access_success]
        new_info[:message_id] = message_id_text
      else
        new_info[:error] = "TWSMS:CODE_NOT_FOUND"
        new_info[:error] = "TWSMS:#{code_text}" unless code_text.nil?
        new_info[:error].upcase!
      end

      new_info
    end

    def format_balance_info(original_info)
      new_info = {
        access_success: false,
        message_quota: 0,
        error: nil
      }

      code_text  = match_string(/<code>(?<code>\w+)<\/code>/, original_info)
      point_text = match_string(/<point>(?<point>\d+)<\/point>/, original_info)

      new_info[:access_success] = !code_text.nil? && !point_text.nil? && code_text == '00000'

      if new_info[:access_success]
        new_info[:message_quota] = point_text.to_i
      else
        new_info[:error] = "TWSMS:CODE_NOT_FOUND"
        new_info[:error] = "TWSMS:#{code_text}" unless code_text.nil?
        new_info[:error].upcase!
      end

      new_info
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twsms2-1.0.1 lib/twsms2/formatter.rb
twsms2-1.0.0 lib/twsms2/formatter.rb
twsms2-0.9.6 lib/twsms2/formatter.rb
twsms2-0.9.5 lib/twsms2/formatter.rb
twsms2-0.9.0 lib/twsms2/formatter.rb