Class: Rubyists::Kalshi::Client

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/kalshi/client.rb

Overview

HTTP Client for Kalshi API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: Kalshi.config.base_url) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
# File 'lib/kalshi/client.rb', line 16

def initialize(base_url: Kalshi.config.base_url)
  @base_url = base_url
  @http = HTTPX.plugin(:persistent)
               .plugin(:rate_limiter)
               .with(origin: base_url)
end

Instance Attribute Details

#base_url ⇒ Object (readonly)

Returns the value of attribute base_url.



13
14
15
# File 'lib/kalshi/client.rb', line 13

def base_url
  @base_url
end

#http ⇒ Object (readonly)

Returns the value of attribute http.



13
14
15
# File 'lib/kalshi/client.rb', line 13

def http
  @http
end

#prefix ⇒ Object

Returns the value of attribute prefix.



14
15
16
# File 'lib/kalshi/client.rb', line 14

def prefix
  @prefix
end

Instance Method Details

#events ⇒ Object



60
61
62
# File 'lib/kalshi/client.rb', line 60

def events
  @events ||= Events::Client.new(clone)
end

#get(path, params: {}) ⇒ Hash

Get response from a path, adding the base_url, and a prefix, if set on the client.

see #full_path for details

Parameters:

  • path (String) —

    The URL path

Returns:

  • (Hash) —

    The parsed JSON response



31
32
33
# File 'lib/kalshi/client.rb', line 31

def get(path, params: {})
  get_url(full_url(path), params:)
end

#get_url(url, params: {}) ⇒ Hash

Get response from a URL Must pass a full URL, including scheme (http/https), host, etc.

Parameters:

  • path (String) —

    The full URL path

Returns:

  • (Hash) —

    The parsed JSON response



41
42
43
44
45
46
47
48
49
50
# File 'lib/kalshi/client.rb', line 41

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

#market ⇒ Object



52
53
54
# File 'lib/kalshi/client.rb', line 52

def market
  @market ||= Market::Client.new(clone)
end

#search ⇒ Object



56
57
58
# File 'lib/kalshi/client.rb', line 56

def search
  @search ||= Search::Client.new(clone)
end

#series ⇒ Object



64
65
66
# File 'lib/kalshi/client.rb', line 64

def series
  @series ||= Series::Client.new(clone)
end