<commit_before>require 'httparty'

module Stockfighter
  class Api
    BASE_URL = "https://api.stockfighter.io/ob/api"

    def initialize(key:, account:, symbol:, venue:)
      @api_key = key
      @account = account
      @symbol = symbol
      @venue = venue
    end

    def get_quote
      HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/quote", {"X-Starfighter-Authorization" => @api_key}).parsed_response
    end

    def place_order(price:, quantity:, direction:, order_type:)
      order = {
        "account" => @account,
        "venue" => @venue,
        "symbol" => @symbol,
        "price" => price,
        "qty" => quantity,
        "direction" => direction,
        "orderType" => order_type
      }

      HTTParty.post("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders", body: JSON.dump(order),
      headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
    end

    def cancel_order(order_id)
      HTTParty.delete("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}", headers: {"X-Starfighter-Authorization" => @api_key})
    end

    def order_status(order_id)
      HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}", :headers => {"X-Starfighter-Authorization" => @api_key}).parsed_response
    end

    def order_book
      HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}", headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
    end

    def venue_up?
      response = HTTParty.get("#{BASE_URL}/venues/#{@venue}/heartbeat", headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
      response["ok"]
    end

    def status_all
      HTTParty.get("#{BASE_URL}/venues/#{@venue}/accounts/#{@account}/orders", headers: {"X-Starfighter-Authorization" => @api_key})
    end
  end
end
<commit_msg>Refactor of api.rb to include error handling<commit_after>require 'httparty'

module Stockfighter
  class Api
    BASE_URL = "https://api.stockfighter.io/ob/api"

    def initialize(key:, account:, symbol:, venue:)
      @api_key = key
      @account = account
      @symbol = symbol
      @venue = venue
    end

    def get_quote
      perform_request("get", "#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/quote")
    end

    def place_order(price:, quantity:, direction:, order_type:)
      order = {
        "account" => @account,
        "venue" => @venue,
        "symbol" => @symbol,
        "price" => price,
        "qty" => quantity,
        "direction" => direction,
        "orderType" => order_type
      }
      perform_request("post", "#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders", body: JSON.dump(order))
    end

    def cancel_order(order_id)
      perform_request("delete", "#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}")
    end

    def order_status(order_id)
      perform_request("get", "#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}")
    end

    def order_book
      perform_request("get", "#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}")
    end

    def venue_up?
      response = perform_request("get", "#{BASE_URL}/venues/#{@venue}/heartbeat")
      response["ok"]
    end

    def status_all
      perform_request("get", "#{BASE_URL}/venues/#{@venue}/accounts/#{@account}/orders")
    end

    def perform_request(action, url, body:nil)
      options = {
        :headers => {"X-Starfighter-Authorization" => @api_key}
      }
      if body != nil
        options[:body] = body
      end
      response = HTTParty.method(action).call(url, options)
      if response.code != 200
        raise "HTTP error response received from #{url}: #{response.code}"
      end
      if not response["ok"]
        raise "Error response received from #{url}: #{response['error']}"
      end
      response
    end

    private :perform_request
  end
end

{'fraction_non_alphanumeric': 0.16843467011642949, 'fraction_numerical': 0.0007761966364812419, 'mean_word_length': 3.091005291005291, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '3792430', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1264, 'n_words': 225}