Sha256: 80bea4aa94e1ab47824d8e19316172a818fc2e95e58beb32c84200a72f5421ee

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module SmsTraffic
  class Client
    class DeliverReply < Client::Reply
      OK_REPLY = 'OK'.freeze unless const_defined?(:OK_REPLY)

      def ok?
        fetch_value(hash, :result) == OK_REPLY
      rescue NoMethodError
        false
      end

      def error?
        !ok?
      end

      def result
        fetch_value(hash, :result)
      end

      def code
        fetch_value(hash, :code)
      end

      def description
        fetch_value(hash, :description)
      end

      def message_infos
        fetch_value(hash, :message_infos)
      end

      def sms_id(phone = nil)
        message_info = fetch_value(message_infos, :message_info)
        return fetch_value(message_info, :sms_id) if message_info.is_a?(Hash) || message_info.nil?

        if phone
          fetch_value(find_message_info(phone), :sms_id)
        elsif message_info.is_a?(Array)
          fetch_value(message_info.first, :sms_id)
        end
      end

      def sms_ids
        message_info = fetch_value(message_infos, :message_info)

        return [fetch_value(message_info, :sms_id)] if message_info&.size == 1

        message_info.each_with_object({}) do |info, hash|
          hash[fetch_value(info, :phone)] = fetch_value(info, :sms_id)
        end
      end

      def error_description
        "#{result}: code: #{code}, description: #{description}" if error?
      end

      private

      def find_message_info(phone)
        fetch_value(message_infos, :message_info).find { |info| fetch_value(info, :phone) == phone }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sms_traffic_sdk-0.0.6 lib/sms_traffic/client/deliver_reply.rb
sms_traffic_sdk-0.0.5 lib/sms_traffic/client/deliver_reply.rb