Sha256: 8954c8a2504d7881e7c7020dd55dc9f8f5b7f4675e825d369cf515e68dc86748

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

module SmsTraffic
  class Client
    class StatusReply < Reply
      def sms_array
        fetch_value(hash, :sms).is_a?(Array) ? fetch_value(hash, :sms) : [hash]
      end

      def status(sms_id = nil)
        return fetch_value(sms_array.first, :status) if sms_array.size == 1

        raise ArgumentError, 'sms_id is required' if sms_id.nil?

        statuses[sms_id.to_s]
      end

      # @return [Hash] statuses hash (key: sms_id, value: status)
      # @example
      #
      #  {
      #    '1234567890': 'DELIVERED',
      #    '0987654321': 'FAILED'
      #  }
      def statuses
        sms_array.each_with_object({}) do |sms, hash|
          hash[fetch_value(sms, :sms_id)] = fetch_value(sms, :status)
        end.compact
      end

      def error(sms_id = nil)
        return fetch_value(sms_array.first, :error) if sms_array.size == 1

        raise ArgumentError, 'sms_id is required' if sms_id.nil?

        errors[sms_id.to_s]
      end

      def errors
        sms_array.each_with_object({}) do |sms, hash|
          hash[fetch_value(sms, :sms_id)] = fetch_value(sms, :error)&.strip
        end.compact
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sms_traffic_sdk-0.0.7 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.6 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.5 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.4 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.3 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.2 lib/sms_traffic/client/status_reply.rb
sms_traffic_sdk-0.0.1 lib/sms_traffic/client/status_reply.rb