Class: Qunar::Hotel

Inherits:
Object
  • Object
show all
Defined in:
lib/qunar/hotel.rb

Constant Summary collapse

BASEURI =
"http://hotel.qunar.com"

Instance Method Summary collapse

Constructor Details

#initialize(hotelId) ⇒ Hotel

Returns a new instance of Hotel.



6
7
8
9
10
11
12
13
14
15
# File 'lib/qunar/hotel.rb', line 6

def initialize(hotelId)
  #super
  raise "id should be string" if !hotelId.instance_of?(String)
  @HotelSEQ = hotelId             #hotelId is "cityurl_hotelcode"
  @cityurl = hotelId[0..hotelId.rindex("_",-1)-1]
  @hotelcode = hotelId[hotelId.rindex("_",-1)+1..hotelId.length]
  uri = URI(File.join(BASEURI, 'city', @cityurl, "dt-#{@hotelcode}"))
  page = HTTParty.get(uri.to_s)
  @hotel = Nokogiri::HTML page
end

Instance Method Details

#addressObject



63
64
65
66
67
# File 'lib/qunar/hotel.rb', line 63

def address
  @hotel.search("//p[@class='adress']/span").first['title'].strip
  #return "" if address.nil?
  #return address['title'].strip
end

#citynameCnObject



51
52
53
54
55
56
57
# File 'lib/qunar/hotel.rb', line 51

def citynameCn
  match_data = @hotel.to_s.match(/var\s*cityName\s*=\s*\'\p{Han}+\'/u)
  return "" if match_data.nil?
  match_data = match_data[0].match(/\p{Han}+/u)
  return "" if match_data.nil?
  match_data[0].strip
end

#citynameEnObject



47
48
49
# File 'lib/qunar/hotel.rb', line 47

def citynameEn
  @cityurl.sub('_city','')
end

#coordinateObject



93
94
95
96
97
98
99
100
# File 'lib/qunar/hotel.rb', line 93

def coordinate
  xy = @hotel.to_s.match(/hotelPoint=\[(\d+\.\d+),(\d+\.\d+)\];/)
  if xy.nil?
    return [0.0,0.0]
  else
    return [xy[1],xy[2]]
  end
end

#descObject

the description of the hotel



74
75
76
# File 'lib/qunar/hotel.rb', line 74

def desc
  @hotel.search("div[@class='introduce']/p").text
end

#idObject



59
60
61
# File 'lib/qunar/hotel.rb', line 59

def id
  @hotelcode = @hotelcode.to_i
end

#nameObject

get the name of the hotel



17
18
19
20
21
22
23
24
25
# File 'lib/qunar/hotel.rb', line 17

def name          #get the name of the hotel
  name  = @hotel.search("//div[@class='htl-info fr']//span")
  if name.empty?
    name = '该酒店不存在'
  else
    name = name.first.text.strip
  end
  return name
end

#opentimeObject

开业时间



79
80
81
82
83
# File 'lib/qunar/hotel.rb', line 79

def opentime
  #date ||= @hotel.search("p[@class='insttime']/cite").text.sub("开业时间:","")
  #date.strip
  @hotel.search("dl[@class='inform-list']//cite")[-2].text.sub("年开业",'')
end

#phoneObject



69
70
71
# File 'lib/qunar/hotel.rb', line 69

def phone
  @hotel.search("//dl[@class='inform-list']//cite").first.text.sub('电话','').strip
end

#reference_priceObject



85
86
87
88
89
90
91
# File 'lib/qunar/hotel.rb', line 85

def reference_price
  match_data = @hotel.to_s.match(/var\s*miniRetailPrice\s*=\s*\'\d+\';/)
  return 0 if match_data.nil?
  match_data = match_data[0].match(/[\d]+/)
  return 0 if match_data.nil?
  match_data[0].strip.to_i
end

#starObject

get the level of the hotel



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/qunar/hotel.rb', line 27

def star         #get the level of the hotel
  name_tag = @hotel.search('//div[@class="htl-info fr"]//em[@class="dangci"]').first
  return 0 if name_tag.nil?
  if name_tag.text.include?'经济型'
    1
  elsif name_tag.text.include?'舒适型'
    3
  elsif name_tag.text.include?'高档型'
    4
  elsif name_tag.text.include?'豪华型'
    5
  elsif name_tag.text.include?'star20'
    2
  elsif name_tag.text.include?'star50'
    5
  else
    0
  end
end