class CommitLeaderboardSync

Attributes

api_secret[RW]
leaderboard_api_url[RW]
queue_file_path[RW]

Public Instance Methods

run(args, options) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 10
def run(args, options)
  process_args(args)
  print_configuration if options.verbose

  unless File.exist?(@queue_file_path)
    abort(">> Commit queue file does not exist.")
  end

  unless @leaderboard_api_url.match?(URI::DEFAULT_PARSER.make_regexp)
    abort(">> Invalid API URL.")
  end

  commits = JSON.parse(File.read(@queue_file_path))
  send_commits(commits)
end

Private Instance Methods

clear_queue() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 101
def clear_queue
  File.write(@queue_file_path, "[]")
end
generate_signature_for_payload(secret_key, payload) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 105
def generate_signature_for_payload(secret_key, payload)
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), secret_key, payload)
  "sig=#{signature}"
end
handle_response(response) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 85
def handle_response(response)
  case response.code.to_i
  when 200
    puts ">> Successfully sent commits to the leaderboard."
  when 401
    abort(">> Unauthorized. Please check your secret.")
  when 403
    abort(">> Forbidden. Please check your secret.")
  when 404
    abort(">> Not found. Please check the URL.")
  else
    raise SyncError, "Failed to send commits to the leaderboard:\n#{response.code} - #{response.body}"
  end
end
print_configuration() click to toggle source
print_payload(payload) click to toggle source
process_args(args) click to toggle source
# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 28
def process_args(args)
  if args[0].blank?
    abort(">> No API secret given.")
  end

  @api_secret = args[0]
  @queue_file_path = File.expand_path(args[1] || "~/.renuo_commit_leaderboard.json")
  @leaderboard_api_url = args[2] || "https://dashboard.renuo.ch/api/v1/commit_leaderboard"
end
send_commit(payload) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 69
def send_commit(payload)
  uri = URI(@leaderboard_api_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  request = Net::HTTP::Post.new(uri.path, { "Content-Type" => "application/json", "Accept" => "application/json" })
  request["LEADERBOARD_SIGNATURE"] = generate_signature_for_payload(@api_secret, payload)
  request.body = payload

  http.request(request)
end
send_commits(commits) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/renuo/cli/app/commit_leaderboard_sync.rb, line 47
def send_commits(commits)
  next_queue = []

  until commits.empty?
    commit = commits.shift
    payload = { commit: commit }.to_json

    puts "Sending commit to leaderboard..."
    print_payload(payload)

    begin
      handle_response(send_commit(payload))
    rescue SyncError => e
      puts e.message
      next_queue << commit
    end
  end

  File.write(@queue_file_path, JSON.generate(next_queue))
end