Class: Hostelbookers

Inherits:
Object
  • Object
show all
Defined in:
lib/hostelify/hostelbookers.rb

Constant Summary collapse

HB_SINGULAR_DETAIL_URL =

constants

"http://www.hostelbookers.com/hostels/"
HB_PLURAL_HOSTELS_URL =

poland/krakow/

"http://www.hostelbookers.com/hostels/"

Class Method Summary collapse

Class Method Details

.find_hostel_by_id(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hostelify/hostelbookers.rb', line 46

def self.find_hostel_by_id(options)
  city = options[:location].split(',').first.gsub(' ','')
  country = options[:location].split(',').last.gsub(' ','')
  id = options[:id]
  url = HB_SINGULAR_DETAIL_URL + "#{country}/#{city}/#{id}"
  
  hostel = Hostelify.new

  if options[:date]
    options = @default_options.merge(options)
    data = setSearch_id(url,options[:date],options[:no_days])
  else
    Retryable.try 3 do
      data = Hpricot(open(url))
    end
  end
  
  hostel.hostel_id = id
  hostel.name = data.at("h1").inner_text
  hostel.address = data.at("p.address").inner_text
  hostel.description = data.at('div[@id="overviewPane"]').inner_text
  facilities_td = data.at("table.tableFacilities")
  
  facilities = []
  (facilities_td/"td").each do |row|
     facilities << row.inner_text
  end
  hostel.facilities = facilities
  extras = []
  extras_td = data.at("table.tableFeatures")
  (extras_td/"td.name").each do |row|
    extras << "Free " + row.inner_text.to_s
  end
  facilities = facilities + extras
  
  ratings = []
  ratings_td = data.at('div[@id="overviewIndRtng"]/table')
  
  (ratings_td/"tr").each do |row|
    ratings << row.at("td").inner_text.to_s.to_f
  end
  
  hostel.ratings = ratings
  images = []
  image = data.at('div[@id="propMedia"]/table')
  (image/"td").each do |row|
    img = row.at("img")['onclick']
    if img =~ /(http).*(jpg|gif|png|jpeg)/
      images << img.match(/(http).*(jpg|gif|png|jpeg)/)[0]
    else
      #add youtube?
    end
  end
  hostel.images = images
  
  if options[:all]
    data = Hpricot(open(url + "/map"))
    data.search("h2").remove #get rid of header
    hostel.directions = data.at('div[@id="directions"]').inner_text
    hostel.geo = data.to_s.scan(/-{0,1}\d{1,3}\.\d{7}/).uniq!
  end
  
  if options[:date]
    date = Date.strptime(options[:date])
    @availables = []
    available = data.at("div.tableAvailability/table")
    if available
      (available/"tr").each do |row|
        name = row.at("td.roomType/label/text()")
        people = row.at("td.people/select")
        people = people.at("option:last-child").inner_text unless people.nil?
        price = row.at("td.price")
        price = price.inner_text.to_s.match(/[\d.]{1,5}/)[0] unless price.nil?
        (0..(options[:no_days].to_i-1)).each do |x|
          #@availables << { :name => name, :spots => people, :price => price, :bookdate => (date+x).to_s } unless price.nil?
          @availables << HostelifyAvailable.new(name,price,people,(date+x).to_s) unless price.nil?
        end
      end
    end
    hostel.availability = @availables
  end
  
  return hostel
end

.find_hostels_by_location(options) ⇒ Object

location



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
44
# File 'lib/hostelify/hostelbookers.rb', line 10

def self.find_hostels_by_location(options) #location
  city = options[:location].split(',').first.rstrip.lstrip.gsub(' ','-').squeeze("-")
  country = options[:location].split(',').last.rstrip.lstrip.gsub(' ','-').squeeze("-")

  url = HB_PLURAL_HOSTELS_URL + "#{country}/#{city}"
  
  if options[:date]
    options = @default_options.merge(options)
    date = Date.strptime(options[:date])
    data = setSearch(url,options[:date],options[:no_days])
  else
    Retryable.try 3 do
      data = Hpricot(open(url))
    end
  end
  
  data = data.search("//div[@id='propertyResultsList']")
  #@results = []
  @results = HostelifyCollection.new
    #coder = HTMLEntities.new
  (data/"tr.propertyRow").each do |row|
    name = row.at("a.propertyTitle").inner_text
    url = row.at("a.propertyTitle")['href']
    desc = row.at("p.shortDescription").inner_text
    rating = row.at("td.rating/text()")
    rating = rating.to_s.to_i unless rating.nil?
    dorm = row.at("td.shared/text()")
    single = row.at("td.private/text()")
    hb_id = url.match(/[\d]{2,5}.$/).to_s.to_i
    
    #@results << Hostelify.new(:hostel_id => hb_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single)
    @results << Hostelify.new(:hostel_id => hb_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single)
  end    
  return @results
end

.setSearch(url, date, no_days) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/hostelify/hostelbookers.rb', line 131

def self.setSearch(url,date,no_days)
  date = Date.strptime(date).strftime("%d/%m/%Y")
  agent = WWW::Mechanize.new
  page = agent.get(url)
  form = page.form_with(:name => 'searchForm') # => WWW::Mechanize::Form
  form.field_with(:name => 'intnights').options[no_days.to_i-1].select
  form.dtearrival = date #d/m/y
  
  Retryable.try 3 do
    page = agent.submit(form)
  end
  
  #to dollars!
  form = page.forms[0]
  form.field_with(:name => 'strSelectedCurrencyCode').options[5].select
  
  Retryable.try 3 do
    page = agent.submit(form)
  end
  
  data = page.search('//div[@id="content"]')

  return data
end

.setSearch_id(url, date, no_days) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/hostelify/hostelbookers.rb', line 156

def self.setSearch_id(url,date,no_days)
  date = Date.strptime(date).strftime("%d/%m/%Y")
  agent = WWW::Mechanize.new
  page = agent.get(url)
  form = page.form_with(:name => 'frmCheckAvailBook') # => WWW::Mechanize::Form
  form.field_with(:name => 'intNights').options[no_days.to_i-1].select
  form.dteArrival = date #d/m/y
  
  Retryable.try 3 do
    page = agent.submit(form)
  end
  #change currency to dollars
  form = page.forms[1]
  #puts form.name
  form.field_with(:name => 'strSelectedCurrencyCode').options[5].select
  
  Retryable.try 3 do
    page = agent.submit(form)
  end
  data = page.search('//div[@id="content"]')

  return data
end