Class: OFX::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ofx.rb', line 24

def initialize(&block)
  @headers = [
              [ "OFXHEADER", "100" ],
              [ "DATA", "OFXSGML" ],
              [ "VERSION", "103" ],
              [ "SECURITY", "NONE" ],
              [ "ENCODING", "USASCII" ],
              [ "CHARSET", "1252" ],
              [ "COMPRESSION", "NONE" ],
              [ "OLDFILEUID", "NONE" ],
              [ "NEWFILEUID", "NONE" ]
             ]
  @transactions = []
  self.dtserver = Date.today
  if block_given?
    yield self
  end
end

Instance Attribute Details

#acct_idObject

Returns the value of attribute acct_id.



13
14
15
# File 'lib/ofx.rb', line 13

def acct_id
  @acct_id
end

#acct_typeObject

CHECKING, SAVINGS, MONEYMRKT, CREDITLINE



14
15
16
# File 'lib/ofx.rb', line 14

def acct_type
  @acct_type
end

#bal_amtObject

Returns the value of attribute bal_amt.



21
22
23
# File 'lib/ofx.rb', line 21

def bal_amt
  @bal_amt
end

#bank_idObject

Returns the value of attribute bank_id.



12
13
14
# File 'lib/ofx.rb', line 12

def bank_id
  @bank_id
end

#dtasofObject

Returns the value of attribute dtasof.



22
23
24
# File 'lib/ofx.rb', line 22

def dtasof
  @dtasof
end

#dtendObject

Returns the value of attribute dtend.



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

def dtend
  @dtend
end

#dtserverObject

Returns the value of attribute dtserver.



10
11
12
# File 'lib/ofx.rb', line 10

def dtserver
  @dtserver
end

#dtstartObject

Returns the value of attribute dtstart.



16
17
18
# File 'lib/ofx.rb', line 16

def dtstart
  @dtstart
end

#fi_fidObject

Returns the value of attribute fi_fid.



9
10
11
# File 'lib/ofx.rb', line 9

def fi_fid
  @fi_fid
end

#fi_orgObject

Returns the value of attribute fi_org.



8
9
10
# File 'lib/ofx.rb', line 8

def fi_org
  @fi_org
end

#transactionsObject

Returns the value of attribute transactions.



19
20
21
# File 'lib/ofx.rb', line 19

def transactions
  @transactions
end

Instance Method Details

#format_amount(amount) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/ofx.rb', line 136

def format_amount(amount)
  if amount > 0
    "+#{amount.to_s('F')}"
  else
    "#{amount.to_s('F')}"
  end
end

#format_balance(balance) ⇒ Object



152
153
154
# File 'lib/ofx.rb', line 152

def format_balance(balance)
  balance.to_s('F')
end

#format_date(time) ⇒ Object



132
133
134
# File 'lib/ofx.rb', line 132

def format_date(time)
  time.strftime("%Y%m%d")
end

#format_datetime(time) ⇒ Object



128
129
130
# File 'lib/ofx.rb', line 128

def format_datetime(time)
  time.strftime("%Y%m%d000000")
end

#format_trntype(amount) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/ofx.rb', line 144

def format_trntype(amount)
  if amount > 0
    "CREDIT"
  else
    "DEBIT"
  end
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ofx.rb', line 62

def print_body
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.OFX {
      xml.SIGNONMSGSRSV1 {
        xml.SONRS {
          xml.STATUS {
            xml.CODE "0"
            xml.SEVERITY "INFO"
          }
          xml.DTSERVER format_datetime(self.dtserver)
          xml.LANGUAGE "ENG"
          xml.FI {
            xml.ORG self.fi_org
            xml.FID self.fi_fid
          }
          xml.send "INTU.BID", self.fi_fid
        }
      }
      xml.BANKMSGSRSV1 {
        xml.STMTTRNRS {
          xml.TRNUID "0"
          xml.STATUS {
            xml.CODE "0"
            xml.SEVERITY "INFO"
          }
          xml.STMTRS {
            xml.CURDEF "USD"
            xml.BANKACCTFROM {
              xml.BANKID self.bank_id
              xml.ACCTID self.acct_id
              xml.ACCTTYPE self.acct_type
            }
            xml.BANKTRANLIST {
              if self.dtstart
                xml.DTSTART format_date(self.dtstart)
              end
              if self.dtend
                xml.DTEND format_date(self.dtend)
              end
              self.transactions.each do |transaction|
                xml.STMTTRN {
                  xml.TRNTYPE format_trntype(transaction.trnamt)
                  xml.DTPOSTED format_date(transaction.dtposted)
                  xml.TRNAMT format_amount(transaction.trnamt)
                  xml.FITID transaction.fitid
                  xml.NAME transaction.name
                  xml.MEMO transaction.memo
                }
              end
            }
            xml.LEDGERBAL {
              if self.bal_amt
                xml.BALAMT format_balance(self.bal_amt)
              end
              if self.dtasof
                xml.DTASOF format_date(self.dtasof)
              end
            }
          }
        }
      }
    }
  end
  builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
end


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

def print_headers
  @headers.map { |key, value| "#{key}:#{value}" }.join("\n") + "\n\n" 
end

#to_ofxObject



53
54
55
56
# File 'lib/ofx.rb', line 53

def to_ofx
  print_headers +
    print_body
end

#transaction {|transaction| ... } ⇒ Object

Yields:



47
48
49
50
51
# File 'lib/ofx.rb', line 47

def transaction(&block)
  transaction = OFX::Transaction.new
  yield transaction
  self.transactions.push transaction
end