Sha256: f62fbad743a9a5bb088ff18df87d54c82a1b7117213931cdcfce7d0456d4da4b

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

module Zanders
  class Base

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

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

        yield ftp
      end
    rescue Net::FTPPermError
      raise Zanders::NotAuthenticated
    end

    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

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

    def get_file(filename)
      connect(@options) do |ftp|
        begin
          tempfile = Tempfile.new

          ftp.chdir(Zanders.config.ftp_directory)
          ftp.getbinaryfile(filename, tempfile.path)

          tempfile
        ensure
          ftp.close
        end
      end
    end

    def content_for(xml_doc, field)
      node = xml_doc.css(field).first

      if node.nil?
        nil
      else
        if node.css("DATA").present?
          node.css("DATA").text.strip
        else
          node.content.strip
        end
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
zanders-5.0.6 lib/zanders/base.rb
zanders-5.0.5 lib/zanders/base.rb
zanders-5.0.4 lib/zanders/base.rb
zanders-5.0.3 lib/zanders/base.rb
zanders-5.0.2 lib/zanders/base.rb
zanders-5.0.1 lib/zanders/base.rb
zanders-5.0.0 lib/zanders/base.rb
zanders-4.0.0 lib/zanders/base.rb
zanders-3.0.1 lib/zanders/base.rb
zanders-3.0.0 lib/zanders/base.rb