Class: Drand::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/drand/http_client.rb

Constant Summary collapse

OPEN_TIMEOUT =
3
READ_TIMEOUT =
3
RETRYABLE_NETWORK_ERRORS =
[
  SocketError,
  Net::OpenTimeout,
  Net::ReadTimeout,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::ENETUNREACH,
  IOError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(endpoints:, chain_hash:) ⇒ HttpClient

Returns a new instance of HttpClient.

Raises:



25
26
27
28
29
30
31
# File 'lib/drand/http_client.rb', line 25

def initialize(endpoints:, chain_hash:)
  list = Array(endpoints).map { |u| u.to_s.chomp("/") }
  raise ArgumentError, "at least one endpoint is required" if list.empty?

  @endpoints = list
  @chain_hash = chain_hash
end

Instance Method Details

#fetch_round(number) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/drand/http_client.rb', line 33

def fetch_round(number)
  body, served_by = fetch_with_fallback("/#{@chain_hash}/public/#{number}")
  data = JSON.parse(body)

  {
    round: Integer(data.fetch("round")),
    randomness: data.fetch("randomness"),
    signature: data.fetch("signature"),
    previous_signature: data["previous_signature"],
    served_by: served_by
  }
rescue JSON::ParserError => e
  raise NetworkError, "malformed JSON from drand: #{e.message}"
rescue KeyError => e
  raise NetworkError, "missing field in drand response: #{e.message}"
end