class RsrGroup::ResponseFile

Attributes

content[RW]
filename[R]
get_content[RW]
mtime[RW]

Public Class Methods

all(options = {}) click to toggle source
# File lib/rsr_group/response_file.rb, line 41
def self.all(options = {})
  requires!(options, :username, :password)

  Base.connect(options) do |ftp|
    ftp.chdir(RsrGroup.config.response_dir)
    @resp = ftp.nlst("*.txt")
    ftp.close
  end

  @resp
end
get_each(options = {}) { |resource| ... } click to toggle source
# File lib/rsr_group/response_file.rb, line 21
def self.get_each(options = {}, &block)
  requires!(options, :username, :password)

  Base.connect(options) do |ftp|
    ftp.chdir(RsrGroup.config.response_dir)

    @list = ftp.nlst("*.txt")
    @list.each do |file|
      resource         = new(options.merge(filename: file))
      resource.content = ftp.gettextfile(file, nil)
      resource.mtime   = ftp.mtime(file)
      yield(resource)
    end

    ftp.close
  end

  @list  
end
new(options = {}) click to toggle source
# File lib/rsr_group/response_file.rb, line 7
def initialize(options = {})
  requires!(options, :username, :password, :filename)

  @credentials    = options.select { |k, v| [:username, :password].include?(k) }
  @filename       = File.basename(options[:filename])
  @account_number = @filename.split('-')[2]
end

Public Instance Methods

response_type() click to toggle source
# File lib/rsr_group/response_file.rb, line 64
def response_type
  FILE_TYPES[@filename.split("-").first]
end
to_json() click to toggle source
# File lib/rsr_group/response_file.rb, line 68
def to_json
  get_content

  if @content.length == 0
    raise ZeroByteFile.new("File is empty (filename: #{@filename})")
  end

  @json = {
    response_type: response_type,
    filename: @filename,
    account_number: @account_number,
    rsr_order_number: nil,
    details: [],
    errors: [],
  }

  return parse_eerr  if error?
  return parse_econf if confirmation?
  return parse_eship if shipping?
end

Private Instance Methods

parse_econf() click to toggle source
# File lib/rsr_group/response_file.rb, line 101
def parse_econf
  details = @content.lines[2..-3].map do |line|
    DataRow.new(line).to_h
  end.compact

  @json.merge!({
    rsr_order_number: @content.lines[1].split(";")[2].chomp,
    details: details
  })
end
parse_eerr() click to toggle source
# File lib/rsr_group/response_file.rb, line 91
def parse_eerr
  errors = @content.lines[0..-2].map do |line|
    DataRow.new(line, has_errors: true).to_h
  end.compact

  errors.select! { |e| !e[:error_code].nil? }

  @json.merge!(errors: errors)
end
parse_eship() click to toggle source
# File lib/rsr_group/response_file.rb, line 112
def parse_eship
  details = @content.lines[1..-3].map do |line|
    DataRow.new(line).to_h
  end.compact

  @json.merge!({
    details: details
  })
end