module WeixinAuthorize

def api_endpoint

def api_endpoint
  "https://api.weixin.qq.com"
end

def configure

def configure
  yield self.config ||= Config.new
end

def endpoint_url(endpoint, url)

def endpoint_url(endpoint, url)
  send("#{endpoint}_endpoint") + url
end

def file_endpoint

def file_endpoint
  "http://file.api.weixin.qq.com/cgi-bin"
end

def http_get_without_token(url, headers={}, endpoint="plain")

def http_get_without_token(url, headers={}, endpoint="plain")
  get_api_url = endpoint_url(endpoint, url)
  load_json(resource(get_api_url).get(params: headers))
end

def http_post_without_token(url, payload={}, headers={}, endpoint="plain")

def http_post_without_token(url, payload={}, headers={}, endpoint="plain")
  post_api_url = endpoint_url(endpoint, url)
  payload = JSON.dump(payload) if endpoint == "plain" # to json if invoke "plain"
  load_json(resource(post_api_url).post(payload, params: headers))
end

def load_json(string)

return hash
def load_json(string)
  result_hash = JSON.parse(string)
  code   = result_hash.delete("errcode")
  en_msg = result_hash.delete("errmsg")
  ResultHandler.new(code, en_msg, result_hash)
end

def mp_endpoint(url)

def mp_endpoint(url)
  "https://mp.weixin.qq.com/cgi-bin#{url}"
end

def open_endpoint(url)

def open_endpoint(url)
  "https://open.weixin.qq.com#{url}"
end

def plain_endpoint

def plain_endpoint
  "#{api_endpoint}/cgi-bin"
end

def resource(url)

def resource(url)
  RestClient::Resource.new(url, rest_client_options)
end

def rest_client_options

key 必须是符号
可选配置: RestClient timeout, etc.
def rest_client_options
  if config.nil?
    return {timeout: 5, open_timeout: 5, verify_ssl: true}
  end
  config.rest_client_options
end

def weixin_redis

def weixin_redis
  return nil if config.nil?
  @redis ||= config.redis
end