class WechatClient::Client

Constants

DEFAULT
GETTING_APIS
NEED_AUTO_CACHE
POSTING_APIS

Attributes

error[R]
url[R]

Public Class Methods

new(args) click to toggle source
# 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

Public Instance Methods

get(api_name, params = {}, at = nil, &block) click to toggle source
# 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
options() click to toggle source
# File lib/wechat_client/client.rb, line 82
def options
  OpenStruct.new(@options)
end
post(api_name, post_data = {}, at = nil, &block) click to toggle source
# 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

Private Instance Methods

cache_read(api_name) { |nil| ... } click to toggle source
# File lib/wechat_client/client.rb, line 88
def cache_read api_name
  WechatClient.cache.exists("__wechat_client_#{api_name}__") ? \
    WechatClient.cache.read("__wechat_client_#{api_name}__") : yield(nil)
end
cache_write(api_name, json) click to toggle source
# File lib/wechat_client/client.rb, line 93
def cache_write api_name, json
  WechatClient.cache.write("__wechat_client_#{api_name}__", json)
end
request_handle(api_name, params) { |params| ... } click to toggle source
# File lib/wechat_client/client.rb, line 97
def request_handle api_name, params, &block
  if @api.respond_to?(api_name)
    @url = @api.send(api_name)
  else
    raise WechatApiNotFoundError, api_name
  end

  return cache_read(api_name, &block) if options.auto_cache && NEED_AUTO_CACHE.include?(api_name.to_sym)
  yield params
end
response_handle(api_name, response, raise_flag) { |data| ... } click to toggle source
# File lib/wechat_client/client.rb, line 108
def response_handle(api_name, response, raise_flag)
  # 重置 @error
  @error.clear
  content_type = response.respond_to?(:headers) ? response.headers[:content_type] : ""

  # 判断返回类型, 已知 json 和 image 两种
  data = if content_type.include?("json") || content_type.include?("text")
    json = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
    if json["errcode"] && json["errcode"].to_i != 0
      WechatClient.logger.error(json)
      raise(ResponseCodeError, json) if raise_flag
      @error = json
    else
      WechatClient.logger.info("WechatClientLog: api-#{api_name} return #{json}")
      cache_write(api_name, json)if options.auto_cache && NEED_AUTO_CACHE.include?(api_name.to_sym)
    end
    json
  elsif content_type.include? "image"
    tmp = Tempfile.new('tmp')
    tmp.binmode
    tmp.write(response)
    tmp.close
    tmp
  else
    response
  end

  if block_given? && @error.empty?
    yield(data)
  else
    data
  end
end