Class: Mechanize

Inherits:
Object
  • Object
show all
Defined in:
lib/phantom_mechanize/ext/mechanize.rb

Instance Method Summary collapse

Instance Method Details

#phget(url, *args) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phantom_mechanize/ext/mechanize.rb', line 2

def phget url, *args
   args = args[0] || {}
   wait = args[:wait] || 10000
   selector = args[:selector] || ""
   selector = [selector] if selector.is_a?(String)
   js = args[:js] || ""
   js = [js] if js.is_a?(String)

   pc = cookies.map{|c| [c.name, c.value, c.domain, c.path, c.httponly, c.secure, c.expires.to_i]}.to_json

   ph_args = []
   ph_args << "--proxy=#{proxy_addr}:#{proxy_port}" if proxy_port && proxy_addr
   # puts "phantomjs #{ph_args.join(' ')} \"#{PhantomMechanize::JS_FOLDER}/phget.js\" \"#{url}\" \"#{wait}\" \"#{selector.to_json.gsub('"', '\"')}\" \"#{pc.gsub('"', '\"')}\" \"#{user_agent.gsub('"', '\"')}\" \"#{js.to_json.gsub('"', '\"')}\""


   response = `phantomjs #{ph_args.join(' ')} "#{PhantomMechanize::JS_FOLDER}/phget.js" "#{url}" "#{wait}" "#{selector.to_json.gsub('"', '\"')}" "#{pc.gsub('"', '\"')}" "#{user_agent.gsub('"', '\"')}" "#{js.to_json.gsub('"', '\"')}"`
   mcs, html = response.split '<<<phget_separator>>>'
   JSON.parse(mcs).each do |mc|
     cookie = Cookie.new Hash[mc.map{|k, v| [k.to_sym, v]}]
     cookie_jar << cookie
   end

   page = Mechanize::Page.new URI.parse(url), [], html, nil, self
end