<commit_before># coding: utf-8
#
# Gist backend for Boom.
begin
  require "httparty"
rescue LoadError
  puts "The Gist backend requires HTTParty: gem install httpparty"
  exit
end

# Crack's parsing is no bueno. Use the stdlib instead.
class JsonParser < HTTParty::JsonParser
  def json
    ::JSON.parse(body)
  end
end

module Boom
  module Storage
    class Gist < Base
      include HTTParty
      parser JsonParser

      def bootstrap
        unless Boom.config.attributes['gist']
          puts "A gist key must be defined in ~/.boom.conf"
          exit
        end

        @username = Boom.config.attributes['gist']['username']
        @password = Boom.config.attributes['gist']['password']
        @gist_id = Boom.config.attributes['gist']['gist_id']

        unless @username and @password
          puts "GitHub username and password must be defined in ~/.boom.conf"
          exit
        end

        params = {
          :basic_auth => {
            :username => @username,
            :password => <PASSWORD>
          }
        }

        if @gist_id
          response = self.class.get("https://api.github.com/gist/#{@gist_id}", params)
        else
          params.merge!({
            :body => JSON.generate({
              :description => "Data for Boom",
              :public => false,
              :files => {
                "boom.json" => {
                  "content" => '{"lists":[]}'
                }
              }
            })
          })

          response = self.class.post("https://api.github.com/gists", params)
        end

        @storage = JSON.parse(response['files']['boom.json']['content']) if response['files'] and response['files']['boom.json']
        Boom.config.attributes['gist']['gist_id'] = @gist_id = response['id']
        Boom.config.save

        unless @storage
          puts "No Boom data could be found in this Gist"
          exit
        end
      end

      def populate
        @storage['lists'].each do |lists|
          lists.each do |list_name, items|
            @lists << list = List.new(list_name)

            items.each do |item|
              item.each do |name, value|
                list.add_item(Item.new(name, value))
              end
            end
          end
        end
      end

      def save
        params = {
          :basic_auth => {
            :username => @username,
            :password => <PASSWORD>
          },
          :body => JSON.generate({
            :description => "Data for Boom",
            :public => false,
            :files => {
              "boom.json" => {
                "content" => JSON.generate(to_hash)
              }
            }
          })
        }

        response = self.class.post("https://api.github.com/gist/#{@gist_id}", params)
      end
    end
  end<commit_msg>Move bootstap logic into private methods<commit_after># coding: utf-8
#
# Gist backend for Boom.
begin
  require "httparty"
rescue LoadError
  puts "The Gist backend requires HTTParty: gem install httpparty"
  exit
end

# Crack's parsing is no bueno. Use the stdlib instead.
class JsonParser < HTTParty::JsonParser
  def json
    ::JSON.parse(body)
  end
end

module Boom
  module Storage
    class Gist < Base
      include HTTParty
      parser JsonParser
      base_uri "https://api.github.com"

      def bootstrap
        unless Boom.config.attributes['gist']
          puts 'A "gist" data structure must be defined in ~/.boom.conf'
          exit
        end

        set_up_auth
        find_or_create_gist
      end

      def populate
        @storage['lists'].each do |lists|
          lists.each do |list_name, items|
            @lists << list = List.new(list_name)

            items.each do |item|
              item.each do |name, value|
                list.add_item(Item.new(name, value))
              end
            end
          end
        end
      end

      def save
        self.class.post("/gist/#{@gist_id}", request_params)
      end

      private

      def set_up_auth
        username, password = Boom.config.attributes['gist']['username'], Boom.config.attributes['gist']['password']

        if username and password
          self.class.basic_auth(username, password)
        else
          puts "GitHub username and password must be defined in ~/.boom.conf"
          exit
        end
      end

      def find_or_create_gist
        @gist_id = Boom.config.attributes['gist']['gist_id']
        @public = Boom.config.attributes['gist']['public'] == true

        if @gist_id.nil? or @gist_id.empty?
          response = self.class.post('/gists', request_params)
        else
          response = self.class.get("/gist/#{@gist_id}", request_params)
        end

        @storage = JSON.parse(response['files']['boom.json']['content']) if response['files'] and response['files']['boom.json']

        unless @storage
          puts "Boom data could not be obtained"
          exit
        end

        unless @gist_id
          Boom.config.attributes['gist']['gist_id'] = @gist_id = response['id']
          Boom.config.save
        end
      end

      def request_params
        {
          :body => JSON.generate({
            :description => "Data for Boom",
            :public => @public,
            :files => { "boom.json" => { "content" => JSON.generate(to_hash) } }
          })
        }
      end
    end
  end
{'fraction_non_alphanumeric': 0.11589342276877213, 'fraction_numerical': 0.0003726476616359232, 'mean_word_length': 1.7193515704154, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 4, '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': '1171614', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1544, 'n_words': 389}