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.



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

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



61
62
63
64
65
# File 'lib/qunar/hotel.rb', line 61

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

#citynameCnObject



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

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



45
46
47
# File 'lib/qunar/hotel.rb', line 45

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

#coordinateObject



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

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



72
73
74
# File 'lib/qunar/hotel.rb', line 72

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

#idObject



57
58
59
# File 'lib/qunar/hotel.rb', line 57

def id
  @hotelcode = @hotelcode.to_i
end

#nameObject

get the name of the hotel



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

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

开业时间



77
78
79
80
81
# File 'lib/qunar/hotel.rb', line 77

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



67
68
69
# File 'lib/qunar/hotel.rb', line 67

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

#reference_priceObject



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

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



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

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