Sha256: aaaf583b5002ab3fca567e1d3b7ef7e272ba4eb009857cb4b5986032da9fe5f0

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module RsrGroup
  class Base

    FTP_HOST = 'ftp.rsrgroup.com'

    protected

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

    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

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

      Net::FTP.open(FTP_HOST, options[:username], options[:password]) do |ftp|
        yield ftp
      end
    rescue Net::FTPPermError
      raise RsrGroup::NotAuthenticated
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rsr_group-0.1.3 lib/rsr_group/base.rb
rsr_group-0.1.2 lib/rsr_group/base.rb
rsr_group-0.1.1 lib/rsr_group/base.rb
rsr_group-0.1.0 lib/rsr_group/base.rb