Class: Mbanker::Crawler

Inherits:
Object
  • Object
show all
Defined in:
lib/mbanker/crawler.rb

Constant Summary collapse

BASE_URI =
'https://www.mbank.com.pl/'
PATHS =
{
  :login => 'logon.aspx',
  :logout => 'logout.aspx',
  :accounts_list => 'accounts_list.aspx',
  :account_details => 'account_details.aspx',
  :account_name_change => 'contract_change_name.aspx',
  :operation_history => 'account_oper_list.aspx'
}

Instance Method Summary collapse

Constructor Details

#initializeCrawler

Returns a new instance of Crawler.



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

def initialize 
  @agent = Mechanize.new
end

Instance Method Details

#can_find?(selector) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mbanker/crawler.rb', line 67

def can_find? selector
  not search(selector).empty?
end

#click(link_or_button) ⇒ Object



23
24
25
26
27
# File 'lib/mbanker/crawler.rb', line 23

def click link_or_button
  action = extract_action link_or_button
  parameters = extract_post_parameters link_or_button
  submit_navigation_form action, parameters
end

#extract_action(link) ⇒ Object



29
30
31
32
# File 'lib/mbanker/crawler.rb', line 29

def extract_action link
  onclick_value = link.attribute('onclick').value
  onclick_value.gsub(/\A.*\('/, '').gsub(/'.*\Z/,'')
end

#extract_post_parameters(link) ⇒ Object



34
35
36
37
# File 'lib/mbanker/crawler.rb', line 34

def extract_post_parameters link
  onclick_value = link.attribute('onclick').value
  onclick_value.gsub(/'/,'').split(',')[3]
end

#extract_text(selector) ⇒ Object



58
59
60
61
# File 'lib/mbanker/crawler.rb', line 58

def extract_text selector
  element = find_first(selector)
  element ? element.text.strip : ''
end

#extract_texts(selector) ⇒ Object



63
64
65
# File 'lib/mbanker/crawler.rb', line 63

def extract_texts selector
  search(selector).map(&:text).map(&:strip)
end

#find_first(selector) ⇒ Object



54
55
56
# File 'lib/mbanker/crawler.rb', line 54

def find_first selector
  @agent.page.at selector
end

#get_formObject



72
73
74
# File 'lib/mbanker/crawler.rb', line 72

def get_form
  @agent.page.form
end

#search(selector) ⇒ Object



50
51
52
# File 'lib/mbanker/crawler.rb', line 50

def search selector
  @agent.page.search(selector)
end

#submit_navigation_form(action, parameters) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/mbanker/crawler.rb', line 39

def submit_navigation_form action, parameters
  navigation_form = @agent.page.form
  navigation_form.action = action
  navigation_form.__PARAMETERS = parameters
  navigation_form.method = 'POST'
  navigation_form.submit
end

#visit(path_name) ⇒ Object



19
20
21
# File 'lib/mbanker/crawler.rb', line 19

def visit path_name
  @agent.get(BASE_URI + PATHS[path_name])
end