Class: ESpider::Front::Qunar::Rank

Inherits:
Base
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/espider/front/qunar/rank.rb

Constant Summary

Constants inherited from Base

Base::BASEURI

Instance Method Summary collapse

Methods inherited from Base

#initialize, #parse_mixkey

Constructor Details

This class inherits a constructor from ESpider::Front::Qunar::Base

Instance Method Details

#load_price_ajaxObject



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

def load_price_ajax
	mixKey = parse_mixkey
	set_cookies
	raise QunarMixKeyParsedException if mixKey.nil?
	uri = URI(File.join(BASEURI, 'render', 'detailV2.jsp'))
	params = {:fromDate=>Date.today+2, :toDate=>Date.today+3, :cityurl=>@cityurl, :HotelSEQ=>@HotelSEQ, :mixKey=>mixKey}
	headers = Hash.new
	headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0'
	headers['Connection'] = 'keep-alive'
	headers['Accept-Encoding'] = 'gzip, deflate'
	headers['Cookie'] = @cookies.map{|k, v| k+'='+v}.join('; ')
	uri.query = URI.encode_www_form(params)
	res = HTTParty.get(uri.to_s, :headers=>headers)
	json_ajax = parse_json_from_response_body(res.body)
	if !json_ajax['errcode'].nil? and json_ajax['errcode'].eql?110
		raise QunarIpBlockException,json_ajax
	end
	raise QunarIpBlockException,json_ajax if !json_ajax['errcode'].nil? and json_ajax['errcode'].eql?110
	@ranks = json_ajax['result'].values
end

#otasObject



51
52
53
# File 'lib/espider/front/qunar/rank.rb', line 51

def otas
	@otas ||= @ranks.map{|e| e[6] }.uniq
end

#parse_json_from_response_body(res) ⇒ Object



54
55
56
# File 'lib/espider/front/qunar/rank.rb', line 54

def parse_json_from_response_body(res)
	JSON.parse(res.gsub(/[()]/,""))
end

#rank(ota, room) ⇒ Object



44
45
46
47
# File 'lib/espider/front/qunar/rank.rb', line 44

def rank(ota, room)
	@ranks.each { |rank| return rank[15]+1 if rank[3].eql?room and rank[6].eql?ota and rank[9].eql?1 }
	return nil
end

#roomsObject



48
49
50
# File 'lib/espider/front/qunar/rank.rb', line 48

def rooms
	@rooms ||= @ranks.map { |e|  e[3] }.uniq
end

#set_cookiesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/espider/front/qunar/rank.rb', line 10

def set_cookies
	headless = Headless.new
	headless.start
	Capybara.current_driver = :selenium
	Capybara.app_host = BASEURI
	Capybara.default_wait_time = 10
	begin
		visit "/city/#{@cityurl}/dt-#{@hotelcode}/"
	rescue Errno::ECONNREFUSED => e
		raise
	end
	@cookies = {}
	Capybara.current_session.driver.browser.manage.all_cookies.each { |cookie| @cookies[cookie[:name]] = cookie[:value] }
end