Class: Polygonio::Rest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/polygonio/rest/client.rb

Constant Summary collapse

BASE_URL =
"https://api.polygon.io/"
RETRY_OPTIONS =
{
  max: 2,
  interval: 0.05,
  interval_randomness: 0.5,
  backoff_factor: 2,
  exceptions: [Faraday::ConnectionFailed].concat(Faraday::Request::Retry::DEFAULT_EXCEPTIONS)
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, &block) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/polygonio/rest/client.rb', line 12

def initialize(api_key, &block)
  @url = BASE_URL
  @api_key = Types::String[api_key]
  @request_builder = block if block_given?
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/polygonio/rest/client.rb', line 10

def api_key
  @api_key
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/polygonio/rest/client.rb', line 10

def url
  @url
end

Instance Method Details

#cryptoObject



54
55
56
# File 'lib/polygonio/rest/client.rb', line 54

def crypto
  Rest::Crypto.new(self)
end

#forexObject



50
51
52
# File 'lib/polygonio/rest/client.rb', line 50

def forex
  Rest::Forex.new(self)
end

#referenceObject



37
38
39
40
41
42
43
44
# File 'lib/polygonio/rest/client.rb', line 37

def reference
  Struct::Reference.new(
    Rest::Reference::Locales.new(self),
    Rest::Reference::Markets.new(self),
    Rest::Reference::Stocks.new(self),
    Rest::Reference::Tickers.new(self)
  )
end

#requestObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/polygonio/rest/client.rb', line 26

def request
  Faraday.new(url: "#{url}?apiKey=#{api_key}") do |builder|
    builder.request :retry, RETRY_OPTIONS
    builder.use ErrorMiddleware
    @request_builder&.call(builder)
    builder.request :json
    builder.response :oj
    builder.adapter Faraday.default_adapter
  end
end

#stocksObject



46
47
48
# File 'lib/polygonio/rest/client.rb', line 46

def stocks
  Rest::Stocks.new(self)
end