Created
May 15, 2012 14:18
-
-
Save jfrolich/2702112 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'nokogiri' | |
require 'wbw/exceptions' | |
module Wbw | |
class Client | |
attr_accessor :username, :session_id, :logged_in | |
def initialize params = {} | |
if params | |
@username = params[:username] | |
@cookie = params[:cookie] | |
@logged_in = params[:logged_in] | |
end | |
end | |
def to_hash | |
{ | |
username: @username, | |
cookie: @cookie, | |
logged_in: @logged_in | |
} | |
end | |
def session_id | |
if @cookie && match = /PHPSESSID=(?<session_id>[0-9a-z]+);/.match(@cookie) | |
match[:session_id] | |
end | |
end | |
def login username, password | |
logout if logged_in | |
params = parameterize action: 'login', username: username, password: password | |
self.username = username | |
resp = http.post "/index.php", params, headers | |
# because the server does not send back correct http codes we | |
# check if the response body includes "uitloggen" | |
if (/Uitloggen/m.match resp.body) | |
@logged_in = true | |
else | |
@logged_in = false | |
raise Wbw::Exceptions::Unauthorized if !@logged_in | |
end | |
end | |
def lists | |
if doc = fetch("/index.php?page=dashboard") | |
lists = doc.css ".view-lists tbody tr" | |
lists.map do |list| | |
entry = {} | |
entry[:lid] = /lid=(?<lid>[[:digit:]]+)/.match(list.at_css('a')['href'])[:lid].to_i | |
entry[:title] = list.at_css('a').content | |
entry[:balance] = list.at_css('.balance-pos').content[2..-1].to_f | |
entry | |
end | |
end | |
end | |
def payments lid | |
if doc = fetch("/index.php?page=balance&lid=#{lid.to_s}&sort_column=timestamp&rows=10000") | |
payments = doc.css "#list tbody tr" | |
payments.map do |payment| | |
entry = {} | |
entry[:by] = payment.at_css('.payment-by').content | |
entry[:description] = payment.at_css('.description').content | |
entry[:amount] = payment.at_css('.amount').content[2..-1].to_f | |
entry[:date] = payment.at_css('.date').content | |
entry[:participants] = payment.at_css('.participants').content | |
entry[:tid] = /tid=(?<tid>[[:digit:]]+)/.match(payment.at_css('.operations a')['href'])[:tid].to_i | |
entry | |
end | |
end | |
end | |
def participants lid | |
if doc = fetch("http://www.wiebetaaltwat.nl/index.php?lid=#{lid.to_s}&page=transaction&type=add") | |
participants = doc.css "#payment-members label" | |
participants.map do |participant| | |
entry = {} | |
entry[:name] = participant.content | |
entry[:uid] = participant.attr("for").match("factor_(?<uid>[0-9]+)")[:uid] | |
end | |
end | |
end | |
# creates a payment | |
# | |
# payment attributes: | |
# uid => 23124 | |
# description => "this is a payment" | |
# amount => 12.50 | |
# participants => [] | |
# uid: => 123123 | |
# times: => 2 | |
# | |
def create_payment lid, payment | |
payment[:payment_by] = payment.delete :uid | |
if payment[:participants] | |
payment.merge! payment.delete(:paticipants).inject({}) do |payment, participant| | |
payment["factor_#{participant[:uid]}"] = participant[:times] | |
end | |
end | |
params = parameterize payment.merge(action: 'add_transaction', lid: lid) | |
resp = http.post "/index.php", params, headers | |
end | |
def logout | |
doc = fetch("/index.php?action=logout") | |
@cookie = nil | |
@logged_in = false | |
true | |
end | |
private | |
def parameterize params | |
URI.escape params.collect { |k,v| "#{k}=#{v}" }.join('&') | |
end | |
def headers | |
{ 'cookie' => cookie } | |
end | |
def cookie | |
@cookie = @cookie || http.get('/index.php').response['set-cookie'] | |
end | |
def http | |
@http || @http = Net::HTTP.new('www.wiebetaaltwat.nl') | |
end | |
def fetch url | |
html = http.get(url,headers).body | |
if /Je hebt geen toegang/.match html | |
@logged_in = false | |
raise Wbw::Exceptions::Unauthorized | |
else | |
Nokogiri.HTML html | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment