diff --git a/Gemfile.lock b/Gemfile.lock index c1a8881..8ab36f4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kalshi (0.0.3) + kalshi (0.0.4) dry-cli dry-configurable dry-container @@ -230,7 +230,7 @@ CHECKSUMS httpx (1.7.0) sha256=e219689555951f9c13c40862da120cdd2535e1454189bed589f04d7059557154 io-console (0.8.2) sha256=d6e3ae7a7cc7574f4b8893b4fca2162e57a825b223a177b7afa236c5ef9814cc json (2.18.0) sha256=b10506aee4183f5cf49e0efc48073d7b75843ce3782c68dbeb763351c08fd505 - kalshi (0.0.3) + kalshi (0.0.4) language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87 logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203 diff --git a/lib/kalshi/client.rb b/lib/kalshi/client.rb index 4df5e0a..20ca3a6 100644 --- a/lib/kalshi/client.rb +++ b/lib/kalshi/client.rb @@ -2,6 +2,7 @@ require 'httpx' require 'json' +require 'uri' module Rubyists module Kalshi @@ -19,13 +20,33 @@ def initialize(base_url: Kalshi.config.base_url) .with(origin: base_url) end + # Get response from a path, adding the base_url, + # and a prefix, if set on the client. + # + # see #full_path for details + # + # @param path [String] The URL path + # + # @return [Hash] The parsed JSON response def get(path, params: {}) - get_without_prefix(full_url(path), params:) + get_url(full_url(path), params:) end - def get_without_prefix(path, params: {}) - response = @http.get(path, params:) + # Get response from a URL + # Must pass a full URL, including scheme (http/https), host, etc. + # + # @param path [String] The full URL path + # + # @return [Hash] The parsed JSON response + def get_url(url, params: {}) + uri = URI.parse(url) + raise ArgumentError, 'URL must be http or https' unless %w[http https].include?(uri.scheme) + + response = @http.get(url, params:) handle_response(response) + rescue ArgumentError => e + logger.error('Invalid URL', url:, exception: e) + raise Error, "Invalid URL: #{e.message}" end def market diff --git a/test/kalshi/client_test.rb b/test/kalshi/client_test.rb index 28b2e29..82de174 100644 --- a/test/kalshi/client_test.rb +++ b/test/kalshi/client_test.rb @@ -18,6 +18,35 @@ end end + describe '#get_url' do + it 'fetches from a full URL' do + url = 'https://example.com/api/resource' + stub_request(:get, url) + .to_return(status: 200, body: '{"data": "value"}', headers: { 'Content-Type' => 'application/json' }) + + client = Rubyists::Kalshi::Client.new + response = client.get_url(url) + + assert_equal({ data: 'value' }, response) + end + + it 'raises Error for invalid URL scheme' do + client = Rubyists::Kalshi::Client.new + error = assert_raises(Rubyists::Kalshi::Error) do + client.get_url('ftp://example.com') + end + assert_match(/Invalid URL/, error.message) + end + + it 'raises Error for invalid URL format' do + client = Rubyists::Kalshi::Client.new + error = assert_raises(Rubyists::Kalshi::Error) do + client.get_url('not_a_url') + end + assert_match(/Invalid URL/, error.message) + end + end + describe '#market' do it 'returns a Market::Client instance' do client = Rubyists::Kalshi::Client.new