Sha256: d9ee80d3b8f914fc03b9590a3964009b18fa2b4b054b4cee3aac6d1abd299471

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

require 'socket'

module Killbill::PaypalExpress
  class Utils
    def self.ip
      first_public_ipv4 ? first_public_ipv4.ip_address : first_private_ipv4.ip_address
    end

    def self.first_private_ipv4
      @@first_private_ipv4 ||= Socket.ip_address_list.detect{ |intf| intf.ipv4_private? }
    end

    def self.first_public_ipv4
      @@first_public_ipv4 ||= Socket.ip_address_list.detect{ |intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private? }
    end
  end

  # Closest from a streaming API as we can get with ActiveRecord
  class StreamyResultSet
    include Enumerable

    def initialize(limit, batch_size = 100, &delegate)
      @limit = limit
      @batch = [batch_size, limit].min
      @delegate = delegate
    end

    def each(&block)
      (0..(@limit - @batch)).step(@batch) do |i|
        result = @delegate.call(i, @batch)
        block.call(result)
        # Optimization: bail out if no more results
        break if result.nil? || result.empty?
      end if @batch > 0
      # Make sure to return DB connections to the Pool
      ActiveRecord::Base.connection.close
    end

    def to_a
      super.to_a.flatten
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
killbill-paypal-express-1.7.0 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.7 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.6 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.5 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.4 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.3 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.2 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.1 lib/paypal_express/paypal_express_utils.rb
killbill-paypal-express-1.6.0 lib/paypal_express/paypal_express_utils.rb