gist: 3861 Download_button fork
public
Description:
Turned this into a project: http://github.com/klondike/wesabe
Public Clone URL: git://gist.github.com/3861.git
usage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
##########################################################################
# This is now a proper Github project: http://github.com/klondike/wesabe
##########################################################################
 
wesabe = Wesabe.new 'username', 'password'
 
# login to trigger automatic updates, so your account info will be fresh
wesabe.trigger_updates
 
# fetch your account data (you may want to wait a minute after triggering updates)
wesabe.fetch_accounts
 
# the floating point balance of account #1
wesabe[1][:balance]
 
# the integer balance in cents of account #1
wesabe[1][:balance_in_cents]
 
# name of account #1
wesabe[1][:name]
wesabe.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
##########################################################################
# This is now a proper Github project: http://github.com/klondike/wesabe
##########################################################################
 
require 'net/https'
require 'rexml/document'
require 'yaml'
 
CERT_FILENAME = File.dirname(__FILE__) + "/cacert.pem"
 
class Wesabe
  attr_accessor :username, :password, :accounts, :account_xml, :http
  
  def initialize(username, password)
    self.username = username
    self.password = password
    initialize_http
  end
  
  def initialize_http
    unless File.exist?(CERT_FILENAME)
      puts "Downloading certificate..."
      cert = Net::HTTP.get('curl.haxx.se', '/ca/cacert.pem')
      File.open(CERT_FILENAME, "w") { |f| f << cert }
      puts "Certificate downloaded."
    end
    self.http = Net::HTTP.new("www.wesabe.com", 443)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file = CERT_FILENAME
  end
  
  def fetch_accounts
    self.account_xml = get_accounts_xml
    self.accounts = parse_accounts_list account_xml
  end
  
  def trigger_updates
    http.start do |wesabe|
      request = Net::HTTP::Post.new('/user/login')
      request.set_form_data({'username' => username, 'password' => password})
      response = http.request(request)
  
      case response
      when Net::HTTPOK
        STDERR.puts "Bad username and password"
        exit(-1)
      when Net::HTTPRedirection
        puts "Logged in successfully"
        response.body
      else
        STDERR.puts "Unexpected response: #{response.inspect}"
        exit(-1)
      end
    end
  end
  
  def get_accounts_xml
    http.start do |wesabe|
      request = Net::HTTP::Get.new('/accounts.xml')
      request.basic_auth(username, password)
      response = http.request(request)
  
      case response
      when Net::HTTPFound
        STDERR.puts "Incorrect username or password."
        exit(-1)
      when Net::HTTPOK
        response.body
      else
        STDERR.puts "Unexpected response: #{response.inspect}"
        exit(-1)
      end
    end
  end
  
  def parse_accounts_list(xml)
    accounts = {}
    doc = REXML::Document.new(xml)
    doc.root.each_element('//account') do |account|
      id = account.elements["id"].text
      name = account.elements["name"].text
      balance = account.elements["current-balance"].text.to_f
      balance_in_cents = (balance * 100).to_i
      accounts[id] = {
        :name => name,
        :balance => balance,
        :balance_in_cents => balance_in_cents
      }
    end
    accounts
  end
  
  def [](id)
    accounts[id.to_s]
  end
end

Owner

klondike

Revisions

  • 8e577a klondike Mon Aug 04 07:57:13 -0700 2008
  • 2e347c klondike Mon Aug 04 07:56:53 -0700 2008
  • 17613b klondike Sun Aug 03 22:46:11 -0700 2008
  • c041a2 klondike Sun Aug 03 22:45:50 -0700 2008
  • ff1db3 klondike Sun Aug 03 22:37:02 -0700 2008
  • c30565 klondike Sun Aug 03 22:32:43 -0700 2008