class VantivSFTPReports::Fetch

Attributes

config[R]

Public Class Methods

new(config = VantivSFTPReports.default_config) click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 17
def initialize(config = VantivSFTPReports.default_config)
  @config = Config.with_obj(config)
end

Public Instance Methods

call(*args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
session() { |sftp| ... } click to toggle source
# 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

Private Instance Methods

date_str(obj) click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 65
def date_str(obj)
  obj.respond_to?(:strftime) ? obj.strftime('%Y%m%d') : obj.to_s
end
filter_by_date!(list, dates) click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 69
def filter_by_date!(list, dates)
  list.select! { |r| r =~ /_(#{Array(dates).map { |d| date_str(d) }.join('|')})\.CSV\Z/ }
end
filter_by_organization_id!(list, organization_id) click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 73
def filter_by_organization_id!(list, organization_id)
  list.select! { |r| r =~ /_#{organization_id}_/ }
end
filter_by_pattern!(list, pattern) click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 77
def filter_by_pattern!(list, pattern)
  pattern = pattern.is_a?(Regexp) ? pattern : /^#{pattern}/i
  list.select! { |r| r =~ pattern }
end
parse_report(str) click to toggle source

To avoid numberic conversion, IDs from Vantiv are prefixed with a single quote. Since no conversions are done here, this is nothing but an inconvenience and is stripped.

# File lib/vantiv_sftp_reports/fetch.rb, line 85
def parse_report(str)
  CSV.parse(str.gsub(/'(\d{18})/, '\1'), headers: true, header_converters: :symbol)
end
proxy() click to toggle source
# File lib/vantiv_sftp_reports/fetch.rb, line 59
def proxy
  return nil unless config.proxy_url
  uri = URI(config.proxy_url)
  Net::SSH::Proxy::HTTP.new(uri.host, uri.port)
end