Class: InternetHakai::HttpClient
- Inherits:
-
Object
- Object
- InternetHakai::HttpClient
- Includes:
- HttpClientIf
- Defined in:
- lib/internethakai/http_client.rb
Instance Attribute Summary collapse
-
#busy ⇒ Object
readonly
Returns the value of attribute busy.
-
#requestURL ⇒ Object
readonly
Returns the value of attribute requestURL.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #encode_cookie(req) ⇒ Object
-
#free ⇒ Object
コンストラクタ/デストラクタヘルパ.
- #has_callback ⇒ Object
-
#initialize(host, port) ⇒ HttpClient
constructor
コンストラクタ.
- #request_send(method, path, body = nil) ⇒ Object
- #set_callback(on_success, on_error) ⇒ Object
- #set_cookie(cookie) ⇒ Object
- #set_header(key, value) ⇒ Object
- #set_headers(header) ⇒ Object
Methods included from HttpClientIf
Constructor Details
#initialize(host, port) ⇒ HttpClient
コンストラクタ
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/internethakai/http_client.rb', line 23 def initialize(host, port) @host = host @port = port @on_error = nil @on_success = nil free() @header = Hash::new Net::HTTP.version_1_2 if ENV["HTTP_PROXY"] @proxy = ENV["HTTP_PROXY"].split(':') end end |
Instance Attribute Details
#busy ⇒ Object (readonly)
Returns the value of attribute busy.
16 17 18 |
# File 'lib/internethakai/http_client.rb', line 16 def busy @busy end |
#requestURL ⇒ Object (readonly)
Returns the value of attribute requestURL.
16 17 18 |
# File 'lib/internethakai/http_client.rb', line 16 def requestURL @requestURL end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
16 17 18 |
# File 'lib/internethakai/http_client.rb', line 16 def response @response end |
#timeout ⇒ Object
Returns the value of attribute timeout.
17 18 19 |
# File 'lib/internethakai/http_client.rb', line 17 def timeout @timeout end |
Class Method Details
.create(host, port) ⇒ Object
18 19 20 21 |
# File 'lib/internethakai/http_client.rb', line 18 def self::create host, port o = self::new(host,port) o end |
Instance Method Details
#encode_cookie(req) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/internethakai/http_client.rb', line 64 def req return unless @cookie req['Cookie'] = @cookie.map{|k,v| "#{k}=#{v}" }.join(';') end |
#free ⇒ Object
コンストラクタ/デストラクタヘルパ
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/internethakai/http_client.rb', line 36 def free() @client = nil @ca_path = nil @requestURL = nil @response = nil @timeout = nil @header = nil @proxy = nil @post_string = nil @response_time = nil @busy = false end |
#has_callback ⇒ Object
57 58 59 |
# File 'lib/internethakai/http_client.rb', line 57 def has_callback !@on_success.nil? and !@on_error.nil? end |
#request_send(method, path, body = nil) ⇒ Object
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/internethakai/http_client.rb', line 70 def request_send method, path, body = nil if method == GET req = Net::HTTP::Get::new(path, @header) elsif method == POST req = Net::HTTP::Post::new(path, @header) if body @post_string = body end else raise 'invalid method' end (req) begin response = nil @busy = true if @proxy Net::HTTP::Proxy(@proxy[0], @proxy[1]).start(@host, @port) do |http| http.read_timeout = @timeout @starttime = Time::now response = http.request(req, @post_string) @response_time = Time::now - @starttime end else Net::HTTP.start(@host, @port) do |http| http.read_timeout = @timeout #puts "send: #{object_id}" @starttime = Time::now response = http.request(req, @post_string) @response_time = Time::now - @starttime end end $REQUEST_COUNT += 1 rescue Exception => err @on_error.call(err) if @on_error @busy = false return end if response.nil? @response = nil else @response = ResponseObject::create_from_nethttp(response) end #puts "starttime: #{@starttime.usec} : #{object_id}" #puts "restime: #{@response_time} : #{object_id}" @response.time = @response_time @on_success.call(@response) if @on_success @busy = false @response end |
#set_callback(on_success, on_error) ⇒ Object
60 61 62 63 |
# File 'lib/internethakai/http_client.rb', line 60 def set_callback on_success, on_error @on_success = on_success @on_error = on_error end |
#set_cookie(cookie) ⇒ Object
51 52 53 |
# File 'lib/internethakai/http_client.rb', line 51 def @cookie = end |
#set_header(key, value) ⇒ Object
54 55 56 |
# File 'lib/internethakai/http_client.rb', line 54 def set_header key, value @header[key] = value end |
#set_headers(header) ⇒ Object
48 49 50 |
# File 'lib/internethakai/http_client.rb', line 48 def set_headers header @header = header end |