Module: CrawlerRocks::DSL

Defined in:
lib/crawler_rocks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_urlObject (readonly)

Returns the value of attribute current_url.



7
8
9
# File 'lib/crawler_rocks.rb', line 7

def current_url
  @current_url
end

#htmlObject (readonly)

Returns the value of attribute html.



7
8
9
# File 'lib/crawler_rocks.rb', line 7

def html
  @html
end

Instance Method Details

#get_view_stateObject



56
57
58
# File 'lib/crawler_rocks.rb', line 56

def get_view_state
  Hash[@doc.css('input[type="hidden"]').map {|d| [d[:name], d[:value]]}]
end

#post(url, opt = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/crawler_rocks.rb', line 47

def post url, opt = {}
  handle_response RestClient.post url, opt.merge({cookies: @cookies})
  # @curl.url = url
  # @curl.http_post(opt)
  # binding.pry
  # handle_response
  @current_url = url
end

#setup(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/crawler_rocks.rb', line 9

def setup opts = {}
  opts = Hash[opts.map{ |k, v| [k.to_sym, v] }]
  # @curl = Curl::Easy.new
  # @curl.follow_location = true
  # @curl.enable_cookies = true
  @cookies = nil
  # @encoding = opts.fetch(:encoding, 'utf-8')
end

#submit(submit_name = nil, form_data = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/crawler_rocks.rb', line 26

def submit submit_name=nil, form_data={}
  submit_selector = "input[type=\"submit\"][value=\"#{submit_name}\"]"

  if submit_name.nil?
    post_hash = get_view_state.merge(form_data)
  else
    post_hash = Hash[@doc.css(submit_selector).map{|node| [node[:name], node[:value]]}].merge(get_view_state).merge(form_data)
  end

  post_path = @doc.css(submit_selector).xpath('ancestor::form[1]//@action')[0].value

  uri = URI.parse(@current_url)
  if post_path[0] == '/'
    post_path = "#{uri.scheme}://#{uri.host}/"
  else
    post_path = URI.join("#{File.dirname(uri.to_s)}/", post_path).to_s
  end

  post post_path, post_hash
end

#visit(url) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/crawler_rocks.rb', line 18

def visit url
  # @curl.url = url
  # @curl.http_get
  # handle_response
  handle_response RestClient.get url
  @current_url = url
end