<commit_before>require 'rails/generators'

class HoptoadGenerator < Rails::Generators::Base

  class_option :api_key, :aliases => "-k", :type => :string, :desc => "Your Hoptoad API key"

  def self.source_root
    @_hoptoad_source_root ||= File.expand_path("../../../../generators/hoptoad/templates", __FILE__)
  end

  def install
    ensure_api_key_was_configured
    generate_initializer
    test_hoptoad
  end

  private

  def ensure_api_key_was_configured
    if !options[:api_key] # && !api_key_configured?
      puts "Must pass --api-key or create config/initializers/hoptoad.rb"
      exit
    end
  end

  def api_key
    options[:api_key]
  end

  def generate_initializer
    # api_key = options[:api_key]
    template 'initializer.rb', 'config/initializers/hoptoad.rb'
  end

  # Justified by scenario:
  #
  # Scenario: Configure the notifier by hand
  #
  # def api_key_configured?
  #   File.exists?('config/initializers/hoptoad.rb') || system("grep HoptoadNotifier config/environment.rb")
  # end<commit_msg>Rails 3 generator works with an existing api key<commit_after>require 'rails/generators'

class HoptoadGenerator < Rails::Generators::Base

  class_option :api_key, :aliases => "-k", :type => :string, :desc => "Your Hoptoad API key"

  def self.source_root
    @_hoptoad_source_root ||= File.expand_path("../../../../generators/hoptoad/templates", __FILE__)
  end

  def install
    ensure_api_key_was_configured
    generate_initializer unless api_key_configured?
    test_hoptoad
  end

  private

  def ensure_api_key_was_configured
    if !options[:api_key] && !api_key_configured?
      puts "Must pass --api-key or create config/initializers/hoptoad.rb"
      exit
    end
  end

  def api_key
    options[:api_key]
  end

  def generate_initializer
    template 'initializer.rb', 'config/initializers/hoptoad.rb'
  end

  def api_key_configured?
    File.exists?('config/initializers/hoptoad.rb')
  end
{'fraction_non_alphanumeric': 0.10858324715615306, 'fraction_numerical': 0.000517063081695967, 'mean_word_length': 3.7195121951219514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3826962', 'n_tokens_mistral': 670, 'n_tokens_neox': 650, 'n_words': 139}