Class: Yodlee::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(credentials, logger = nil) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/yodlee/connection.rb', line 3

def initialize(credentials, logger = nil)
  @credentials = credentials
  @logger = logger
  
  @connected = false
  @accounts = nil

  @agent = WWW::Mechanize.new
  @agent.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5'

  @uris = {}
end

Instance Method Details

#account_info(acct) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yodlee/connection.rb', line 40

def (acct)
  page = @agent.get(@uris[:accounts])

  link = page.links.detect{|lnk| lnk.href =~ /itemAccountId=#{acct.id}/ } or raise AccountNotFound, "Could not find account in list"
  link.href << "&dateRangeId=-1"
  page = link.click

  doc = Nokogiri::HTML.parse(page.body)

  last_upd, next_upd = doc.at(".accountlinks").text.scan(/Last updated (.*?)\s*\(next scheduled update (.*)\)/).flatten

  # Regular accounts have a heading + div, investments have heading + table
  regular_acct = doc.at("h2[contains('Account Overview')] + div")

   = regular_acct ? (doc) : (doc)
  [:next_update]  = next_upd
  [:last_updated] = last_upd

  csv_page = page.form_with(:name => 'rep').submit
  [:simple_transactions] = csv_page.response['content-type'] =~ /csv/ ? csv_page.body : []

  
end

#accountsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yodlee/connection.rb', line 16

def accounts
  return @accounts if @accounts
  
  handle_connection!
  
  page = @agent.get(@uris[:accounts])
  doc = Nokogiri::HTML.parse(page.body)

  @accounts = doc.search(".acctbean a").map{|e|
    acct = Account.new(self)

    e['href'].scan(/(\w+Id)=(\d+)/).each do |k,v|
      case k 
        when /itemAccountId/ then acct.id = v
        when /itemId/        then acct.institute_id = v
      end
    end

    acct.institute_name = e.at('strong').text
    acct.name = e.children.last.text.sub(/^\s*-\s*/,'')
    acct
  }
end

#answer_question(page) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/yodlee/connection.rb', line 253

def answer_question(page)
  question = Nokogiri::HTML.parse(page.body).at("label[@for=answer]").text
  log(:debug, question)
  
  begin
    answer = @credentials.answers.detect{|q, a| question =~ /^#{Regexp.escape(q)}/}.last
  rescue 
    raise NoAnswerForQuestion, "No answer found for #{question}"
  end

  f = page.form_with(:name => 'loginForm')
  f['answer'] = answer

  @agent.submit(f)
end

#check_expectation(page) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/yodlee/connection.rb', line 269

def check_expectation(page)
  d = Nokogiri::HTML.parse(page.body)
  node = d.at("dl > dt[contains('Secret Phrase')] + dd .caption")

  if node
    if @credentials.expectation == node.previous.text.strip
      return page
    else
      raise ExpectationMismatch, "Expectation found, but was incorrect"
    end
  else
    raise ExpectationNotFound, "Didn't find expectation"
  end
end

#connected?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/yodlee/connection.rb', line 236

def connected?
  @connected
end

#handle_connection!Object



221
222
223
# File 'lib/yodlee/connection.rb', line 221

def handle_connection!
   unless connected?
end

#investment_account_info(doc) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/yodlee/connection.rb', line 74

def (doc)
   = {}
  [:holdings] = [] # TODO
  [:current_balance] = 
    doc.search("h2[contains('Account Overview')] + table tr[last()] td[last()]").text
  
end

#log(level, msg) ⇒ Object



240
241
242
# File 'lib/yodlee/connection.rb', line 240

def log(level, msg)
  @logger.__send__(level, question) if @logger
end

#loginObject



225
226
227
228
229
230
231
232
233
234
# File 'lib/yodlee/connection.rb', line 225

def 
  @connected = false
  page = nil

  %w(provide_username answer_question check_expectation provide_password).each do |m|
    page = send(*[m, page].compact)
  end
  
  @connected = true
end

#provide_password(page) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/yodlee/connection.rb', line 284

def provide_password(page)
  f = page.form_with(:name => 'loginForm')
  f['password'] = @credentials.password
  page = @agent.submit(f)

  # ack javascript disabled
  f = page.form_with(:name => 'updateForm')
  page = @agent.submit(f)

  link = page.links.detect { |lnk| lnk.attributes["id"] == "ACCOUNT_BALANCE" }

  @uris[:accounts] = link.href.to_s << "&filter_id=-1"
end

#provide_usernameObject

login scrapers



246
247
248
249
250
251
# File 'lib/yodlee/connection.rb', line 246

def provide_username
  p = @agent.get 'https://moneycenter.yodlee.com/'
  f = p.form_with(:name => 'loginForm')
  f['loginName'] = @credentials.username
  @agent.submit(f)
end

#regular_account_info(doc) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/yodlee/connection.rb', line 64

def (doc)
  info_block = doc.at("h2[contains('Account Overview')] + div")

  Hash[*info_block.search("dt").
      zip(info_block.search("dt+dd")).
      map{|a,b|[a.text.gsub(/\W/,'').underscore.to_sym, b.text]}.
      flatten
  ]
end

#transactions(acct) ⇒ Object

This method returns each transaction as an object, based on the underyling javascript structures used to build the transactions as displayed in the Yodlee UI. These objects are able to access more information than the CSV Yodlee provides, such as the finanical institute’s transaction id, useful for tracking duplicates.

Calling this method requires Johnson to be installed, otherwise an exception is raised.



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/yodlee/connection.rb', line 88

def transactions(acct)
  unless Object.const_defined? "Johnson"
    raise "Johnson not found. Install the johnson gem, or use simple_transactions instead." 
  end

  post_headers = {
    "c0-scriptName"=>"TxnService", 
    "c0-methodName"=>"searchTransactions", 
    "c0-id"=>"#{rand(5000)}_#{Time.now.to_i}#{Time.now.usec / 1000}}", 
    "c0-e1"=>"number:10000004", 
    "c0-e2"=>"string:17CBE222A42161A3FF450E47CF4C1A00", 
    "c0-e3"=>"null:null", 
    "c0-e4"=>"number:1", 
    "c0-e5"=>"boolean:false", 
    "c0-e6"=>"string:#{acct.id}", 
    "c0-e7"=>"string:-1", 
    "c0-e8"=>"null:null", 
    "c0-e9"=>"string:-1", 
    "c0-e10"=>"null:null", 
    "c0-e11"=>"null:null", 
    "c0-e12"=>"null:null",
    "c0-e13"=>"string:-1", 
    "c0-e14"=>"null:null", 
    "c0-e15"=>"number:-1", 
    "c0-e16"=>"number:-1", 
    "c0-e17"=>"boolean:false", 
    "c0-e18"=>"Boolean:false", 
    "c0-e19"=>"boolean:false", 
    "c0-e20"=>"Boolean:false", 
    "c0-e21"=>"string:", 
    "c0-e22"=>"string:", 
    "c0-e23"=>"Boolean:false", 
    "c0-e24"=>"Boolean:false", 
    "c0-e25"=>"boolean:false", 
    "c0-e26"=>"Number:0", 
    "c0-e27"=>"string:0", 
    "c0-e28"=>"null:null", 
    "c0-e29"=>"null:null", 
    "c0-e30"=>"string:allTransactions", 
    "c0-e31"=>"string:InProgressAndCleared", 
    "c0-e32"=>"number:999", 
    "c0-e33"=>"string:", 
    "c0-e34"=>"null:null", 
    "c0-e35"=>"null:null", 
    "c0-e36"=>"string:", 
    "c0-e37"=>"null:null", 
    "c0-e38"=>"string:ALL", 
    "c0-e39"=>"string:false", 
    "c0-e40"=>"string:0.0", 
    "c0-e41"=>"string:0.0", 

    "c0-param0"=>"Object:{
       cobrandId:reference:c0-e1,
       applicationId:reference:c0-e2,
       csit:reference:c0-e3,
       loggingLevel:reference:c0-e4,
       loggingEnabled:reference:c0-e5}", 

    "c0-param1"=>"Object:{
      itemAccountId:reference:c0-e6,
      categoryId:reference:c0-e7,
      categoryLevelId:reference:c0-e8,
      dateRangeId:reference:c0-e9,
      fromDate:reference:c0-e10,
      toDate:reference:c0-e11,
      groupBy:reference:c0-e12,
      groupAccountId:reference:c0-e13,
      filterTranasctions:reference:c0-e14,
      transactionTypeId:reference:c0-e15,
      transactionStatusId:reference:c0-e16,
      ignorePendingTransactions:reference:c0-e17,
      includeBusinessExpense:reference:c0-e18,
      includeTransfer:reference:c0-e19,
      includeReimbursableExpense:refrence:c0-e20,
      fromDate1:reference:c0-e21,
      toDate1:reference:c0-e22,
      includeMedicalExpense:reference:c0-e23,
      includeTaxDeductible:reference:c0-e24,
      includePersonalExpense:reference:c0-e25,
      transactionAmount:reference:c0-e26,
      transactionAmountRange:reference:c0-e27,
      billStatementRange:reference:c0-e28,
      criteria:reference:c0-e29,
      module:reference:c0-e30,
      transactionType:reference:c0-e31,
      pageSize:reference:c0-e32,
      sharedMemId:reference:c0-e33,
      overRideDateRangeId:reference:c0-e34,
      overRideContainer:referencec0-e35,
      searchString:reference:c0-e36,
      pageId:reference:c0-e37,
      splitTypeTransaction:reference:c0-e38,
      isAvailableBalance:reference:c0-e39,
      currentBalance:reference:c0-e40,
      availableBalance:reference:c0-e41}",

    "c0-param2"=>"boolean:false", 

    "callCount"=>"1", 
    "xml"=>"true", 
  }
  page = @agent.post(
    'https://moneycenter.yodlee.com/moneycenter/dwr/exec/TxnService.searchTransactions.dwr',
    post_headers
  )

  j = Johnson::Runtime.new

  # Remove the last line (a call to DWREngine), and execute
  j.evaluate page.body.strip.sub(/\n[^\n]+\Z/m, '')

  if x = j['s0']
    transactions = x.transactions.map do |e|
      transaction = Yodlee::Transaction.new
      transaction. = e.accountName
      transaction.currency = e.amount.cobCurrencyCode
      transaction.amount = e.amount.cobPreciseAmount
      transaction.description = e.description
      transaction. = e.itemAccountId
      transaction.fit_id = e.transactionId
      transaction.status = e['type']['type']

      # Re-parse in order to get a real Time, not a Johnson::SpiderMonkey::RubyLandProxy.
      transaction.date = Time.parse(e.date.to_s)
      transaction
    end

    return transactions
  end

  return []
end