<commit_before>
require 'rest-core'

module RestCore::ClientOauth1
  include RestCore

  def authorize_url!
    set_token(ParseQuery.parse_query(
      post(request_token_path, {}, {}, {:json_decode => false})))

    url(authorize_path, :oauth_token => oauth_token, :format => false)
  end

  def authorize! verifier
    set_token(ParseQuery.parse_query(
      post(access_token_path, {}, {}, {:verifier => verifier,
                                       :json_decode => false})))
  end

  def data_json
    JsonDecode.json_encode(data.merge('sig' => calculate_sig))
  end

  def data_json= json
    self.data = check_sig_and_return_data(JsonDecode.json_decode(json))
  rescue JsonDecode.const_get(:ParseError)
    self.data = nil
  end

  def oauth_token
    data['oauth_token'] if data.kind_of?(Hash)
  end
  def oauth_token= token
    data['oauth_token'] = token if data.kind_of?(Hash)
  end
  def oauth_token_secret
    data['oauth_token_secret'] if data.kind_of?(Hash)
  end
  def oauth_token_secret= secret
    data['oauth_token_secret'] = secret if data.kind_of?(Hash)
  end

  private
  def default_data
    {}
  end

  def set_token query
    self.oauth_token        = query['oauth_token']
    self.oauth_token_secret = query['oauth_token_secret']
  end

  def check_sig_and_return_data hash
    hash if consumer_secret && hash.kind_of?(Hash) &&
            calculate_sig(hash) == hash['sig']
  end

  def calculate_sig hash=data
    base = hash.reject{ |(k, _)| k == 'sig' }.sort.map{ |(k, v)|
      "#{escape(k)}=#{escape(v)}"
    }.join('&')
    Digest::MD5.hexdigest("#{escape(consumer_secret)}&#{base}")
  end

  def escape string
    CGI.escape(string).gsub('+', '%20')
  end
end<commit_msg>client_oauth1.rb: always use data for accessing tokens<commit_after>
require 'rest-core'

module RestCore::ClientOauth1
  include RestCore

  def authorize_url!
    set_token(ParseQuery.parse_query(
      post(request_token_path, {}, {}, {:json_decode => false})))

    url(authorize_path, :oauth_token => oauth_token, :format => false)
  end

  def authorize! verifier
    set_token(ParseQuery.parse_query(
      post(access_token_path, {}, {}, {:verifier => verifier,
                                       :json_decode => false})))
  end

  def data_json
    JsonDecode.json_encode(data.merge('sig' => calculate_sig))
  end

  def data_json= json
    self.data = check_sig_and_return_data(JsonDecode.json_decode(json))
  rescue JsonDecode.const_get(:ParseError)
    self.data = nil
  end

  def oauth_token
    data['oauth_token'] if data.kind_of?(Hash)
  end
  def oauth_token= token
    data['oauth_token'] = token if data.kind_of?(Hash)
  end
  def oauth_token_secret
    data['oauth_token_secret'] if data.kind_of?(Hash)
  end
  def oauth_token_secret= secret
    data['oauth_token_secret'] = secret if data.kind_of?(Hash)
  end

  private
  def default_data
    {}
  end

  def set_token query
    self.data = query
  end

  def check_sig_and_return_data hash
    hash if consumer_secret && hash.kind_of?(Hash) &&
            calculate_sig(hash) == hash['sig']
  end

  def calculate_sig hash=data
    base = hash.reject{ |(k, _)| k == 'sig' }.sort.map{ |(k, v)|
      "#{escape(k)}=#{escape(v)}"
    }.join('&')
    Digest::MD5.hexdigest("#{escape(consumer_secret)}&#{base}")
  end

  def escape string
    CGI.escape(string).gsub('+', '%20')
  end
end
{'fraction_non_alphanumeric': 0.13004750593824227, 'fraction_numerical': 0.002672209026128266, 'mean_word_length': 3.4860186418109187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '495956', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1173, 'n_words': 235}