Class: Ifin24::Client

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/ifin24/client.rb

Constant Summary collapse

LOGIN_FORM_URL =
'https://www.ifin24.pl/logowanie'.freeze
ENTRY_FORM_URL =
'https://www.ifin24.pl/zarzadzanie-finansami/transakcje/dodaj-wydatek'.freeze
LIST_URL =
'https://www.ifin24.pl/zarzadzanie-finansami/transakcje/lista'.freeze
LIMITS_URL =
'https://www.ifin24.pl/zarzadzanie-finansami/kontrola-wydatkow'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(login, password) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/ifin24/client.rb', line 12

def initialize(, password)
  @login, @password = , password
end

Instance Method Details

#accountsObject



28
29
30
# File 'lib/ifin24/client.rb', line 28

def accounts
  fetch_accounts
end

#agentObject



16
17
18
# File 'lib/ifin24/client.rb', line 16

def agent
  @agent ||= Mechanize.new
end

#agent=(agent) ⇒ Object



20
21
22
# File 'lib/ifin24/client.rb', line 20

def agent=(agent)
  @agent = agent
end

#categoriesObject



24
25
26
# File 'lib/ifin24/client.rb', line 24

def categories
  @categories ||= fetch_categories
end

#fetch_entries(curr_page = 1) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ifin24/client.rb', line 47

def fetch_entries(curr_page = 1)
  page = agent.get("#{LIST_URL}?pageNumber=#{curr_page}")
  total_pages = extract_entries_total_pages(page)
  entry_row_elements = page.search('table tbody tr')

  entries = []

  entry_row_elements.each do |entry_row_element|
    entry_elements = entry_row_element.search('td')
    next if entry_elements.size != 5

    entry = Entry.new

    title_column = entry_elements[2]
    entry.title = title_column.children[0].text.strip
    entry.note = title_column.search('span').text.strip

    date_column = entry_elements[1]
    entry.date = date_column.text.strip

    category_column = entry_elements[3]
    sub_category = Category.new(:name => category_column.children[0].text.strip)
    entry.sub_category = sub_category
      
    entry.tags = category_column.search('span').text.strip
    amount_column = entry_elements[4]
    entry.amount = amount_column.text.strip

    entries << entry
  end

  return entries, total_pages
end

#fetch_limits(date = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ifin24/client.rb', line 81

def fetch_limits(date = nil)
  limits_url = "#{LIMITS_URL}"
  if date
    date = date.is_a?(Date) ? date.strftime("%Y-%m") : date
    limits_url << "?data=#{date}"
  end

  page = agent.get(limits_url)

  limits = []

  table = page.search('table#expenses-tracking-limits tbody')
  table.search('tr').each do |tr|
    limit = Limit.new
    columns = tr.search('td')

    name_column = columns[0]
    limit.name = name_column.text.strip

    amounts_column = columns[2]
    match = amounts_column.text.strip.match(/^(?<amount>\d+,\d+) PLN z (?<max>\d+,\d+) PLN$/)
    if match
      limit.amount = sanitize_price(match['amount'])
      limit.max = sanitize_price(match['max'])
    end

    limits << limit
  end

  return limits
end

#logged?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ifin24/client.rb', line 126

def logged?
  @logged
end

#loginObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ifin24/client.rb', line 113

def 
  page = agent.get(LOGIN_FORM_URL)
  form = page.forms.first

  form['login'] = @login
  form['password'] = @password

  page = form.submit
  @logged = page.forms.first.nil?

  return logged?
end

#send_entry(entry) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ifin24/client.rb', line 32

def send_entry(entry)
  page = agent.get(ENTRY_FORM_URL)
  form = page.forms.first

  form['entry.title'] = entry.title.to_s
  form['entry.date'] = entry.date.to_s
  form['selectedBankAccount'] = entry..id.to_s
  form['entry.entryCategory.id'] = entry.sub_category.id.to_s
  form['entry.amount'] = entry.amount.to_s
  form['value'] = entry.tags.to_s
  form['entry.note'] = entry.note.to_s

  form.submit
end