File: header_value.rb

package info (click to toggle)
ruby-aws-eventstream 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 108 kB
  • sloc: ruby: 283; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,131 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module Aws
  module EventStream

    class HeaderValue

      def initialize(options)
        @type = options.fetch(:type)
        @value = options[:format] ?
          format_value(options.fetch(:value)) :
          options.fetch(:value)
      end

      attr_reader :value

      # @return [String] type of the header value
      #   complete type list see Aws::EventStream::Types
      attr_reader :type

      private

      def format_value(value)
        case @type
        when 'timestamp' then format_timestamp(value)
        when 'uuid' then format_uuid(value)
        else
          value
        end
      end

      def format_uuid(value)
        bytes = value.bytes
        # For user-friendly uuid representation,
        # format binary bytes into uuid string format
        uuid_pattern = [ [ 3, 2, 1, 0 ], [ 5, 4 ], [ 7, 6 ], [ 8, 9 ], 10..15 ]
        uuid_pattern.map {|p| p.map {|n| "%02x" % bytes.to_a[n] }.join }.join('-')
      end

      def format_timestamp(value)
        # millis_since_epoch to sec_since_epoch
        Time.at(value / 1000.0)
      end

    end

  end
end