Module: Crown::HTTP

Defined in:
lib/crown/http.rb

Class Method Summary collapse

Class Method Details

.get(session, path, limit = 3) ⇒ Object

——————————————————————- #

get

サーバの状況によって失敗する事があるので,最大で limit  get()
を試す.session には Net::HTTP クラス,またはその派生クラスの
インスタンスを指定する.

——————————————————————- #



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
# File 'lib/crown/http.rb', line 48

def get(session, path, limit = 3)
    n = 0
    begin
        session.start if (!session.active?)
        response = session.get(path)
        while (response.code.to_i == 301)
            session.finish if (session.active?)
            uri = URI.parse(response['Location'])
            session = Net::HTTP.new(uri.host, uri.port, session.proxy_address, session.proxy_port)
            path = String.new(uri.path)
            if (uri.query != nil)
                path.concat("?")
                path.concat(uri.query)
            end
            response = session.get(path)
        end
        return response
    rescue Exception
        session.finish if (session.active?)
        if (n < limit)
            n += 1
            retry
        end
        return nil
    end
end