Class: BudaApi::PublicClient

Inherits:
Client
  • Object
show all
Includes:
Models
Defined in:
lib/buda_api/public_client.rb

Overview

Public API client for endpoints that don't require authentication

Direct Known Subclasses

AuthenticatedClient

Constant Summary

Constants inherited from Client

Client::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Client

#base_url, #debug_mode, #retries, #timeout

Instance Method Summary collapse

Methods inherited from Client

#initialize

Constructor Details

This class inherits a constructor from BudaApi::Client

Instance Method Details

#average_prices_report(market_id, start_at: nil, end_at: nil) ⇒ Array<AveragePrice>

Get average prices report

Examples:

client = BudaApi::PublicClient.new
start_time = Time.now - 86400  # 24 hours ago
prices = client.average_prices_report("BTC-CLP", start_at: start_time)

Parameters:

  • market_id (String) —

    market identifier

  • start_at (Time, nil) (defaults to: nil) —

    start time for report

  • end_at (Time, nil) (defaults to: nil) —

    end time for report

Returns:



179
180
181
182
183
# File 'lib/buda_api/public_client.rb', line 179

def average_prices_report(market_id, start_at: nil, end_at: nil)
  get_report(market_id, ReportType::AVERAGE_PRICES, start_at, end_at) do |report_data|
    AveragePrice.new(report_data)
  end
end

#candlestick_report(market_id, start_at: nil, end_at: nil) ⇒ Array<Candlestick>

Get candlestick report

Examples:

client = BudaApi::PublicClient.new
start_time = Time.now - 86400  # 24 hours ago
candles = client.candlestick_report("BTC-CLP", start_at: start_time)

Parameters:

  • market_id (String) —

    market identifier

  • start_at (Time, nil) (defaults to: nil) —

    start time for report

  • end_at (Time, nil) (defaults to: nil) —

    end time for report

Returns:



195
196
197
198
199
# File 'lib/buda_api/public_client.rb', line 195

def candlestick_report(market_id, start_at: nil, end_at: nil)
  get_report(market_id, ReportType::CANDLESTICK, start_at, end_at) do |report_data|
    Candlestick.new(report_data)
  end
end

#market_details(market_id) ⇒ Market

Get details for a specific market

Examples:

client = BudaApi::PublicClient.new
market = client.market_details("BTC-CLP")
puts "Minimum order: #{market.minimum_order_amount}"

Parameters:

  • market_id (String) —

    market identifier (e.g., "BTC-CLP")

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/buda_api/public_client.rb', line 35

def market_details(market_id)
  validate_required_params({ market_id: market_id }, [:market_id])
  validate_param_values({ market_id: market_id }, { market_id: Market::ALL })

  BudaApi::Logger.info("Fetching market details for #{market_id}")
  
  response = get("markets/#{market_id}")
  Market.new(response["market"])
end

#markets ⇒ Array<Market>

Get all available markets

Examples:

client = BudaApi::PublicClient.new
markets = client.markets
markets.each { |market| puts "#{market.id}: #{market.name}" }

Returns:

  • (Array<Market>) —

    list of available markets



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/buda_api/public_client.rb', line 15

def markets
  BudaApi::Logger.info("Fetching all markets")
  
  response = get("markets")
  markets_data = response["markets"] || []
  
  markets = markets_data.map { |market_data| Market.new(market_data) }
  BudaApi::Logger.info("Retrieved #{markets.length} markets")
  
  markets
end

#order_book(market_id) ⇒ OrderBook

Get order book for a market

Examples:

client = BudaApi::PublicClient.new
order_book = client.order_book("BTC-CLP") 
puts "Best ask: #{order_book.best_ask.price}"
puts "Best bid: #{order_book.best_bid.price}"
puts "Spread: #{order_book.spread_percentage}%"

Parameters:

  • market_id (String) —

    market identifier

Returns:



74
75
76
77
78
79
80
81
82
# File 'lib/buda_api/public_client.rb', line 74

def order_book(market_id)
  validate_required_params({ market_id: market_id }, [:market_id])
  validate_param_values({ market_id: market_id }, { market_id: Market::ALL })

  BudaApi::Logger.info("Fetching order book for #{market_id}")
  
  response = get("markets/#{market_id}/order_book")
  OrderBook.new(response["order_book"])
end

