Class: Boursorama

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

Defined Under Namespace

Classes: Account

Constant Summary collapse

VERSION =
'0.1'
HOST =
"www.boursorama.com"
LOGIN =
"/logunique.phtml"
SYNTHESE =
"/clients/synthese.phtml"
USER_AGENT =
"boursorama.rb/#{VERSION}"

Instance Method Summary collapse

Constructor Details

#initialize(user, password) ⇒ Boursorama

Returns a new instance of Boursorama.



136
137
138
139
140
# File 'lib/boursorama.rb', line 136

def initialize(user, password)
  (user, password)
  
  @synthese = Nokogiri::HTML(get(SYNTHESE).body)
end

Instance Method Details

#accountsObject



144
145
146
# File 'lib/boursorama.rb', line 144

def accounts
  @synthese.search("#synthese-list tr.L10").map {|acc| Account.new(self, acc) }
end

#get(url) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/boursorama.rb', line 109

def get(url)
  http = Net::HTTP.new(HOST, 443)
  http.use_ssl = true
  req = Net::HTTP::Get.new(url)
  req["User-Agent"] = USER_AGENT
  req["Cookie"] = @cookies.map {|k,v| "#{k}=#{v}"}.join("; ")

  http.request(req)
end

#inspectObject



142
# File 'lib/boursorama.rb', line 142

def inspect; "#<#{self.class}:0x#{object_id}>" end

#login(user, password) ⇒ Object



130
131
132
133
134
# File 'lib/boursorama.rb', line 130

def (user, password)
  @cookies = nil
  res = post(LOGIN) {|req| req.form_data = {'login' => user, 'password' => password}}
  @cookies = Hash[*res.get_fields("set-cookie").map {|c| c.sub(/;.*$/, "").split("=") }.flatten]
end

#post(url) {|req| ... } ⇒ Object

Yields:

  • (req)


119
120
121
122
123
124
125
126
127
128
# File 'lib/boursorama.rb', line 119

def post(url)
  http = Net::HTTP.new(HOST, 443)
  http.use_ssl = true
  req = Net::HTTP::Post.new(url)
  req["User-Agent"] = USER_AGENT
  req["Cookie"] = @cookies.map {|k,v| "#{k}=#{v}"}.join("; ") if @cookies
  yield req if block_given?
  
  http.request(req)
end