File: share.rb

package info (click to toggle)
ruby-benchmark-ips 2.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 807; makefile: 11
file content (48 lines) | stat: -rw-r--r-- 1,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'net/http'
require 'net/https'
require 'json'

module Benchmark
  module IPS
    class Share
      DEFAULT_URL = "https://benchmark.fyi"
      def initialize(report, job)
        @report = report
        @job = job
      end

      def share
        base = (ENV['SHARE_URL'] || DEFAULT_URL)
        url = URI(File.join(base, "reports"))

        req = Net::HTTP::Post.new(url)

        data = {
          "entries" => @report.data,
          "options" => {
            "compare" => @job.compare?
          }
        }

        req.body = JSON.generate(data)

        http = Net::HTTP.new(url.hostname, url.port)
        if url.scheme == "https"
          http.use_ssl = true
          http.ssl_version = :TLSv1_2
        end

        res = http.start do |h|
          h.request req
        end

        if Net::HTTPOK === res
          data = JSON.parse res.body
          puts "Shared at: #{File.join(base, data["id"])}"
        else
          puts "Error sharing report"
        end
      end
    end
  end
end