Sha256: 87132568bccf2c12acd5115c588c8a19fd0bcb2b614521186443a0bcfabef9be

Contents?: true

Size: 1.18 KB

Versions: 24

Compression:

Stored size: 1.18 KB

Contents

module RsrGroup
  class Base

    def self.connect(options = {})
      requires!(options, :username, :password)

      Net::FTP.open(RsrGroup.config.ftp_host, options[:username], options[:password]) do |ftp|
        ftp.debug_mode = RsrGroup.config.debug_mode
        ftp.passive = true
        yield ftp
      end
    end

    protected

    def self.requires!(hash, *params)
      params.each do |param|
        if param.is_a?(Array)
          raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)

          valid_options = param[1..-1]
          raise ArgumentError.new("Parameter: #{param.first} must be one of: #{valid_options.join(', ')}") unless valid_options.include?(hash[param.first])
        else
          raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
        end
      end
    end

    # Wrapper to `self.requires!` that can be used as an instance method.
    def requires!(*args)
      self.class.requires!(*args)
    end

    # Instance methods become class methods through inheritance
    def connect(options)
      self.class.connect(options) do |ftp|
        yield ftp
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rsr_group-3.0.1 lib/rsr_group/base.rb
rsr_group-2.4.0 lib/rsr_group/base.rb
rsr_group-2.3.0 lib/rsr_group/base.rb
rsr_group-2.2.1 lib/rsr_group/base.rb
rsr_group-2.2.0 lib/rsr_group/base.rb
rsr_group-1.8.0 lib/rsr_group/base.rb
rsr_group-2.1.6 lib/rsr_group/base.rb
rsr_group-2.1.5 lib/rsr_group/base.rb
rsr_group-1.7.9 lib/rsr_group/base.rb
rsr_group-2.1.4 lib/rsr_group/base.rb
rsr_group-2.1.3 lib/rsr_group/base.rb
rsr_group-2.1.2 lib/rsr_group/base.rb
rsr_group-2.1.1 lib/rsr_group/base.rb
rsr_group-2.1.0 lib/rsr_group/base.rb
rsr_group-2.0.4 lib/rsr_group/base.rb
rsr_group-2.0.3 lib/rsr_group/base.rb
rsr_group-2.0.2 lib/rsr_group/base.rb
rsr_group-2.0.1 lib/rsr_group/base.rb
rsr_group-1.7.8 lib/rsr_group/base.rb
rsr_group-1.7.7 lib/rsr_group/base.rb