Class: Drand::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, chain_hash:) ⇒ HttpClient

Returns a new instance of HttpClient.



11
12
13
14
# File 'lib/drand/http_client.rb', line 11

def initialize(base_url:, chain_hash:)
  @base_url = base_url.to_s.chomp("/")
  @chain_hash = chain_hash
end

Instance Method Details

#fetch_round(number) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/drand/http_client.rb', line 16

def fetch_round(number)
  uri = URI("#{@base_url}/#{@chain_hash}/public/#{number}")
  body = get(uri)
  data = JSON.parse(body)

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