Skip to content

Commit 22e847c

Browse files
committed
refactor: moves Candlesticks to its own class and initiates a Filter pattern
1 parent 489f73f commit 22e847c

File tree

9 files changed

+186
-146
lines changed

9 files changed

+186
-146
lines changed

Readme.adoc

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,15 @@ Get candlesticks:
7676

7777
[source,ruby]
7878
----
79-
candlesticks = client.polymarket.markets.candlesticks(
80-
condition_id: 'condition_id',
81-
start_time: 1625097600,
82-
end_time: 1625184000,
83-
interval: 60
84-
)
85-
----
86-
87-
==== Orders
88-
89-
List orders:
90-
91-
[source,ruby]
92-
----
93-
orders = client.polymarket.orders.list(
94-
market_slug: 'market_slug',
95-
limit: 10
79+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
80+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new(
81+
condition_id: 'condition_id',
82+
start_time: 1625097600,
83+
end_time: 1625184000,
84+
interval: 60
85+
)
9686
)
87+
candlesticks = client.polymarket.candlesticks.get(filter)
9788
----
9889

9990
== Development
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
module Rubyists
4+
module Domeapi
5+
module Polymarket
6+
# Candlesticks API endpoints
7+
class Candlesticks
8+
attr_reader :client
9+
10+
# @param client [Rubyists::Domeapi::Polymarket::Client]
11+
#
12+
# @return [void]
13+
def initialize(client = Rubyists::Domeapi::Polymarket::Client.new)
14+
@client = client
15+
end
16+
17+
# Filter for candlestick data,
18+
# from https://docs.domeapi.io/api-reference/endpoint/get-candlesticks
19+
class Filter < Contract
20+
Properties = Struct.new(
21+
:condition_id,
22+
:start_time,
23+
:end_time,
24+
:interval,
25+
keyword_init: true
26+
)
27+
28+
Properties.members.each { |member| property member, populator: ->(value:, **) { value || skip! } }
29+
30+
validation do
31+
params do
32+
required(:condition_id).filled(:string)
33+
required(:start_time).filled(:integer)
34+
required(:end_time).filled(:integer)
35+
optional(:interval).maybe(:integer, gteq?: 1, lteq?: 1440)
36+
end
37+
end
38+
end
39+
40+
# Get OHLC candlesticks
41+
#
42+
# @param filter [Filter] Filter options
43+
#
44+
# @return [Hash] candlestick data
45+
def get(filter = Filter.new(Filter::Properties.new))
46+
raise ArgumentError, filter.errors.full_messages.join(', ') unless filter.valid?
47+
48+
client.get('markets/get_candlesticks', params: filter.to_h)
49+
end
50+
end
51+
end
52+
end
53+
end

lib/domeapi/polymarket/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def markets
2020
@markets ||= Markets.new(client)
2121
end
2222

23-
def orders
24-
@orders ||= Orders.new(client)
23+
def candlesticks
24+
@candlesticks ||= Candlesticks.new(client)
2525
end
2626
end
2727
end

lib/domeapi/polymarket/market_filter.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/domeapi/polymarket/markets.rb

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@ module Polymarket
77
class Markets
88
attr_reader :client
99

10+
# Filter for Polymarket markets,
11+
# from https://docs.domeapi.io/api-reference/endpoint/get-markets
12+
class Filter < Contract
13+
Properties = Struct.new(
14+
*custom_definitions,
15+
:market_slug,
16+
:event_slug,
17+
:condition_id,
18+
:tags,
19+
:status,
20+
:min_volume,
21+
:limit,
22+
:offset,
23+
:start_time,
24+
:end_time,
25+
keyword_init: true
26+
)
27+
28+
# Define properties with custom populator to skip optional params with nil values
29+
(Properties.members - custom_definitions).each do |member|
30+
property member, populator: ->(value:, **) { value || skip! }
31+
end
32+
33+
validation do
34+
params do
35+
optional(:status).maybe(:string, included_in?: %w[open closed])
36+
optional(:offset).maybe(:integer, gteq?: 0, lteq?: 100)
37+
end
38+
end
39+
end
40+
1041
class << self
1142
# @see #list
1243
def list(...)
@@ -25,8 +56,8 @@ def initialize(client = Rubyists::Domeapi::Polymarket::Client.new)
2556
# @param filter [MarketFilter] Filter options
2657
#
2758
# @return [Array<Polymarket::Market>] list of markets
28-
def list(filter = MarketFilter.new(MarketFilter::Properties.new))
29-
raise ArgumentError, filter.errors.full_messages.join(', ') unless filter.validate({})
59+
def list(filter = Filter.new(Filter::Properties.new))
60+
raise ArgumentError, filter.errors.full_messages.join(', ') unless filter.valid?
3061

