<commit_before>class ApiCalls
  def initialize(client_id, client_secret)
    @client_id = client_id
    @client_secret = client_secret
  end

  def user_agent
    'ruby:lita-reddit:v0.0.7 (by /u/dosman711)'
  end

  def update_token(request)
    request.basic_auth(@client_id, @client_secret)
    auth_response = request.post do |req|
      req.url 'https://www.reddit.com/api/v1/access_token'
      req.headers['User-Agent'] = user_agent
      req.body = 'grant_type=client_credentials'
    end
    response = MultiJson.load(auth_response.body)
    @access_token = response['access_token']
  end

  def get_posts(request, subreddit, redis_key)
    resp = request.get do |req|
      req.url 'https://oauth.reddit.com/r/%s/new.json' % [subreddit]
      req.headers['Authorization'] = 'bearer %s' % [@access_token]
      req.headers['User-Agent'] = user_agent
      req.headers['Content-Type'] = 'Application/json'
      req.params['limit'] = 3
      req.params['before'] = redis_key
    end
    response_body = MultiJson.load(resp.body)
    results = response_body['data']['children']
    results.collect {|r| 
      {
        id: r['data']['id'], 
        subreddit: r['data']['subreddit'], 
        title: r['data']['title']
      }}
  end
end
<commit_msg>adding basic error handling to API calls<commit_after>class ApiCalls
  def initialize(client_id, client_secret)
    @client_id = client_id
    @client_secret = client_secret
  end

  def user_agent
    'ruby:lita-reddit:v0.0.7 (by /u/dosman711)'
  end

  def update_token(request) 
    request.basic_auth(@client_id, @client_secret)
    auth_response = request.post do |req|
      req.url 'https://www.reddit.com/api/v1/access_token'
      req.headers['User-Agent'] = user_agent
      req.body = 'grant_type=client_credentials'
    end
    response = MultiJson.load(auth_response.body)
    @access_token = response['access_token']
  rescue
   nil 
  end

  def get_posts(request, subreddit, redis_key)
    resp = request.get do |req|
      req.url 'https://oauth.reddit.com/r/%s/new.json' % [subreddit]
      req.headers['Authorization'] = 'bearer %s' % [@access_token]
      req.headers['User-Agent'] = user_agent
      req.headers['Content-Type'] = 'Application/json'
      req.params['limit'] = 3
      req.params['before'] = redis_key
    end
    response_body = MultiJson.load(resp.body)
    results = response_body['data']['children']
    results.collect {|r| 
      {
        id: r['data']['id'], 
        subreddit: r['data']['subreddit'], 
        title: r['data']['title']
      }}
  rescue
    return []
  end
end

{'fraction_non_alphanumeric': 0.14252517428350117, 'fraction_numerical': 0.006196746707978312, 'mean_word_length': 3.1594202898550723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '3168808', 'n_tokens_mistral': 890, 'n_tokens_neox': 881, 'n_words': 158}