module IpfsPublicGatewayChecker

Constants

Channel
GATEWAY_LIST_URL
HASH_STRING
HASH_TO_TEST
VERSION

Public Class Methods

active_list() click to toggle source

get all active gateway list I've tried to use concurrent-edge to do this parallel detecting but there's always some problem. Perhaps I havn't grasped the gem's usage yet, or due to it's still experimental. Then I use the more robust Parallel gem

# File lib/ipfs_public_gateway_checker.rb, line 33
def active_list
  list = gateway_list
  return unless list
  active_gateways = []
  Parallel.map(list, in_threads: list.length) do |url|
    test_url = url.sub(':hash', HASH_TO_TEST)
    active_gateways << url if check(test_url)
  end
  active_gateways&.compact
end
check(url, timeout=5) click to toggle source
# File lib/ipfs_public_gateway_checker.rb, line 48
def check(url, timeout=5)
  begin
    resp = RestClient::Request.execute(method: :get, url: url, timeout: timeout)
  rescue
    return false
  end
  # puts resp.body
  return false unless resp.code == 200
  resp.body&.chomp == HASH_STRING
end
gateway_list() click to toggle source
# File lib/ipfs_public_gateway_checker.rb, line 44
def gateway_list
  JSON.parse(RestClient.get(GATEWAY_LIST_URL) || '')
end
get() click to toggle source

get the possible fastest one

# File lib/ipfs_public_gateway_checker.rb, line 16
def get
  list = gateway_list
  return unless list
  ch = Channel.new
  list.each do |url|
    Channel.go do
      test_url = url.sub(':hash', HASH_TO_TEST)
      ch << url if check(test_url)
    end
  end
  ~ch
end