<commit_before>require 'httparty'

module Stockfighter
  class GM
    GM_URL = "https://www.stockfighter.io/gm"

    attr_accessor :instance_id

    def initialize(key:, level:)
      @api_key = key
      @level = level

      resp = HTTParty.post("#{GM_URL}/levels/#{level}", :headers => {"X-Starfighter-Authorization" => @api_key})

      update_config(resp)
    end

    def config
      if @config[:account] && @config[:venue] && @config[:symbol]
        @config
      else
        nil
      end
    end

    def restart
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/restart", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def stop
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/stop", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def resume
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/resume", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def active?
      response = HTTParty.get("#{GM_URL}/instances/#{@instance_id}", :headers => {"X-Starfighter-Authorization" => @api_key})
      response["done"] && response["ok"]
    end

    def update_config(resp)
      @config = {}
      @config[:key] = @api_key
      @config[:account] = resp["account"]
      @config[:venue] = resp["venues"][0]
      @config[:symbol] = resp["tickers"][0]

      @instance_id = resp["instanceId"]
    end
    private :update_config
  end
end<commit_msg>Simple error handling for gm<commit_after>require 'httparty'

module Stockfighter
  class GM
    GM_URL = "https://www.stockfighter.io/gm"

    attr_accessor :instance_id

    def initialize(key:, level:)
      @api_key = key
      @level = level

      resp = HTTParty.post("#{GM_URL}/levels/#{level}", :headers => {"X-Starfighter-Authorization" => @api_key})

      update_config(resp)
    end

    def config
      if @config[:account] && @config[:venue] && @config[:symbol]
        @config
      else
        nil
      end
    end

    def restart
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/restart", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def stop
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/stop", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def resume
      if @instance_id
        resp = HTTParty.post("#{GM_URL}/instances/#{@instance_id}/resume", :headers => {"X-Starfighter-Authorization" => @api_key})
        update_config(resp)
      end
    end

    def active?
      response = HTTParty.get("#{GM_URL}/instances/#{@instance_id}", :headers => {"X-Starfighter-Authorization" => @api_key})
      response["done"] && response["ok"]
    end

    def update_config(resp)

      if resp["ok"]
        @config = {}
        @config[:key] = @api_key
        @config[:account] = resp["account"]
        @config[:venue] = resp["venues"][0]
        @config[:symbol] = resp["tickers"][0]

        @instance_id = resp["instanceId"]
      else 
        raise "Error response received from #{GM_URL}: #{resp['error']}"
      end
    end
    private :update_config
  end
end
{'fraction_non_alphanumeric': 0.15306418820795817, 'fraction_numerical': 0.0011617775196049957, 'mean_word_length': 2.7353579175704987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3181846', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1113, 'n_words': 212}