klondike (owner)

Revisions

gist: 158801 Download_button fork
public
Public Clone URL: git://gist.github.com/158801.git
Embed All Files: show embed
Ruby #
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
98
99
100
require 'net/https'
require 'rexml/document'
require 'yaml'
 
CERT_FILENAME = File.dirname(__FILE__) + "/cacert.pem"
 
class Wesabe
  attr_accessor :email, :password, :accounts, :account_xml, :http
  
  def initialize(file)
    creds_from_file file
    initialize_http
  end
  
  def fetch_accounts
    self.account_xml = get_accounts_xml
    self.accounts = parse_accounts_list account_xml
  end
  
  def creds_from_file(file)
    creds = YAML.load_file file
    self.email = creds[:email]
    self.password = creds[:password]
  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 trigger_updates
    http.start do |wesabe|
      request = Net::HTTP::Post.new('/user/login')
      request.set_form_data({'email' => email, 'password' => password})
      response = http.request(request)
  
      case response
      when Net::HTTPOK
        STDERR.puts "Bad email and password"
      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(email, password)
      response = http.request(request)
  
      case response
      when Net::HTTPFound
        STDERR.puts "Incorrect email or password."
        response.each_header do |key, value|
          STDERR.puts "#{key}: #{value}"
        end
        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