#quotation(market_id, quotation_type, amount, limit: nil) ⇒ Quotation

Get a quotation for a potential trade

Examples:

client = BudaApi::PublicClient.new
# Get quotation for buying 0.1 BTC at market price
quote = client.quotation("BTC-CLP", "bid_given_size", 0.1)
puts "You would pay: #{quote.quote_balance_change}"

Parameters:

  • market_id (String) —

    market identifier

  • quotation_type (String) —

    type of quotation

  • amount (Float) —

    amount for quotation

  • limit (Float, nil) (defaults to: nil) —

    limit price for limit quotations

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/buda_api/public_client.rb', line 121

def quotation(market_id, quotation_type, amount, limit: nil)
  validate_required_params({ 
    market_id: market_id, 
    quotation_type: quotation_type, 
    amount: amount 
  }, [:market_id, :quotation_type, :amount])
  
  validate_param_values({ 
    market_id: market_id,
    quotation_type: quotation_type 
  }, { 
    market_id: Market::ALL,
    quotation_type: QuotationType::ALL 
  })

  quotation_payload = {
    type: quotation_type,
    amount: amount.to_s
  }
  quotation_payload[:limit] = limit.to_s if limit

  BudaApi::Logger.info("Getting quotation for #{market_id}: #{quotation_type} #{amount}")
  
  response = post("markets/#{market_id}/quotations", body: { quotation: quotation_payload })
  Quotation.new(response["quotation"])
end

#quotation_limit(market_id, quotation_type, amount, limit) ⇒ Quotation

Get a limit quotation (with limit price)

Parameters:

  • market_id (String) —

    market identifier

  • quotation_type (String) —

    type of quotation

  • amount (Float) —

    amount for quotation

  • limit (Float) —

    limit price

Returns:



165
166
167
# File 'lib/buda_api/public_client.rb', line 165

def quotation_limit(market_id, quotation_type, amount, limit)
  quotation(market_id, quotation_type, amount, limit: limit)
end

#quotation_market(market_id, quotation_type, amount) ⇒ Quotation

Get a market quotation (no limit price)

Parameters:

  • market_id (String) —

    market identifier

  • quotation_type (String) —

    type of quotation

  • amount (Float) —

    amount for quotation

Returns:



154
155
156
# File 'lib/buda_api/public_client.rb', line 154

def quotation_market(market_id, quotation_type, amount)
  quotation(market_id, quotation_type, amount)
end

#ticker(market_id) ⇒ Ticker

Get ticker information for a market

Examples:

client = BudaApi::PublicClient.new  
ticker = client.ticker("BTC-CLP")
puts "Last price: #{ticker.last_price}"
puts "24h change: #{ticker.price_variation_24h}%"

Parameters:

  • market_id (String) —

    market identifier

Returns:

  • (Ticker) —

    current ticker information



54
55
56
57
58
59
60
61
62
# File 'lib/buda_api/public_client.rb', line 54

def ticker(market_id)
  validate_required_params({ market_id: market_id }, [:market_id])
  validate_param_values({ market_id: market_id }, { market_id: Market::ALL })

  BudaApi::Logger.info("Fetching ticker for #{market_id}")
  
  response = get("markets/#{market_id}/ticker")
  Ticker.new(response["ticker"])
end

#trades(market_id, timestamp: nil, limit: nil) ⇒ Trades

Get recent trades for a market

Examples:

client = BudaApi::PublicClient.new
trades = client.trades("BTC-CLP", limit: 10)
trades.each { |trade| puts "#{trade.amount} at #{trade.price}" }

Parameters:

  • market_id (String) —

    market identifier

  • timestamp (Integer, nil) (defaults to: nil) —

    trades after this timestamp

  • limit (Integer, nil) (defaults to: nil) —

    maximum number of trades to return

Returns:

  • (Trades) —

    collection of recent trades



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/buda_api/public_client.rb', line 94

def trades(market_id, timestamp: nil, limit: nil)
  validate_required_params({ market_id: market_id }, [:market_id])
  validate_param_values({ market_id: market_id }, { market_id: Market::ALL })

  params = normalize_params({
    timestamp: timestamp,
    limit: limit
  })

  BudaApi::Logger.info("Fetching trades for #{market_id} with params: #{params}")
  
  response = get("markets/#{market_id}/trades", params)
  Trades.new(response["trades"])
end