Class: AutoExpreso::Client

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

Constant Summary collapse

AE_URL =
'https://www.autoexpreso.com/Login.aspx'

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.



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

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

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



13
14
15
# File 'lib/autoexpreso.rb', line 13

def 
  @account
end

#account_pageObject (readonly)

Returns the value of attribute account_page.



13
14
15
# File 'lib/autoexpreso.rb', line 13

def 
  @account_page
end

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/autoexpreso.rb', line 13

def client
  @client
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



13
14
15
# File 'lib/autoexpreso.rb', line 13

def transactions
  @transactions
end

Instance Method Details

#login(username, password) ⇒ Object



21
22
23
24
# File 'lib/autoexpreso.rb', line 21

def (username, password)
  process_request(username, password)
  ap @account
end

#process_request(username, password) ⇒ Object



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

def process_request(username, password)
  @client.get(AE_URL) do |page|
    @account_page = page.form_with(id: form_data.) do |form|
      form[form_data.] = username
      form[form_data.] = password
    end.click_button
  end
  
  table_strip
end

#save_accountObject



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

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

#table_dataObject



45
46
47
# File 'lib/autoexpreso.rb', line 45

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

#table_stripObject



49
50
51
52
53
54
55
56
57
# File 'lib/autoexpreso.rb', line 49

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



41
42
43
# File 'lib/autoexpreso.rb', line 41

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