Class: Nacha::AchFile

Inherits:
Object
  • Object
show all
Defined in:
lib/nacha/ach_file.rb

Overview

Class for handling ACH files, which can be a URL, a file path, or a string of data. encapsulating the records and handling output

Constant Summary collapse

TEMPLATES_DIR =
File.join(Gem::Specification.find_by_name("nacha").gem_dir,
"templates").freeze
HTML_PREAMBLE_FILE =
File.join(TEMPLATES_DIR, "html_preamble.html")
HTML_POSTAMBLE_FILE =
File.join(TEMPLATES_DIR, "html_postamble.html")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_string) ⇒ AchFile

Returns a new instance of AchFile.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nacha/ach_file.rb', line 20

def initialize(input_string)
  @input_type = classify_input(input_string)
  @records = []
  case @input_type
  when 'url'
    @input_source = input_string
    @raw_input = read_url_contents_httparty(input_string)
  when 'filepath'
    raise ArgumentError, "File not found: #{input_string}" unless File.exist?(input_string)

    @input_source = input_string
    @raw_input = File.read(input_string)
  when 'string of data'
    @input_source = "string"
    @raw_input = input_string
  else
    if input_string.respond_to?(:read)
      # If input_string is an IO object (like $stdin), read it directly
      @input_source = "stdin"
      @raw_input = input_string.read
    else
      # Otherwise, treat it as a simple string
      @input_source = "string"
      @raw_input = input_string.to_s
    end
  end
  @checksum = OpenSSL::Digest::SHA256.hexdigest(@raw_input) if @raw_input
end

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def checksum
  @checksum
end

#created_atObject (readonly)

Returns the value of attribute created_at.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def created_at
  @created_at
end

#input_sourceObject (readonly)

Returns the value of attribute input_source.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def input_source
  @input_source
end

#input_typeObject (readonly)

Returns the value of attribute input_type.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def input_type
  @input_type
end

#modified_atObject (readonly)

Returns the value of attribute modified_at.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def modified_at
  @modified_at
end

#raw_inputObject (readonly)

Returns the value of attribute raw_input.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def raw_input
  @raw_input
end

#recordsObject (readonly)

Returns the value of attribute records.



17
18
19
# File 'lib/nacha/ach_file.rb', line 17

def records
  @records
end

Instance Method Details

#parseObject



49
50
51
52
# File 'lib/nacha/ach_file.rb', line 49

def parse
  @records = Nacha::Parser.new.parse_string(@raw_input)
  self
end

#to_achObject



54
55
56
# File 'lib/nacha/ach_file.rb', line 54

def to_ach
  records.map(&:to_ach).join("\n")
end

#to_htmlObject



62
63
64
# File 'lib/nacha/ach_file.rb', line 62

def to_html
  formatter(:html).format
end

#to_jsonObject



58
59
60
# File 'lib/nacha/ach_file.rb', line 58

def to_json
  formatter(:json).format
end

#to_markdownObject



66
67
68
# File 'lib/nacha/ach_file.rb', line 66

def to_markdown
  formatter(:markdown).format
end