Sha256: 0b26d25cc0665a08b2116fa5acafb51e76b39137b9697a40676a6d3b6245c76a

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

require "date"
require "net/ftp"

module FBO
  class RemoteFile < File
    FTP_SERVER = "ftp.fbo.gov"
    TMP_DIR    = "/tmp/fbo"

    class << self
      def for_date(date, options = {})
        filename = filename_for_date(date)
        FBO::RemoteFile.new(filename, options)
      end

      def filename_for_date(date)
        raise ArgumentError, "No date given for file" unless date
        "FBOFeed#{ date.strftime("%Y%m%d") }"
      end
    end

    def initialize(filename, options = {})
      @filename = filename
      @tmp_dir = options[:tmp_dir] || TMP_DIR
      @file = fetch_file(filename)
    end

    protected

    def fetch_file(filename)
      Dir.mkdir(@tmp_dir) unless Dir.exists?(@tmp_dir)
      tmp_filename = ::File.join(@tmp_dir, filename)
      ftp = Net::FTP.new(FTP_SERVER)
      ftp.login
      ftp.getbinaryfile(filename, tmp_filename)
      ftp.close
      ::File.new(tmp_filename)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fbo-0.0.1 lib/fbo/remote_file.rb