Class: Insta::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/insta/snapshot.rb,
sig/insta/snapshot.rbs

Constant Summary collapse

SEPARATOR =

@rbs! type snapshot_metadata = Hash[String, untyped]

Returns:

  • (String)
"---"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, metadata = {}) ⇒ Snapshot

: (String, ?snapshot_metadata) -> void

Parameters:

  • (String)
  • (snapshot_metadata)


16
17
18
19
# File 'lib/insta/snapshot.rb', line 16

def initialize(content,  = {})
  @content = content
  @metadata = 
end

Instance Attribute Details

#contentString (readonly)

: String

Returns:

  • (String)


12
13
14
# File 'lib/insta/snapshot.rb', line 12

def content
  @content
end

#metadatasnapshot_metadata (readonly)

: snapshot_metadata

Returns:

  • (snapshot_metadata)


13
14
15
# File 'lib/insta/snapshot.rb', line 13

def 
  @metadata
end

Class Method Details

.parse(raw) ⇒ Snapshot

: (String) -> Snapshot

Parameters:

  • (String)

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/insta/snapshot.rb', line 22

def self.parse(raw)
  raw = raw.to_s

  if raw.start_with?("#{SEPARATOR}\n")
    parts = raw.split("#{SEPARATOR}\n", 3)

    if parts.length >= 3
       = begin
        YAML.safe_load(parts[1], permitted_classes: [Symbol]) || {}
      rescue Psych::SyntaxError
        {} #: snapshot_metadata
      end

      return new(parts[2].chomp.concat("\n"), )
    end
  end

  new(raw, {})
end

Instance Method Details

#descriptionString?

: () -> String?

Returns:

  • (String, nil)


60
61
62
# File 'lib/insta/snapshot.rb', line 60

def description
  @metadata["description"]
end

#expressionString?

: () -> String?

Returns:

  • (String, nil)


65
66
67
# File 'lib/insta/snapshot.rb', line 65

def expression
  @metadata["expression"]
end

#infoHash[String, untyped]?

: () -> Hash[String, untyped]?

Returns:

  • (Hash[String, untyped], nil)


70
71
72
# File 'lib/insta/snapshot.rb', line 70

def info
  @metadata["info"]
end

#inputString?

: () -> String?

Returns:

  • (String, nil)


55
56
57
# File 'lib/insta/snapshot.rb', line 55

def input
  @metadata["input"]
end

#serializeString

: () -> String

Returns:

  • (String)


43
44
45
46
47
# File 'lib/insta/snapshot.rb', line 43

def serialize
  return @content if @metadata.empty?

  YAML.dump(@metadata) + "#{SEPARATOR}\n" + @content
end

#sourceString?

: () -> String?

Returns:

  • (String, nil)


50
51
52
# File 'lib/insta/snapshot.rb', line 50

def source
  @metadata["source"]
end