Sha256: 4666b9b278798713e59c17c4f362b3631a45c1429e4459701ccd91c61324d706

Contents?: true

Size: 832 Bytes

Versions: 2

Compression:

Stored size: 832 Bytes

Contents

require 'thread'

module Webmoney

  class Messenger

    attr_reader :thread

    def initialize(owner, &logger)
      @webmoney = owner
      @queue = Queue.new
      @thread = Thread.new(@queue) do |q|
        loop do
          msg = q.pop
          unless msg.nil?
            begin
              result = @webmoney.request(:send_message, msg)
              logger.call(msg, result)
              # Requeue message on fail
              @queue.push(msg) unless result.kind_of?(Hash)
            rescue ResultError, ResponseError => e
              logger.call(msg, e)
              # Requeue message
              @queue.push(msg)
            end
          end
          sleep(0.2)
        end
      end
    end
    
    def push(msg)
      @queue.push(msg)
    end

    # TODO callback on success send message
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webmoney-0.0.5 lib/messenger.rb
webmoney-0.0.4.8 lib/messenger.rb