<commit_before>module Wechat
  class AccessToken
    attr_reader :client, :appid, :secret, :token_file, :token_data

    def initialize(client, appid, secret, token_file)
      @appid = appid
      @secret = secret
      @client = client
      @token_file = token_file
    end

    def token
      begin
        @token_data ||= read_token
      rescue 
        self.refresh
      end
      return valid_token(@token_data)
    end

    def refresh
      data = client.get("token", params:{grant_type: "client_credential", appid: appid, secret: secret})
      File.open(token_file, 'w'){|f| f.write(data.merge(expires_at: expired_at).to_json)} if valid_token(data)
      return @token_data = data
    end

    def read_token
      token_data = JSON.parse(file.read(token_file))
      return self.refresh if expire? token_data
      return token_data  
    end  

    private 
    def valid_token token_data
      access_token = token_data["access_token"]
      raise "Response didn't have access_token" if  access_token.blank?
      return access_token
    end

    def expired_at 
      (Time.now.to_i + 7200) - 10
    end  

    def expire? token_data
      Time.now.to_i >= token_data["expired_at"].to_i
    end  

  end
end<commit_msg>个性获取token的问题<commit_after>module Wechat
  class AccessToken
    attr_reader :client, :appid, :secret, :token_file, :token_data

    def initialize(client, appid, secret, token_file)
      @appid = appid
      @secret = secret
      @client = client
      @token_file = token_file
    end

    def token
      begin
        @token_data ||= read_token
       rescue 
         self.refresh
      end
      return valid_token(@token_data)
    end

    def refresh
      data = client.get("token", params:{grant_type: "client_credential", appid: appid, secret: secret})
      File.open(token_file, 'w'){|f| f.write(data.merge(expires_at: expired_at).to_json)} if valid_token(data)
      return @token_data = data
    end

    def read_token
      token_data = JSON.parse(File.read(token_file))
      return self.refresh if expire? token_data
      return token_data  
    end  

    private 
    def valid_token token_data
      access_token = token_data["access_token"]
      raise "Response didn't have access_token" if  access_token.blank?
      return access_token
    end

    def expired_at 
      (Time.now.to_i + 7200) - 10
    end  

    def expire? token_data
      Time.now.to_i >= token_data["expires_at"].to_i
    end  

  end
end
{'fraction_non_alphanumeric': 0.08646464646464647, 'fraction_numerical': 0.0048484848484848485, 'mean_word_length': 2.604075691411936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893416', 'n_tokens_mistral': 835, 'n_tokens_neox': 795, 'n_words': 193}