Skip to content

Commit a641a5f

Browse files
authored
fix: renamed #get_without_prefix to #get_url for clarity (#11)
1 parent a608fbe commit a641a5f

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
kalshi (0.0.3)
4+
kalshi (0.0.4)
55
dry-cli
66
dry-configurable
77
dry-container
@@ -230,7 +230,7 @@ CHECKSUMS
230230
httpx (1.7.0) sha256=e219689555951f9c13c40862da120cdd2535e1454189bed589f04d7059557154
231231
io-console (0.8.2) sha256=d6e3ae7a7cc7574f4b8893b4fca2162e57a825b223a177b7afa236c5ef9814cc
232232
json (2.18.0) sha256=b10506aee4183f5cf49e0efc48073d7b75843ce3782c68dbeb763351c08fd505
233-
kalshi (0.0.3)
233+
kalshi (0.0.4)
234234
language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
235235
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87
236236
logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203

lib/kalshi/client.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'httpx'
44
require 'json'
5+
require 'uri'
56

67
module Rubyists
78
module Kalshi
@@ -19,13 +20,33 @@ def initialize(base_url: Kalshi.config.base_url)
1920
.with(origin: base_url)
2021
end
2122

23+
# Get response from a path, adding the base_url,
24+
# and a prefix, if set on the client.
25+
#
26+
# see #full_path for details
27+
#
28+
# @param path [String] The URL path
29+
#
30+
# @return [Hash] The parsed JSON response
2231
def get(path, params: {})
23-
get_without_prefix(full_url(path), params:)
32+
get_url(full_url(path), params:)
2433
end
2534

26-
def get_without_prefix(path, params: {})
27-
response = @http.get(path, params:)
35+
# Get response from a URL
36+
# Must pass a full URL, including scheme (http/https), host, etc.
37+
#
38+
# @param path [String] The full URL path
39+
#
40+
# @return [Hash] The parsed JSON response
41+
def get_url(url, params: {})
42+
uri = URI.parse(url)
43+
raise ArgumentError, 'URL must be http or https' unless %w[http https].include?(uri.scheme)
44+
45+
response = @http.get(url, params:)
2846
handle_response(response)
47+
rescue ArgumentError => e
48+
logger.error('Invalid URL', url:, exception: e)
49+
raise Error, "Invalid URL: #{e.message}"
2950
end
3051

3152
def market

test/kalshi/client_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@
1818
end
1919
end
2020

21+
describe '#get_url' do
22+
it 'fetches from a full URL' do
23+
url = 'https://example.com/api/resource'
24+
stub_request(:get, url)
25+
.to_return(status: 200, body: '{"data": "value"}', headers: { 'Content-Type' => 'application/json' })
26+
27+
client = Rubyists::Kalshi::Client.new
28+
response = client.get_url(url)
29+
30+
assert_equal({ data: 'value' }, response)
31+
end
32+
33+
it 'raises Error for invalid URL scheme' do
34+
client = Rubyists::Kalshi::Client.new
35+
error = assert_raises(Rubyists::Kalshi::Error) do
36+
client.get_url('ftp://example.com')
37+
end
38+
assert_match(/Invalid URL/, error.message)
39+
end
40+
41+
it 'raises Error for invalid URL format' do
42+
client = Rubyists::Kalshi::Client.new
43+
error = assert_raises(Rubyists::Kalshi::Error) do
44+
client.get_url('not_a_url')
45+
end
46+
assert_match(/Invalid URL/, error.message)
47+
end
48+
end
49+
2150
describe '#market' do
2251
it 'returns a Market::Client instance' do
2352
client = Rubyists::Kalshi::Client.new

0 commit comments

Comments
 (0)