Module: IpfsPublicGatewayChecker

Defined in:
lib/ipfs_public_gateway_checker.rb,
lib/ipfs_public_gateway_checker/version.rb

Constant Summary collapse

HASH_TO_TEST =
'Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a'
HASH_STRING =
'Hello from IPFS Gateway Checker'
GATEWAY_LIST_URL =
'https://raw.githubusercontent.com/ipfs/public-gateway-checker/master/gateways.json'
Channel =
Concurrent::Channel
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.active_listObject

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



33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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_listObject



44
45
46
# File 'lib/ipfs_public_gateway_checker.rb', line 44

def gateway_list
  JSON.parse(RestClient.get(GATEWAY_LIST_URL) || '')
end

.getObject

get the possible fastest one



16
17
18
19
20
21
22
23
24
25
26
27
# 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