Class: Vexapion::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vexapion/connect/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i_sec = 0.5, i_num = 1) ⇒ HTTPClient

Returns a new instance of HTTPClient.



14
15
16
17
18
19
20
21
22
23
# File 'lib/vexapion/connect/http_client.rb', line 14

def initialize(i_sec=0.5, i_num=1)
	@connections = {}
	@timeout = 10
	#以前のリクエストで使ったコネクションの再利用を許可する秒数
	@timeout_keepalive = 300
	@sleep_in_sec  = i_sec
	@min_interval = i_sec
	@num_of_retry = i_num
	@last_access_time = Time.now.to_f
end

Instance Attribute Details

#min_interval=(value) ⇒ Object (writeonly)

Sets the attribute min_interval

Parameters:

  • value

    the value to set the attribute min_interval to.



11
12
13
# File 'lib/vexapion/connect/http_client.rb', line 11

def min_interval=(value)
  @min_interval = value
end

#response_timeObject (readonly)

Returns the value of attribute response_time.



12
13
14
# File 'lib/vexapion/connect/http_client.rb', line 12

def response_time
  @response_time
end

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout

Parameters:

  • value

    the value to set the attribute timeout to.



11
12
13
# File 'lib/vexapion/connect/http_client.rb', line 11

def timeout=(value)
  @timeout = value
end

#timeout_keepalive=(value) ⇒ Object (writeonly)

Sets the attribute timeout_keepalive

Parameters:

  • value

    the value to set the attribute timeout_keepalive to.



11
12
13
# File 'lib/vexapion/connect/http_client.rb', line 11

def timeout_keepalive=(value)
  @timeout_keepalive = value
end

Instance Method Details

#build_http(uri, vmode) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vexapion/connect/http_client.rb', line 45

def build_http(uri, vmode)
	http = Net::HTTP.new(uri.host, uri.port, nil, nil)
	#http.set_debug_output($stderr)
	http.use_ssl = (uri.scheme == 'https')
	http.verify_mode = vmode #if (uri.scheme == 'https') && !vmode.nil?
	#p "set verify_mode #{vmode}" #if (uri.scheme == 'https') && [email protected]?
	http.open_timeout = @timeout
	http.read_timeout = @timeout
	http.keep_alive_timeout = @timeout_keepalive

	http.start
	http
end

#connection(uri, verify_mode) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/vexapion/connect/http_client.rb', line 37

def connection(uri, verify_mode)
	key = uri.host + uri.port.to_s + (uri.scheme == 'https').to_s
	if @connections[key].nil? || @connections[key].started?
		@connections[key] = build_http(uri, verify_mode)
	end
	@connections[key]
end

#disconnectObject



25
26
27
28
29
# File 'lib/vexapion/connect/http_client.rb', line 25

def disconnect
	@connections.values.each do |connection|
		connection.finish
	end
end

#f_minus(a, b) ⇒ Object

of handle_api_error



125
126
127
# File 'lib/vexapion/connect/http_client.rb', line 125

def f_minus(a, b)
	(BigDecimal(a.to_s) - BigDecimal(b.to_s)).to_f
end

#handle_http_error(response) ⇒ Object

of response



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vexapion/connect/http_client.rb', line 103

def handle_http_error(response)
	server_error, client_error = [500, 502, 503, 504], [400, 401, 403, 404]
	http_status_code = response.code.to_i
	message = "#{response.message} #{response.body}"

	#2.server error
	case http_status_code
	when *server_error
		fail RetryException.new(http_status_code, message)

	#3.client error
	when *client_error
		fail Error.new(http_status_code, message)
	
	#4. other
	else
		#puts "http_client.rb: Error: #{http_status_code} #{message}"
		fail Fatal.new(http_status_code, message)

	end #of case
end

#http_request(uri, request, verify_mode = nil) ⇒ String

HTTPのリクエスト

Parameters:

  • uri (String)

    uri

  • request (String)

    request

  • verify_mode (Integer) (defaults to: nil)

    OpenSSL::SSL::VERIFY_*を指定する

Returns:

  • (String)

    request.bodyを返す

Raises:

  • SocketError ソケットエラー

  • RetryException リクエストが成功したか確認できない時 408, 500, 503

  • Warning 何かおかしい時 (200&&response.body==nil), 509

  • Error クライアントのエラー 400, 401, 403, 404

  • Fatal APIラッパーの修正が必要になると思われるエラー



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vexapion/connect/http_client.rb', line 69

def http_request(uri, request, verify_mode=nil)
	#最低接続間隔の保証
	now = Time.now.to_f
	elapse = f_minus(now, @last_access_time)
	if elapse < @min_interval
		s = f_minus(@min_interval, elapse)
		sleep s
	end
	@last_access_time = Time.now.to_f
	#puts (f_minus(@last_access_time, now) + elapse).round(4)*1000
	
	response = nil
	begin
		t1 = Time.now.to_f
		http = connection(uri, verify_mode)
		response = http.request(request)
		t2 = Time.now.to_f
		@response_time = f_minus(t2, t1).round(4)*1000
		#STDERR.puts "\nAPI response time: #{@response_time}ms"

	rescue SocketError, Net::OpenTimeout => e
		fail RetryException.new('0', e.message)

	rescue Net::ReadTimeout => e
		http_status_code = 408
		#message = "Timeout"
		fail RetryException.new(http_status_code, e.message)
	end
	
	handle_http_error(response) if response.code.to_i != 200

	return response.body
end

#terminateObject



31
32
33
34
35
# File 'lib/vexapion/connect/http_client.rb', line 31

def terminate
	@connections.values.each do |connection|
		connection = nil
	end
end