aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/dsl/appcast.rb
blob: fc7e83a20dd971ca7c15c648d23156a942b77aba (plain)
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
require "hbc/system_command"

module Hbc
  class DSL
    class Appcast
      attr_reader :parameters, :checkpoint

      def initialize(uri, parameters = {})
        @parameters     = parameters
        @uri            = UnderscoreSupportingURI.parse(uri)
        @checkpoint     = @parameters[:checkpoint]
      end

      def calculate_checkpoint
        curl_executable, *args = curl_args(
          "--compressed", "--location", "--fail", @uri,
          user_agent: :fake
        )
        result = SystemCommand.run(curl_executable, args: args, print_stderr: false)

        checkpoint = if result.success?
          processed_appcast_text = result.stdout.gsub(%r{<pubDate>[^<]*</pubDate>}m, "")
          Digest::SHA2.hexdigest(processed_appcast_text)
        end

        {
          checkpoint: checkpoint,
          command_result: result,
        }
      end

      def to_yaml
        [@uri, @parameters].to_yaml
      end

      def to_s
        @uri.to_s
      end
    end
  end
end