Class: AutoExpreso::Client

Inherits:
Object
  • Object
show all
Includes:
DataUtils
Defined in:
lib/autoexpreso.rb

Constant Summary collapse

AE_LOGIN =
'https://www.autoexpreso.com/Login.aspx'
AE_ACCOUNT =
'https://www.autoexpreso.com/dynamic/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataUtils

attributes, form_data, included, save_transactions

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/autoexpreso.rb', line 17

def initialize(*args)
  @client       = Mechanize.new
  @account      = Hash.new
  @transactions = []
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



15
16
17
# File 'lib/autoexpreso.rb', line 15

def 
  @account
end

#account_pageObject (readonly)

Returns the value of attribute account_page.



15
16
17
# File 'lib/autoexpreso.rb', line 15

def 
  @account_page
end

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/autoexpreso.rb', line 15

def client
  @client
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



15
16
17
# File 'lib/autoexpreso.rb', line 15

def transactions
  @transactions
end

Instance Method Details

#account_details(json: false) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/autoexpreso.rb', line 71

def (json: false)
  if json
    puts JSON.pretty_generate()
  else
    ap 
  end
end

#authenticate(username, password) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/autoexpreso.rb', line 28

def authenticate(username, password)
  @client.get(AE_LOGIN) do |page|
    page.form_with(id: form_data.) do |form|
      form[form_data.] = username
      form[form_data.] = password
    end.click_button
  end
end

#login(username, password) ⇒ Object



23
24
25
26
# File 'lib/autoexpreso.rb', line 23

def (username, password)
  authenticate(username, password)
  process_request
end

#process_requestObject



37
38
39
40
41
# File 'lib/autoexpreso.rb', line 37

def process_request
  process_transactions
  
  table_strip
end

#process_transactionsObject



43
44
45
# File 'lib/autoexpreso.rb', line 43

def process_transactions
  @account_page = @client.get(AE_ACCOUNT)
end

#save_accountObject



47
48
49
50
51
# File 'lib/autoexpreso.rb', line 47

def 
  attributes.map do |attribute|
    @account[attribute] = text_strip(form_data.send(attribute))
  end
end

#table_dataObject



57
58
59
# File 'lib/autoexpreso.rb', line 57

def table_data
  @account_page.search("#{form_data.} tbody tr")
end

#table_stripObject



61
62
63
64
65
66
67
68
69
# File 'lib/autoexpreso.rb', line 61

def table_strip
  table_data.each do |tr|
    tds = tr.search('td')
    next unless tds.count == 4

    save_transactions(tds, @transactions)
  end
  @account[:transactions] = @transactions
end

#text_strip(field_name) ⇒ Object



53
54
55
# File 'lib/autoexpreso.rb', line 53

def text_strip(field_name)
  @account_page.search(field_name).text.strip
end