Module: Hostelify

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

Constant Summary collapse

VERSION =
"0.8.4"

Class Method Summary collapse

Class Method Details

.find(*args, &block) ⇒ Object



10
11
12
13
14
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
# File 'lib/hostelify.rb', line 10

def self.find(*args, &block)
  options = { :currency => 'EUR', :date_from => Date.today+14, :directions => false, :robotstxt_check => false }
  options.merge!(args.pop) if args.last.kind_of? Hash
  
  if options[:robotstxt_check]
    robot_friendly = Robotstxt.allowed?('http://www.hostelworld.com', 'rowbot')
  else
    robot_friendly = true
  end
  
  if robot_friendly
    RestClient.head 'http://www.hostelworld.com/hosteldetails.php/' + options[:id].to_s do |response, request, result|
      redirect = response.headers[:location]
      redirect = redirect + '/directions' if options[:directions]
      date = Date.strptime(options[:date_from].to_s)
      options = {
        :date_from => date+1, 
        :date_to => date+8,
        :searchperformedflag => 1,
        :dynamicSearchFlag => 1,
        :packNo => 0,
        :currency => options[:currency]
      }
    
      RestClient.post redirect, options do |response, request, result|
        @resource = RestClient.get "http://www.hostelworld.com#{response.headers[:location]}", {:cookies => response.cookies}
      end
    end
    
    result = Hostelworld.parse @resource
  end
  

end