class SeaWitch::Client
Constants
- BASE_URL
- DASHBOARD_PATH
- LOGIN_PATH
- USER_AGENT
Attributes
logged_in[RW]
password[RW]
session_id[RW]
username[RW]
Public Class Methods
default_params()
click to toggle source
# File lib/sea_witch/client.rb, line 51 def self.default_params { 'j_idt14' => 'j_idt14', 'javax.faces.source' => 'j_idt14:submitLogin', 'javax.faces.partial.event' => 'click', 'javax.faces.partial.execute' => ':submitLogin j_idt14:username j_idt14:password', 'javax.faces.partial.render' => 'j_idt14:err', 'javax.faces.behavior.event' => 'action', 'javax.faces.partial.ajax' => 'true' } end
new(username, password)
click to toggle source
# File lib/sea_witch/client.rb, line 13 def initialize(username, password) self.username = username self.password = password self.logged_in = false self end
Public Instance Methods
balance()
click to toggle source
# File lib/sea_witch/client.rb, line 21 def balance @balance ||= dashboard.css('.cardInfo').children.collect(&:text).join.match(/(\$[0-9\.]+)/)[0].gsub('$', '').to_f end
logged_in?()
click to toggle source
# File lib/sea_witch/client.rb, line 25 def logged_in? !!logged_in end
login!()
click to toggle source
# File lib/sea_witch/client.rb, line 29 def login! connection.headers[:user_agent] = USER_AGENT response = connection.get(LOGIN_PATH) login = Nokogiri::HTML(response.body) viewstate = login.xpath('//input').select{|n| n['id'] =~ /viewstate/i}.first['value'] self.session_id = response.headers['set-cookie'].split(';').select{|c| c =~ /^JSESSIONID/i}[0] connection.headers[:cookie] = self.session_id login_params = self.class.default_params.merge({ 'j_idt14:username' => self.username, 'j_idt14:password' => self.password, 'javax.faces.ViewState' => viewstate }) response = connection.post LOGIN_PATH, login_params self.logged_in = true end
Private Instance Methods
connection()
click to toggle source
# File lib/sea_witch/client.rb, line 68 def connection @connection ||= Faraday.new(:url => BASE_URL) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end
dashboard()
click to toggle source
# File lib/sea_witch/client.rb, line 64 def dashboard @dashboard ||= Nokogiri::HTML(request_path(DASHBOARD_PATH).body) end
request_path(path)
click to toggle source
# File lib/sea_witch/client.rb, line 76 def request_path(path) login! unless logged_in? connection.get path end