3162
client.get('markets', params: filter.to_h)
3263
end
@@ -42,24 +73,6 @@ def price(token_id:, at_time: nil)
4273
params[:at_time] = at_time if at_time
4374
client.get('markets/get_market_price', params: params)
4475
end
45-
46-
# Get OHLC candlesticks
47-
#
48-
# @param condition_id [String]
49-
# @param start_time [Integer]
50-
# @param end_time [Integer]
51-
# @param interval [Integer]
52-
#
53-
# @return [Hash] candlestick data
54-
def candlesticks(condition_id:, start_time:, end_time:, interval:)
55-
params = {
56-
condition_id: condition_id,
57-
start_time: start_time,
58-
end_time: end_time,
59-
interval: interval
60-
}
61-
client.get('markets/get_candlesticks', params: params)
62-
end
6376
end
6477
end
6578
end

lib/domeapi/polymarket/orders.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../helper'
4+
5+
describe Rubyists::Domeapi::Client do
6+
let(:client) { Rubyists::Domeapi::Client.new }
7+
let(:candlesticks) { client.polymarket.candlesticks }
8+
9+
before do
10+
Rubyists::Domeapi.configure do |config|
11+
config.api_key = 'test_api_key'
12+
end
13+
end
14+
15+
describe 'Polymarket Candlesticks' do
16+
it 'gets candlesticks with interval' do
17+
stub_request(:get, 'https://api.domeapi.io/v1/polymarket/markets/get_candlesticks')
18+
.with(query: { condition_id: 'abc', start_time: '100', end_time: '200', interval: '60' })
19+
.to_return(status: 200, body: '{"candlesticks": []}')
20+
21+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
22+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new(
23+
condition_id: 'abc',
24+
start_time: 100,
25+
end_time: 200,
26+
interval: 60
27+
)
28+
)
29+
response = candlesticks.get(filter)
30+
31+
_(response).must_equal({ candlesticks: [] })
32+
end
33+
34+
it 'gets candlesticks without interval' do
35+
stub_request(:get, 'https://api.domeapi.io/v1/polymarket/markets/get_candlesticks')
36+
.with(query: { condition_id: 'abc', start_time: '100', end_time: '200' })
37+
.to_return(status: 200, body: '{"candlesticks": []}')
38+
39+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
40+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new(
41+
condition_id: 'abc',
42+
start_time: 100,
43+
end_time: 200
44+
)
45+
)
46+
response = candlesticks.get(filter)
47+
48+
_(response).must_equal({ candlesticks: [] })
49+
end
50+
51+
it 'validates required parameters' do
52+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
53+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new
54+
)
55+
56+
error = _ { candlesticks.get(filter) }.must_raise ArgumentError
57+
_(error.message).must_include 'Condition Id must be filled'
58+
_(error.message).must_include 'Start Time must be filled'
59+
_(error.message).must_include 'End Time must be filled'
60+
end
61+
62+
it 'validates interval range' do
63+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
64+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new(
65+
condition_id: 'abc',
66+
start_time: 100,
67+
end_time: 200,
68+
interval: 1441
69+
)
70+
)
71+
72+
error = _ { candlesticks.get(filter) }.must_raise ArgumentError
73+
_(error.message).must_include 'Interval must be less than or equal to 1440'
74+
75+
filter = Rubyists::Domeapi::Polymarket::Candlesticks::Filter.new(
76+
Rubyists::Domeapi::Polymarket::Candlesticks::Filter::Properties.new(
77+
condition_id: 'abc',
78+
start_time: 100,
79+
end_time: 200,
80+
interval: 0
81+
)
82+
)
83+
84+
error = _ { candlesticks.get(filter) }.must_raise ArgumentError
85+
_(error.message).must_include 'Interval must be greater than or equal to 1'
86+
end
87+
end
88+
end

test/domeapi/polymarket/markets_test.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,13 @@
2929
_(response).must_equal({ price: 0.5 })
3030
end
3131

32-
it 'gets candlesticks' do
33-
stub_request(:get, 'https://api.domeapi.io/v1/polymarket/markets/get_candlesticks')
34-
.with(query: { condition_id: 'abc', start_time: '100', end_time: '200', interval: '60' })
35-
.to_return(status: 200, body: '{"candlesticks": []}')
36-
37-
response = markets.candlesticks(condition_id: 'abc', start_time: 100, end_time: 200, interval: 60)
38-
39-
_(response).must_equal({ candlesticks: [] })
40-
end
41-
4232
it 'lists markets' do
4333
stub_request(:get, 'https://api.domeapi.io/v1/polymarket/markets')
4434
.with(query: { limit: '10', offset: '0' })
4535
.to_return(status: 200, body: '[{"id": "market1"}]')
4636

47-
filter = Rubyists::Domeapi::Polymarket::MarketFilter.new(
48-
Rubyists::Domeapi::Polymarket::MarketFilter::Properties.new(limit: 10, offset: 0)
37+
filter = Rubyists::Domeapi::Polymarket::Markets::Filter.new(
38+
Rubyists::Domeapi::Polymarket::Markets::Filter::Properties.new(limit: 10, offset: 0)
4939
)
5040
response = markets.list(filter)
5141

test/domeapi/polymarket/orders_test.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)