Skip to content

Commit

Permalink
Enhance SimilarWeb test (2factorauth#7487)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 authored Mar 21, 2023
1 parent 56b719a commit 1d8b40d
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions tests/similarweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def self.store(site, rank)

# Similarweb API handling
module Similarweb
def self.connect(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.request(Net::HTTP::Get.new(url, { 'Accept' => 'application/json' }))
def self.api_key
key = ENV['SIMILARWEB_API_KEY']
raise('Similarweb API key not set') if key.nil?

keys = key.split(' ')
keys[rand(0..(keys.length - 1))]
end

def self.fetch(site)
apikey = ENV['SIMILARWEB_API_KEY']
url = URI("https://api.similarweb.com/v1/similar-rank/#{site}/rank?api_key=#{apikey}")
response = connect(url)
response = Net::HTTP.get_response URI("https://api.similarweb.com/v1/similar-rank/#{site}/rank?api_key=#{api_key}")
raise("#{site} doesn't have a Similarweb ranking") if response.code.eql? '404'
raise("(#{response.code}) Request failed.") unless response.code.eql? '200'

Expand All @@ -43,16 +43,17 @@ def self.fetch(site)
end
end

raise('Similarweb API key not set') if ENV['SIMILARWEB_API_KEY'].nil?

status = 0
# Fetch changes
diff = `git diff origin/master...HEAD entries/ | sed -n 's/^+.*"domain"[^"]*"\\(.*\\)".*/\\1/p'`
# Strip and loop through diff
diff.split("\n").each do |site|
diff.split("\n").each.with_index do |site, i|
sleep 2 if i.positive?
domain = Addressable::URI.parse("https://#{site}").domain
rank = Cache.fetch(domain) || Similarweb.fetch(domain)
raise("Global rank #{rank} of #{site} is above the maximum rank of 200K") if rank.to_i > 200_000
failure = rank.to_i > 200_000
puts "\e[#{failure ? '31' : '32'}m#{domain} - #{rank}\e[39m"
raise("Global rank #{rank} of #{domain} is above the maximum rank of 200K") if failure
rescue StandardError => e
puts "\e[31m#{e.message}\e[39m"
status = 1
Expand Down

0 comments on commit 1d8b40d

Please sign in to comment.