Skip to content

Commit

Permalink
To cache the HTTP object, so make Timeout global effective. #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Aug 23, 2016
1 parent a81edeb commit 791154b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/wechat/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module Wechat
class HttpClient
attr_reader :base, :ssl_context
attr_reader :base, :ssl_context, :httprb

def initialize(base, timeout, skip_verify_ssl)
@base = base
HTTP.timeout(:global, write: timeout, connect: timeout, read: timeout)
@httprb = HTTP.timeout(:global, write: timeout, connect: timeout, read: timeout)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.ssl_version = :TLSv1_client
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if skip_verify_ssl
Expand All @@ -15,21 +15,21 @@ def initialize(base, timeout, skip_verify_ssl)
def get(path, get_header = {})
request(path, get_header) do |url, header|
params = header.delete(:params)
HTTP.headers(header).get(url, params: params, ssl_context: ssl_context)
httprb.headers(header).get(url, params: params, ssl_context: ssl_context)
end
end

def post(path, payload, post_header = {})
request(path, post_header) do |url, header|
params = header.delete(:params)
HTTP.headers(header).post(url, params: params, body: payload, ssl_context: ssl_context)
httprb.headers(header).post(url, params: params, body: payload, ssl_context: ssl_context)
end
end

def post_file(path, file, post_header = {})
request(path, post_header) do |url, header|
params = header.delete(:params)
HTTP.headers(header)
httprb.headers(header)
.post(url, params: params,
form: { media: HTTP::FormData::File.new(file),
hack: 'X' }, # Existing here for http-form_data 1.0.1 handle single param improperly
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/wechat/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

describe '#get' do
specify 'Will use http get method to request data' do
allow(HTTP).to receive_message_chain('headers.get') { response_json }
allow(subject.httprb).to receive_message_chain('headers.get') { response_json }
subject.get('token')
end
end

describe '#post' do
specify 'Will use http post method to request data' do
allow(HTTP).to receive_message_chain('headers.post') { response_json }
allow(subject.httprb).to receive_message_chain('headers.post') { response_json }
subject.post('token', 'some_data')
end
end
Expand Down

0 comments on commit 791154b

Please sign in to comment.