<commit_before>require 'httparty'

# Handles Facebook authentication
#
# @!visibility private
class FacebookAuthenticator

  class ApiError < StandardError; end

  def initialize(auth_code)
    @auth_code = auth_code
  end

  def authenticate!
    if login.present?
      connect_login_to_fb_account
    else
      create_login_from_fb_account
    end

    login
  end

  private

    def login
      @login ||= Login.where(identification: facebook_user[:email]).first
    end

    def connect_login_to_fb_account
      login.update_attributes!(facebook_uid: facebook_user[:id])
    end

    def create_login_from_fb_account
      login_attributes = {
        identification: facebook_user[:email],
        facebook_uid:   facebook_user[:id]
      }

      @login = Login.create!(login_attributes)
    end

    def facebook_user
      @facebook_user ||= begin
        response = facebook_request(fb_token_url).body
        access_token = response.match(/access_token=([^&]+)/)[1]
        parsed_response = facebook_request(fb_user_url(access_token)).parsed_response
        parsed_response.symbolize_keys
      end
    end

    def facebook_request(url)
      response = HTTParty.get(url)
      raise ApiError.new if response.code != 200
      response<commit_msg>Add logging to facebook API errors<commit_after>require 'httparty'

# Handles Facebook authentication
#
# @!visibility private
class FacebookAuthenticator

  class ApiError < StandardError; end

  def initialize(auth_code)
    @auth_code = auth_code
  end

  def authenticate!
    if login.present?
      connect_login_to_fb_account
    else
      create_login_from_fb_account
    end

    login
  end

  private

    def login
      @login ||= Login.where(identification: facebook_user[:email]).first
    end

    def connect_login_to_fb_account
      login.update_attributes!(facebook_uid: facebook_user[:id])
    end

    def create_login_from_fb_account
      login_attributes = {
        identification: facebook_user[:email],
        facebook_uid:   facebook_user[:id]
      }

      @login = Login.create!(login_attributes)
    end

    def facebook_user
      @facebook_user ||= begin
        response = facebook_request(fb_token_url).body
        access_token = response.match(/access_token=([^&]+)/)[1]
        parsed_response = facebook_request(fb_user_url(access_token)).parsed_response
        parsed_response.symbolize_keys
      end
    end

    def facebook_request(url)
      response = HTTParty.get(url)
      unless response.code == 200
        Rails.logger.warn "Facebook API request failed with status #{response.code}."
        Rails.logger.debug "Facebook API error response was:\n#{response.body}"
        raise ApiError.new
      end
      response
{'fraction_non_alphanumeric': 0.07855316039459262, 'fraction_numerical': 0.0029229082937522835, 'mean_word_length': 3.1049475262368817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4996363', 'n_tokens_mistral': 836, 'n_tokens_neox': 793, 'n_words': 165}