Sha256: 53a9b33a5194ebb41519221a19c5247b25c2c49d076933a345b14a46c3f80901

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'smstraderb/constants'

class SMSTradeRB
  class InvalidResponse < Exception; end
  class UnknownResponse < Exception; end

  class Response
    def initialize(response)
      check_response_string(response)
      @code, @message_id, @cost, @count = response.split("\n")
      check_response_code
    end

    def code
      @code.to_i
    end

    def message_id
      blank?(@message_id) ? nil : @message_id
    end

    def cost
      blank?(@cost) ? nil : @cost.to_f
    end

    def count
      blank?(@count) ? nil : @count.to_i
    end

    def ok?
      code == 100
    end

    def error?
      !ok?
    end

    def response_message
      SMSTradeRB::RESPONSE_CODES[code]
    end

    private
      def check_response_string(value)
        if value.nil?
          raise SMSTradeRB::InvalidResponse, 'Response string is nil.'
        end
        if value.empty?
          raise SMSTradeRB::InvalidResponse, 'Response string is empty.'
        end
        if value !~ %r{^\d+}
          raise SMSTradeRB::InvalidResponse, "Invalid response string: #{value}."
        end
      end

      def check_response_code
        unless SMSTradeRB::RESPONSE_CODES.key?(code)
          raise SMSTradeRB::UnknownResponse
        end
      end

      def blank?(value)
        value.nil? || value.empty? ? true : false
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smstraderb-0.1.0 lib/smstraderb/response.rb
smstraderb-0.0.1 lib/smstraderb/response.rb