Class: Gofundme::Search
- Inherits:
-
Object
- Object
- Gofundme::Search
- Defined in:
- lib/gofundme/search.rb
Instance Attribute Summary collapse
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#results ⇒ Object
Returns the value of attribute results.
Instance Method Summary collapse
- #fetch_results(page_number) ⇒ Object
-
#initialize(query) ⇒ Search
constructor
A new instance of Search.
Constructor Details
#initialize(query) ⇒ Search
Returns a new instance of Search.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gofundme/search.rb', line 9 def initialize(query) @results = 0 @pages = 0 @query = query STDERR.puts "Sleeping #{Gofundme::GOOD_CITIZEN_DELAY} seconds" if Gofundme::DEBUG sleep Gofundme::GOOD_CITIZEN_DELAY url = "https://www.gofundme.com/mvc.php?route=category&term=\"#{CGI.escape(@query)}\"" STDERR.puts "Gofundme::Search::initialize(): Fetching: #{url}" if Gofundme::DEBUG page = RestClient.get url nok = Nokogiri::HTML(page) nok.xpath("//h2").each do |h2| if h2.inner_text.strip =~ /(\d+) results found/ @results = $1.to_i end end # Site does not return more results than the first 1000 projects if @results > 1000 @results = 1000 end @pages = (@results / 9.0).ceil.to_i end |
Instance Attribute Details
#pages ⇒ Object
Returns the value of attribute pages.
6 7 8 |
# File 'lib/gofundme/search.rb', line 6 def pages @pages end |
#results ⇒ Object
Returns the value of attribute results.
7 8 9 |
# File 'lib/gofundme/search.rb', line 7 def results @results end |
Instance Method Details
#fetch_results(page_number) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gofundme/search.rb', line 33 def fetch_results(page_number) STDERR.puts "Gofundme::Search::results(): Sleeping #{Gofundme::GOOD_CITIZEN_DELAY} seconds" if Gofundme::DEBUG sleep Gofundme::GOOD_CITIZEN_DELAY url = "https://www.gofundme.com/mvc.php?route=homepage_norma/load_more&page=#{page_number}&term=\"#{CGI.escape(@query)}\"&country=&postalCode=&locationText=" STDERR.puts "Gofundme::Search::results(): Fetching: #{url}" if Gofundme::DEBUG page = RestClient.get url nok = Nokogiri::HTML(page) links = [] nok.xpath("//div[@class='fund-item']/a").each do |a| links.push a.attr('href') end return links end |