Class: VantivSFTPReports::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/vantiv_sftp_reports/fetch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = VantivSFTPReports.default_config) ⇒ Fetch

Returns a new instance of Fetch.



17
18
19
# File 'lib/vantiv_sftp_reports/fetch.rb', line 17

def initialize(config = VantivSFTPReports.default_config)
  @config = Config.with_obj(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/vantiv_sftp_reports/fetch.rb', line 15

def config
  @config
end

Instance Method Details

#call(*args) ⇒ Object



21
22
23
24
25
26
# File 'lib/vantiv_sftp_reports/fetch.rb', line 21

def call(*args)
  download(*list(*args)).each_with_object({}) do |(filename, csv), h|
    next unless csv
    h[filename] = parse_report(csv)
  end
end

#download(*filenames) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/vantiv_sftp_reports/fetch.rb', line 28

def download(*filenames)
  {}.tap do |h|
    session do |sftp|
      filenames.each { |fname| h[fname] = sftp.download!("#{path}/#{fname}") }
    end
  end
end

#first(*args) ⇒ Object



36
37
38
# File 'lib/vantiv_sftp_reports/fetch.rb', line 36

def first(*args)
  (report = call(*args).first) && report.last
end

#list(pattern = nil, by_date: Date.today, by_organization_id: organization_id) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/vantiv_sftp_reports/fetch.rb', line 40

def list(pattern = nil, by_date: Date.today, by_organization_id: organization_id)
  [].tap do |list|
    session { |sftp| sftp.dir.foreach(path) { |e| list << e.name unless e.name[0] == '.' } }
    filter_by_date!(list, by_date) if by_date
    filter_by_organization_id!(list, by_organization_id) if organization_id
    filter_by_pattern!(list, pattern) if pattern
  end
end

#sessionObject



49
50
51
52
53
# File 'lib/vantiv_sftp_reports/fetch.rb', line 49

def session
  sftp_opts = config.sftp_opts
  sftp_opts.merge!(proxy: proxy).delete(:proxy_url) if sftp_opts[:proxy_url]
  Net::SFTP.start(config.host, config.username, sftp_opts) { |sftp| yield(sftp) }
end