Class: Hostelworld
- Inherits:
-
Object
- Object
- Hostelworld
- Defined in:
- lib/hostelify/hostelworld.rb
Constant Summary collapse
- HW_SINGULAR_DETAIL_URL =
constants location list includes/indexjs.js
"http://www.hostelworld.com/hosteldetails.php?HostelNumber="- HW_SINGULAR_IMAGE_URL =
"http://www.hostelworld.com/hostelpictures.php?HostelNumber="- HW_SINGULAR_AVAILABILITY =
"http://www.hostelworld.com/availability.php/"- HW_SINGULAR_YOUTUBE_URL =
"http://www.hostelworld.com/youtubevideo.php?HostelNumber="- HW_PLURAL_HOSTELS_URL =
"http://www.hostelworld.com/findabed.php/"
Class Method Summary collapse
- .find_hostel_by_id(options) ⇒ Object
-
.find_hostels_by_location(options) ⇒ Object
location.
- .parse_html(url) ⇒ Object
Class Method Details
.find_hostel_by_id(options) ⇒ Object
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 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 |
# File 'lib/hostelify/hostelworld.rb', line 22 def self.find_hostel_by_id() opts = { :directions => false, :images => false, :all => false }.merge id = [:id].to_s url = HW_SINGULAR_DETAIL_URL + id #coder = HTMLEntities.new hostel = Hostelify.new hostel.hostel_id = id if [:date] = @default_options.merge() date = Date.strptime([:date]) data = setSearch(url, [:date], [:no_ppl], [:no_days]) else data = parse_html(url) end unless data == "Full" data = data.search("//div[@id='content']") data.search("h3").remove #get rid of header #title, address, desc, facilities, ratings hostel.name = data.at("h2").inner_text.gsub(/( in ).*$/,'') hostel.address = data.at('div[@style="padding-top: 5px"]').inner_text.lstrip if [:date] hostel.availability = parse_availables(data) else hostel.description = data.at('div[@id="microDescription2]').inner_text end #optional no_photos = data.at('div[@id="microPicScroll"]/span/a').inner_text.to_i #no_photos = data.at('span/a[@id="picLink"]').inner_text.to_i video = data.at('div[@id="microVideo"]') facilities = [] (data/"li.microFacilitiesBoomLi").each do |item| facilities << item.inner_text end = [] (data/'div[@id="ratingsBar2"]').each do |item| << item.inner_text.to_i end hostel.facilities = facilities hostel. = if video #exists data = parse_html(HW_SINGULAR_YOUTUBE_URL + id) video_url = data.at('param[@name="movie"]')['value'] hostel.video = video_url #video_url = data.at('tag') end if [:directions] or [:all] data = parse_html(HW_SINGULAR_DETAIL_URL + id + "/directions/") #directions, geo hostel.directions = data.at('div[@id="content"]').inner_text.gsub(/^[\d\D\n]*(DIRECTIONS)/,'') hostel.geo = data.to_s.scan(/-{0,1}\d{1,3}\.\d{7}/).uniq! end if no_photos and ([:images] or [:all]) images = [] (1..no_photos).each do |i| data = parse_html(HW_SINGULAR_IMAGE_URL + id + '&PicNO=' + i.to_s) images << (data/"img").first[:src].to_s end hostel.images = images end else hostel = nil end hostel # return end |
.find_hostels_by_location(options) ⇒ Object
location
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 130 131 132 133 134 135 136 137 138 |
# File 'lib/hostelify/hostelworld.rb', line 100 def self.find_hostels_by_location() #location city = [:location].split(',').first.gsub(' ','') country = [:location].split(',').last.gsub(' ','') url = HW_PLURAL_HOSTELS_URL + "ChosenCity.#{city}/ChosenCountry.#{country}" if [:date] = @default_options.merge() date = Date.strptime([:date]) data = setSearch2(url, [:date], [:no_ppl], [:no_days]) else data = parse_html(url) end data = data.search("//div[@id='content']") @results = HostelifyCollection.new (data/"div.hostelListing").each do |row| name = row.at("h3").inner_text desc = row.at("div.hostelEntry/p").inner_text.to_s.chop.gsub('more info','').squeeze('.') url = row.at("h3/a")['href'] = row.at("h4/text()") = .to_s.to_i unless .nil? type = row.at("div.hostelListingImage/span").inner_text hostel_id = url.match(/[\d]*$/).to_s if [:date] #price_USD = row.at("span.blueBeds").inner_text #need to fix float dorm = (row.at("p.hostelListingRate/span.blueBeds/text()")).to_s.gsub(/[A-Z$]*/,'') single = row.at("p.hostelListingPrivateRate/span.blueBeds/text()").to_s.gsub(/[A-Z$]*/,'') available = row/"ul.hostelListingDates/li.noAvail/text()" available = available.to_a.join(',').split(',') @results << Hostelify.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => , :dorm => dorm, :single => single, :unavailable => available) else @results << Hostelify.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => ) end end return @results end |