Module: ElPinguino

Defined in:
lib/el_pinguino.rb,
lib/el_pinguino/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.find_locations(**opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/el_pinguino.rb', line 15

def find_locations (**opts)
  city = opts.fetch(:city, nil)
  state = opts.fetch(:state, nil)
  country = opts.fetch(:country)
  location_url = "#{country}/#{state}/#{city}".gsub(' ','-').gsub('//','/')
  base_url = "http://www.travelmob.com/vacation-rentals/#{location_url}"

  if !opts.has_key?('pages')
    opts[:pages] = number_of_pages("#{base_url}")
  end

  options_url = ''
  opts.reject{ |k,v| [:city, :country, :pages].include?(k) }.each do |k,v|
    options_url << "?#{k}=#{v}"
  end

  results = []
  opts.fetch(:pages).times do |pg|
    url = "#{base_url}#{options_url}?page=#{(pg + 1).to_s}"
    puts url
    Nokogiri::HTML(open(url)).css(".space.row").each do |box|
      name = box.css('h4').text
      city = box.css('[itemprop="addressRegion"]').text
      price = box.css('.price').text
      bedrooms = box.css('.bedrooms').text
      capacity = box.css('.accommodates').text
      output = {country:  country,
                city:     city,
                name:     name,
                price:    price.to_i,
                bedrooms: bedrooms.to_i,
                capacity: capacity.to_i
      }.to_json
      results << output
    end
  end
  return results
end

.number_of_pages(url) ⇒ Object



9
10
11
12
13
# File 'lib/el_pinguino.rb', line 9

def number_of_pages (url)
  selector = "#paginator > div > div > ul > li:nth-last-child(2) > a"
  page_number = Nokogiri::HTML(open(url)).css(selector).text.to_i
  return page_number
end