Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. require 'uri'
  2. require 'querystring'
  3. require 'net/https'
  4.  
  5. # requires the latest hpricot
  6. # gem install hpricot --source code.whytheluckystiff.net
  7.  
  8. require 'rubygems'
  9. require 'hpricot'
  10.  
  11. class Net::HTTPResponse
  12. def page
  13. @parsed_body ||= Hpricot(self.body)
  14. end
  15. end
  16.  
  17. class Website
  18.  
  19. QS = QueryString
  20.  
  21. def initialize(url)
  22. url = URI.parse(url)
  23. @http = Net::HTTP.new(url.host, url.port)
  24. @http.use_ssl = true if url.port == 443
  25. end
  26.  
  27. def get(path, data = nil, headers = nil)
  28. path = URI.parse(path)
  29. case data.class.to_s # I dont why I have to convert to string here, but class detection does not work
  30. when 'Hash' : path.query = QS.create( QS.parse(path.query.to_s).merge(data) )
  31. when 'String' : path.query = ( path.query.to_s ) + '&' + data
  32. end
  33. #breakpoint
  34. request('GET', path.to_s, nil, headers)
  35. end
  36.  
  37. protected
  38. def request(method, path, data, headers)
  39. @http.start do |http|
  40. http.send_request( method, path, data, headers )
  41. end
  42. end
  43.  
  44. end
Add Comment
Please, Sign In to add comment