Class: Crown::HTTPWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/crown/http-wrapper.rb

Overview

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

HTTPWrapper

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = 80, proxy_addr = nil, proxy_port = nil) ⇒ HTTPWrapper

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

initialize

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



52
53
54
55
56
# File 'lib/crown/http-wrapper.rb', line 52

def initialize(address, port = 80, proxy_addr = nil, proxy_port = nil)
    @http = Net::HTTP.new(address, port, proxy_addr, proxy_port)
    @retry_limit = 3
    @retry_interval = 2.0
end

Instance Attribute Details

#retry_intervalObject

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

accessor

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



47
48
49
# File 'lib/crown/http-wrapper.rb', line 47

def retry_interval
  @retry_interval
end

#retry_limitObject

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

accessor

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



47
48
49
# File 'lib/crown/http-wrapper.rb', line 47

def retry_limit
  @retry_limit
end

Class Method Details

.start(address, port = 80, proxy_addr = nil, proxy_port = nil, &block) ⇒ Object

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

HTTPWrapper.start

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



61
62
63
# File 'lib/crown/http-wrapper.rb', line 61

def HTTPWrapper.start(address, port = 80, proxy_addr = nil, proxy_port = nil, &block)
    return HTTPWrapper.new(address, port, proxy_addr, proxy_port).start(&block)
end

Instance Method Details

#active?Boolean

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

active?

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

Returns:

  • (Boolean)


125
126
127
# File 'lib/crown/http-wrapper.rb', line 125

def active?
    return @http.active?
end

#addressObject

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

address

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



132
133
134
# File 'lib/crown/http-wrapper.rb', line 132

def address
    return @http.address
end

#finishObject

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

finish

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



174
175
176
177
# File 'lib/crown/http-wrapper.rb', line 174

def finish
    @http.finish
    return self
end

#get(path, header = nil) ⇒ Object

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

get

サーバの状況によって失敗する事があるので,最大で retry_limit 回
通信が成功するまで get() を試す.また,3xx 系のうち以下の 3 つの
ステータスコードが返ってきた場合は,ロケーション・フィールド
によって与えられた URI へ再度 GET を試みる.

- 301: Moved Permanently
- 302: Moved Temporarily
- 303: See Other

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/crown/http-wrapper.rb', line 79

def get(path, header = nil)
    n = 0
    begin
        @http.start if (!@http.active?)
        response = @http.get(path, header)
        code = response.code.to_i
        while (code == 301 || code == 302 || code == 303)
            @http.finish if (@http.active?)
            
            # リダイレクト先へ接続
            uri = URI.parse(response['Location'])
            @http = Net::HTTP.new(uri.host, uri.port, @http.proxy_address, @http.proxy_port)
            path = String.new(uri.path)
            path.concat("?").concat(uri.query) if (uri.query != nil)
            
            @http.start
            response = @http.get(path, header)
            code = response.to_i
        end
        return response
    rescue Exception => e
        @http.finish if (@http.active?)
        if (n < @retry_limit)
            sleep(@retry_interval)
            n += 1
            retry
        end
        raise e
    end
end

#open_timeoutObject

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

open_timeout

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



146
147
148
# File 'lib/crown/http-wrapper.rb', line 146

def open_timeout
    return @http.open_timeout
end

#open_timeout=(n) ⇒ Object

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

open_timeout=

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



153
154
155
# File 'lib/crown/http-wrapper.rb', line 153

def open_timeout=(n)
    return @http.open_timeout = n
end

#portObject

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

port

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



139
140
141
# File 'lib/crown/http-wrapper.rb', line 139

def port
    return @http.port
end

#proxy?Boolean

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

proxy?

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

Returns:

  • (Boolean)


182
183
184
# File 'lib/crown/http-wrapper.rb', line 182

def proxy?
    return @http.proxy?
end

#proxy_addressObject

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

proxy_address

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



189
190
191
# File 'lib/crown/http-wrapper.rb', line 189

def proxy_address
    return @http.proxy_address
end

#proxy_portObject

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

proxy_port

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



196
197
198
# File 'lib/crown/http-wrapper.rb', line 196

def proxy_port
    return @http.proxy_port
end

#read_timeoutObject

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

read_timeout

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



160
161
162
# File 'lib/crown/http-wrapper.rb', line 160

def read_timeout
    return @http.read_timeout
end

#read_timeout=(n) ⇒ Object

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

read_timeout=

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



167
168
169
# File 'lib/crown/http-wrapper.rb', line 167

def read_timeout=(n)
    return @http.read_timeout = n
end

#startObject

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

start

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



113
114
115
116
117
118
119
120
# File 'lib/crown/http-wrapper.rb', line 113

def start
    @http.start
    if (block_given?)
        yield self
        @http.finish if (@http.active?)
    end
    return self
end