<commit_before>require 'sent_email_hasher'
require 'member_hasher'

class SignaturesController < ApplicationController
  def create
    petition = Petition.find(params[:petition_id])
    signature = Signature.new(params[:signature])
    signature.ip_address = connecting_ip
    signature.user_agent = request.env["HTTP_USER_AGENT"]
    signature.browser_name = Browser.new(:ua => signature.user_agent).id.to_s
    signature.member = Member.find_or_initialize_by_email(email: signature.email, first_name: signature.first_name, last_name: signature.last_name)
    signature.created_member = signature.member.new_record?
    member_hash = nil
    if signature.valid?
      begin
        petition.signatures.push signature
        petition.save!
        track_referrals petition, signature
        signature.save!

        begin
          Resque.enqueue(SignedPetitionEmailJob, signature.id)
        rescue => ex
          Rails.logger.error "Error queueing email on Resque: #{ex} #{ex.backtrace.join}"
          Notifications.signed_petition Signature.find(signature.id)
        end

        nps_win signature
        win! :signature
        member_hash = signature.member.to_hash
        cookies[:member_id] = { :value => member_hash, :expires => 100.years.from_now }
        flash[:signature_id] = signature.id
      rescue => ex
        Rails.logger.error "Error saving signature: #{ex} #{ex.backtrace.join}"
        flash.notice = ex.message
      end
    else
      flash[:invalid_signature] = signature
    end
    redirect_to petition_url(petition, l: member_hash)
  end

  private

  def track_referrals petition, signature
    track_regular_referral(petition, signature) || track_facebook_referral(petition, signature)
  end

  def track_regular_referral petition, signature
    ref_types.keys.find do |key| 
      (key == :email_hash) ? <commit_msg>forcing a change<commit_after>require 'sent_email_hasher'
require 'member_hasher'

class SignaturesController < ApplicationController
  def create
    petition = Petition.find(params[:petition_id])
    signature = Signature.new(params[:signature])
    signature.ip_address = connecting_ip
    signature.user_agent = request.env["HTTP_USER_AGENT"]
    signature.browser_name = Browser.new(:ua => signature.user_agent).id.to_s
    signature.member = Member.find_or_initialize_by_email(email: signature.email, first_name: signature.first_name, last_name: signature.last_name)
    signature.created_member = signature.member.new_record?
    member_hash = nil
    if signature.valid?
      begin
        petition.signatures.push signature
        petition.save!
        track_referrals petition, signature
        signature.save!

        begin
          Resque.enqueue(SignedPetitionEmailJob, signature.id)
        rescue => ex
          Rails.logger.error "Error queueing email on Resque: #{ex} #{ex.backtrace.join}"
          Notifications.signed_petition Signature.find(signature.id)
        end

        nps_win signature
        win! :signature
        member_hash = signature.member.to_hash
        cookies[:member_id] = { :value => member_hash, :expires => 100.years.from_now }
        flash[:signature_id] = signature.id
      rescue => ex
        Rails.logger.error "Error saving signature: #{ex} #{ex.backtrace.join}"
        flash.notice = ex.message
      end
    else
      flash[:invalid_signature] = signature
    end
    redirect_to petition_url(petition, l: member_hash)
  end

  
  private

  def track_referrals petition, signature
    track_regular_referral(petition, signature) || track_facebook_referral(petition, signature)
  end

  def track_regular_referral petition, signature
    ref_types.keys.find do |key| 
      (key == :email_hash) ? 
{'fraction_non_alphanumeric': 0.08929532006455083, 'fraction_numerical': 0.0016137708445400753, 'mean_word_length': 3.3446261682242993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5203526', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1051, 'n_words': 220}