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
|