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.



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

def current_url
  @current_url
end

#htmlObject (readonly)

Returns the value of attribute html.



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

def html
  @html
end

Instance Method Details

#get_view_stateObject



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

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

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



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

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



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

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



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

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



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

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