Sha256: d035bb6d4b86d4aa754be9abeb53b9da2b6767097e3eb6df76cdcc71ea9fa5d2

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

module Flexirest
  class ConnectionManager
    def self.reset!
      Thread.current[:_connections]={}
    end

    def self.get_connection(base_url)
      raise Exception.new("Nil base URL passed to ConnectionManager.get_connection") if base_url.nil?
      Thread.current[:_connections] ||= {}
      Thread.current[:_connections][base_url] ||= Connection.new(base_url)
      Thread.current[:_connections][base_url]
    end

    def self.find_connection_for_url(url)
      Thread.current[:_connections] ||= {}
      found = Thread.current[:_connections].keys.detect {|key| url[0,key.length] == key}
      Thread.current[:_connections][found] if found
    end

    def self.in_parallel(base_url)
      begin
        require 'typhoeus'
        require 'typhoeus/adapters/faraday' unless Gem.loaded_specs["faraday-typhoeus"].present?
      rescue LoadError
        raise MissingOptionalLibraryError.new("To call '::Flexirest::ConnectionManager.in_parallel' you must include the gem 'Typhoeus' in your Gemfile.")
      end
      session = ConnectionManager.get_connection(base_url).session
      session.in_parallel do
        yield if block_given?
      end
    end

  end

  class MissingOptionalLibraryError < StandardError ; end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
flexirest-1.12.4 lib/flexirest/connection_manager.rb
flexirest-1.12.3 lib/flexirest/connection_manager.rb
flexirest-1.12.2 lib/flexirest/connection_manager.rb
flexirest-1.12.1 lib/flexirest/connection_manager.rb
flexirest-1.12.0 lib/flexirest/connection_manager.rb
flexirest-1.11.3 lib/flexirest/connection_manager.rb
flexirest-1.11.2 lib/flexirest/connection_manager.rb
flexirest-1.11.1 lib/flexirest/connection_manager.rb
flexirest-1.11.0 lib/flexirest/connection_manager.rb