Class: Wechat::Client
- Inherits:
-
Object
- Object
- Wechat::Client
- Defined in:
- lib/wechat/client.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Instance Method Summary collapse
- #get(path, header = {}) ⇒ Object
-
#initialize(base, skip_verify_ssl) ⇒ Client
constructor
A new instance of Client.
- #post(path, payload, header = {}) ⇒ Object
- #request(path, header = {}, &block) ⇒ Object
Constructor Details
#initialize(base, skip_verify_ssl) ⇒ Client
7 8 9 10 |
# File 'lib/wechat/client.rb', line 7 def initialize(base, skip_verify_ssl) @base = base @verify_ssl = !skip_verify_ssl end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
5 6 7 |
# File 'lib/wechat/client.rb', line 5 def base @base end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
5 6 7 |
# File 'lib/wechat/client.rb', line 5 def verify_ssl @verify_ssl end |
Instance Method Details
#get(path, header = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/wechat/client.rb', line 12 def get(path, header = {}) request(path, header) do |url, header| if verify_ssl RestClient.get(url, header) else RestClient::Request.execute(url: url, method: :get, headers: header, verify_ssl: OpenSSL::SSL::VERIFY_NONE) end end end |
#post(path, payload, header = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/wechat/client.rb', line 22 def post(path, payload, header = {}) request(path, header) do |url, header| if verify_ssl RestClient.post(url, payload, header) else RestClient::Request.execute(url: url, method: :post, payload: payload, headers: header, verify_ssl: OpenSSL::SSL::VERIFY_NONE) end end end |
#request(path, header = {}, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wechat/client.rb', line 32 def request(path, header = {}, &block) url = "#{header.delete(:base) || base}#{path}" as = header.delete(:as) header.merge!(accept: :json) response = yield(url, header) fail "Request not OK, response code #{response.code}" if response.code != 200 parse_response(response, as || :json) do |parse_as, data| break data unless parse_as == :json && data['errcode'].present? case data['errcode'] when 0 # for request didn't expect results data when 42001, 40014 # 42001: access_token超时, 40014:不合法的access_token fail AccessTokenExpiredError else fail ResponseError.new(data['errcode'], data['errmsg']) end end end |