Created
October 1, 2019 12:34
-
-
Save manveru/e2e530a147ca056c7b533a1933dd3ee0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'json' | |
def make_query_inner(max_depth = 3, depth = 0) | |
return [] if depth >= max_depth | |
indent = ' ' * (6 + (depth * 2)) | |
["\n#{indent}", | |
"previousBlock {\n#{indent} id", | |
*make_query_inner(max_depth, depth + 1), | |
"\n#{indent}}"] | |
end | |
def make_query | |
<<-GRAPHQL | |
query History { | |
status { | |
latestBlock { | |
id | |
#{make_query_inner(ARGV[0].to_i).join} | |
} | |
} | |
} | |
GRAPHQL | |
end | |
def render_previous(parent_id, obj) | |
return unless obj | |
puts %("#{obj['id']}" -> "#{parent_id}";) | |
render_previous(obj['id'], obj['previousBlock']) | |
end | |
def render(graph) | |
start = graph['data']['status']['latestBlock'] | |
start_id = start['id'] | |
puts "digraph chain {" | |
render_previous(start_id, start['previousBlock']) | |
puts "}" | |
end | |
uri = URI.parse('http://127.0.0.1:3101/explorer/graphql') | |
headers = { | |
'Content-Type' => 'application/json', | |
'Accept' => 'application/json' | |
} | |
request = Net::HTTP::Post.new(uri.request_uri, headers) | |
request.body = { | |
query: make_query, | |
variables: nil, | |
operationName: 'History' | |
}.to_json | |
response = Net::HTTP.new(uri.host, uri.port).request(request) | |
pp response unless response.code == 200 | |
graph = JSON.parse(response.body, max_nesting: 1000) | |
render(graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment