class ESpider::Front::Qunar::Rank

Public Instance Methods

load_price_ajax() click to toggle source
# 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
otas() click to toggle source
# 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) click to toggle source
# File lib/espider/front/qunar/rank.rb, line 54
def parse_json_from_response_body(res)
        JSON.parse(res.gsub(/[()]/,""))
end
rank(ota, room) click to toggle source
# 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
rooms() click to toggle source
# File lib/espider/front/qunar/rank.rb, line 48
def rooms
        @rooms ||= @ranks.map { |e|  e[3] }.uniq
end
set_cookies() click to toggle source
# 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