Class: WechatClient::Client

Inherits:
Object
  • Object
show all
Extended by:
MonitorMixin
Defined in:
lib/wechat_client/client.rb

Constant Summary collapse

DEFAULT =
{
  app_id:          nil,
  app_secret:      nil,
  serial_no:       nil,
  raise_flag:      false,  # 微信返回错误代码时raise
  verify_ssl:      true,   # RestClient::Request.execute 参数
  auto_cache:      true    # 是否使用自带的缓存方式
}
NEED_AUTO_CACHE =
[:access_token, :jsapi_ticket]
GETTING_APIS =
WechatClient::Apis::Getting.public_instance_methods(false)
POSTING_APIS =
WechatClient::Apis::Posting.public_instance_methods(false)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



40
41
42
43
44
# File 'lib/wechat_client/client.rb', line 40

def initialize args
  @options = DEFAULT.merge(args.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
  @api = Api.new(options)
  @error = {}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



27
28
29
# File 'lib/wechat_client/client.rb', line 27

def error
  @error
end

#urlObject (readonly)

Returns the value of attribute url.



27
28
29
# File 'lib/wechat_client/client.rb', line 27

def url
  @url
end

Instance Method Details

#get(api_name, params = {}, at = nil, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/wechat_client/client.rb', line 61

def get(api_name, params = {}, at = nil, &block)
  Client.synchronize do
    @api.access_token = at
    response = request_handle(api_name, params) do |params|
      @url += params.to_query if params
      RestClient::Request.execute(method: :get, url: @url, verify_ssl: options.verify_ssl)
    end
    response_handle(api_name, response, options.raise_flag, &block)
  end
end

#optionsObject



82
83
84
# File 'lib/wechat_client/client.rb', line 82

def options
  OpenStruct.new(@options)
end

#post(api_name, post_data = {}, at = nil, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/wechat_client/client.rb', line 72

def post(api_name, post_data = {}, at = nil, &block)
  Client.synchronize do
    @api.access_token = at
    response = request_handle(api_name, post_data) do |post_data|
      RestClient::Request.execute(method: :post, url: @url, payload: post_data.to_json, verify_ssl: options.verify_ssl)
    end
    response_handle(api_name, response, options.raise_flag, &block)
  end
end