Skip to content

Commit a9fcb72

Browse files
authored
feat: adds series event lookup, and events/ endpoints (#13)
* feat: adds event listing and searching * adds series path and events lookup * feat: adds series path and events lookup
1 parent ed75f86 commit a9fcb72

19 files changed

Lines changed: 415 additions & 18 deletions

lib/kalshi/client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ def search
5757
@search ||= Search::Client.new(clone)
5858
end
5959

60+
def events
61+
@events ||= Events::Client.new(clone)
62+
end
63+
64+
def series
65+
@series ||= Series::Client.new(clone)
66+
end
67+
6068
private
6169

6270
def full_url(path)
63-
File.join(*[base_url, prefix, path].compact)
71+
parts = [base_url, prefix, path].compact
72+
parts.reject!(&:empty?)
73+
File.join(*parts)
6474
end
6575

6676
def handle_response(response)

lib/kalshi/endpoint.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ module Kalshi
66
class Endpoint
77
attr_reader :client
88

9+
class << self
10+
attr_accessor :endpoint_path
11+
12+
# Set the endpoint path for the resource
13+
#
14+
# @param path [String] API endpoint path
15+
#
16+
# @return [String] endpoint path
17+
def kalshi_path(path)
18+
self.endpoint_path = path
19+
end
20+
end
21+
922
# Initialize the Endpoint
1023
#
1124
# @param client [Client] The Kalshi client

lib/kalshi/events/client.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Events
6+
# Events API Client
7+
class Client < ApiClient
8+
def list(...)
9+
List.new(client).list(...)
10+
end
11+
12+
def fetch(...)
13+
List.new(client).fetch(...)
14+
end
15+
16+
def metadata(...)
17+
List.new(client).metadata(...)
18+
end
19+
20+
def multivariate
21+
Multivariate.new(client)
22+
end
23+
24+
def candlesticks
25+
Rubyists::Kalshi::Series::EventCandlesticks.new(client)
26+
end
27+
end
28+
end
29+
end
30+
end

lib/kalshi/events/list.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Events
6+
# Events API endpoint
7+
class List < Kalshi::Endpoint
8+
include Kalshi::Listable
9+
10+
kalshi_path ''
11+
12+
# Filter for Kalshi events list
13+
class Filter < Kalshi::Contract
14+
propertize(%i[limit cursor status series_ticker with_nested_markets])
15+
16+
validation do
17+
params do
18+
optional(:limit).maybe(:integer)
19+
optional(:cursor).maybe(:string)
20+
optional(:status).maybe(:string)
21+
optional(:series_ticker).maybe(:string)
22+
optional(:with_nested_markets).maybe(:bool)
23+
end
24+
end
25+
end
26+
27+
def fetch(event_ticker)
28+
client.get(event_ticker)
29+
end
30+
31+
def metadata(event_ticker)
32+
client.get("#{event_ticker}/metadata")
33+
end
34+
end
35+
end
36+
end
37+
end

lib/kalshi/events/multivariate.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Events
6+
# Multivariate Events API endpoint
7+
class Multivariate < Kalshi::Endpoint
8+
include Kalshi::Listable
9+
10+
kalshi_path 'multivariate'
11+
12+
# Filter for Kalshi multivariate events list
13+
class Filter < Kalshi::Contract
14+
propertize(%i[limit cursor series_ticker collection_ticker])
15+
16+
validation do
17+
params do
18+
optional(:limit).maybe(:integer)
19+
optional(:cursor).maybe(:string)
20+
optional(:series_ticker).maybe(:string)
21+
optional(:collection_ticker).maybe(:string)
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end

lib/kalshi/listable.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ def self.included(base)
1010

1111
# Class methods for Listable
1212
module ClassMethods
13-
attr_accessor :endpoint_path
14-
15-
# Set the endpoint path for the resource
16-
#
17-
# @param path [String] API endpoint path
18-
#
19-
# @return [String] endpoint path
20-
def kalshi_path(path)
21-
self.endpoint_path = path
22-
end
23-
2413
def list(...)
2514
new.list(...)
2615
end

lib/kalshi/market/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def trades
3232
end
3333

3434
def candlesticks
35-
@candlesticks ||= Candlesticks.new(client)
35+
@candlesticks ||= Rubyists::Kalshi::Series::MarketCandlesticks.new(client)
3636
end
3737
end
3838
end

lib/kalshi/series/client.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Series
6+
# Series API Client
7+
class Client < ApiClient
8+
def event_candlesticks
9+
EventCandlesticks.new(client)
10+
end
11+
12+
def forecast_percentile_history
13+
ForecastPercentileHistory.new(client)
14+
end
15+
end
16+
end
17+
end
18+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Series
6+
# Event Candlesticks API endpoint
7+
class EventCandlesticks < Kalshi::Endpoint
8+
# Filter for Event Candlesticks
9+
class EventFilter < Kalshi::Contract
10+
propertize(%i[series_ticker ticker start_ts end_ts period_interval])
11+
12+
validation do
13+
params do
14+
required(:series_ticker).filled(:string)
15+
required(:ticker).filled(:string)
16+
required(:start_ts).filled(:integer)
17+
required(:end_ts).filled(:integer)
18+
required(:period_interval).filled(:integer)
19+
end
20+
end
21+
end
22+
23+
def fetch(params)
24+
filter = EventFilter.new(EventFilter::Properties.new(**params))
25+
raise ArgumentError, filter.errors.full_messages.join(', ') unless filter.validate({})
26+
27+
path = "#{filter.series_ticker}/events/#{filter.ticker}/candlesticks"
28+
query_params = filter.to_h.slice('start_ts', 'end_ts', 'period_interval')
29+
client.get(path, params: query_params)
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Kalshi
5+
module Series
6+
# Event Forecast Percentile History API endpoint
7+
class ForecastPercentileHistory < Kalshi::Endpoint
8+
# Filter for Event Forecast Percentile History
9+
class EventFilter < Kalshi::Contract
10+
propertize(%i[series_ticker ticker percentiles start_ts end_ts period_interval])
11+
12+
validation do
13+
params do
14+
required(:series_ticker).filled(:string)
15+
required(:ticker).filled(:string)
16+
required(:percentiles).filled(:array)
17+
required(:start_ts).filled(:integer)
18+
required(:end_ts).filled(:integer)
19+
required(:period_interval).filled(:integer)
20+
end
21+
end
22+
end
23+
24+
def fetch(params)
25+
filter = EventFilter.new(EventFilter::Properties.new(**params))
26+
raise ArgumentError, filter.errors.full_messages.join(', ') unless filter.validate({})
27+
28+
path = "#{filter.series_ticker}/events/#{filter.ticker}/forecast_percentile_history"
29+
query_params = filter.to_h.slice('start_ts', 'end_ts', 'period_interval')
30+
query_params[:percentiles] = filter.percentiles.join(',')
31+
client.get(path, params: query_params)
32+
end
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)