<commit_before>require "thor"
require "thor/util"
require "fileutils"
require_relative "hoboken/version"
require_relative "hoboken/generate"
require_relative "hoboken/actions"

module Hoboken
  class OmniAuth < Thor::Group
    include Thor::Actions
    include Hoboken::Actions

    attr_reader :provider

    def self.source_root
      File.dirname(__FILE__)
    end

    def add_gem
      @provider = ask("Specify a provider (i.e. twitter, facebook. etc.): ").downcase
      provider_version = ask("Specify provider version: ")
      gem gem_name, provider_version
    end

    def setup_middleware
      insert_into_file("app.rb", after: /require "sinatra("|\/base")/) do
        "\nrequire \"#{gem_name}\""
      end

      if modular?
        insert_into_file("app.rb", after: /use Rack::Session::Cookie.+\n/) do
<<-CODE

    use OmniAuth::Builder do
      provider :#{provider}, ENV["#{provider.upcase}_KEY"], ENV["#{provider.upcase}_SECRET"]
    end

CODE
        end
      else
        insert_into_file("app.rb", after: /use Rack::Session::Cookie.+\n/) do
<<-CODE

use OmniAuth::Builder do
  provider :#{provider}, ENV["#{provider.upcase}_KEY"], ENV["#{provider.upcase}_SECRET"]
end

CODE
        end
      end
    end

    def add_routes
      routes = <<-CODE

get "/login" do
  '<a href="/auth/#{provider}">Login</a>'
end

get "/auth/:provider/callback" do
  # TODO: Insert real authentication logic...
  MultiJson.encode(request.env['omniauth.auth'])
end

get "/auth/failure" do
  # TODO: Insert real error handling logic...
  halt 401, params[:message]
end
CODE

      if modular?
        insert_into_file("app.rb", after: /get.+?end$/m) do
<<-CODE


    get "/login" do
      '<a href="/auth/#{provider}">Login</a>'
    end

    get "/auth/:provider/callback" do
      # TODO: Insert real authentication logic...
      MultiJson.encode(request.env['omniauth.auth'])
    end

    get "/auth/failure" do
      # TODO: Insert real error handling logic...
      halt 401, params[:message]
    end
CODE
        end
      else
        append_file("app.rb", routes)
      end
    end

    def add_tests
      inject_into_class("test/unit/app_test.rb", "AppTest") do
<<-CODE
  setup do
    OmniAuth.config.test_mode = true
  end

  test "GET /login" do
    get "/login"
    assert_equal('<a href="/auth/#{provider}">Login</a>', last_response.body)
  end

  test "GET /auth/#{provider}/callback" do
    auth_hash = {
      "provider" => "#{provider}",
      "uid" => "123545",
      "info" => {
        "name" => "<NAME>"<commit_msg>Clean up OmniAuth AddOn and use indent action helper.<commit_after>require "thor"
require "thor/util"
require "fileutils"
require_relative "hoboken/version"
require_relative "hoboken/generate"
require_relative "hoboken/actions"

module Hoboken
  class OmniAuth < Thor::Group
    include Thor::Actions
    include Hoboken::Actions

    attr_reader :provider

    def self.source_root
      File.dirname(__FILE__)
    end

    def add_gem
      @provider = ask("Specify a provider (i.e. twitter, facebook. etc.): ").downcase
      provider_version = ask("Specify provider version: ")
      gem gem_name, provider_version
    end

    def setup_middleware
      insert_into_file("app.rb", after: /require "sinatra("|\/base")/) do
        "\nrequire \"#{gem_name}\""
      end

      snippet = <<-CODE

use OmniAuth::Builder do
  provider :#{provider}, ENV["#{provider.upcase}_KEY"], ENV["#{provider.upcase}_SECRET"]
end

CODE

      text = modular? ? indent(snippet, 4) : snippet
      insert_into_file("app.rb", after: /use Rack::Session::Cookie.+\n/) { text }
    end

    def add_routes
      routes = <<-CODE


get "/login" do
  '<a href="/auth/#{provider}">Login</a>'
end

get "/auth/:provider/callback" do
  # TODO: Insert real authentication logic...
  MultiJson.encode(request.env['omniauth.auth'])
end

get "/auth/failure" do
  # TODO: Insert real error handling logic...
  halt 401, params[:message]
end
CODE

      if modular?
        insert_into_file("app.rb", after: /get.+?end$/m) { indent(routes, 4) }
      else
        append_file("app.rb", routes)
      end
    end

    def add_tests
      inject_into_class("test/unit/app_test.rb", "AppTest") do
<<-CODE
  setup do
    OmniAuth.config.test_mode = true
  end

  test "GET /login" do
    get "/login"
    assert_equal('<a href="/auth/#{provider}">Login</a>', last_response.body)
  end

  test "GET /auth/#{provider}/callback" do
    auth_hash = {
      "provider" => "#{provider}",
      "uid" => "123545",
      "info" => {
        "name" => "<NAME>"
{'fraction_non_alphanumeric': 0.14756258234519104, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 2.9301121656600517, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 23, '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': '4283057', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1491, 'n_words': 367}