Class: LaunchDarkly::Impl::FileData::Document Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/file_data/document.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The parsed form of one data file. A document may contain full flag definitions, flag key to value entries, and segment definitions. Every hash has symbol keys.

Since:

  • 5.5.0

Constant Summary collapse

NONE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The document of YAML content that is only a null, such as an empty file, a comment, or "---": a document with no entries.

Since:

  • 5.5.0

new(flags: {}.freeze, flag_values: {}.freeze, segments: {}.freeze).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags: {}, flag_values: {}, segments: {}) ⇒ Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Document.

Parameters:

  • flags (Hash{Symbol => Hash}) (defaults to: {})
  • flag_values (Hash{Symbol => Object}) (defaults to: {})
  • segments (Hash{Symbol => Hash}) (defaults to: {})

Since:

  • 5.5.0



59
60
61
62
63
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 59

def initialize(flags: {}, flag_values: {}, segments: {})
  @flags = flags
  @flag_values = flag_values
  @segments = segments
end

Instance Attribute Details

#flag_values ⇒ Hash{Symbol => Object} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 5.5.0



49
50
51
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 49

def flag_values
  @flag_values
end

#flags ⇒ Hash{Symbol => Hash} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{Symbol => Hash})

Since:

  • 5.5.0



46
47
48
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 46

def flags
  @flags
end

#segments ⇒ Hash{Symbol => Hash} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash{Symbol => Hash})

Since:

  • 5.5.0



52
53
54
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 52

def segments
  @segments
end

Class Method Details

.parse(content, format:) ⇒ Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the content of a data file in the given format. LaunchDarkly::Impl::FileData.format_of chooses the format of a file from its name.

Whether content with no entries is valid depends on the format, like the rest of the content. Content that is empty or only white space is not a JSON document, so it fails to parse. In YAML it is a document with no entries, NONE, and so is a YAML document that is only a null, such as one with only a comment. A document that is not a mapping, or whose "flags", "flagValues", or "segments" member is not a mapping, is an error, and so is content that is not valid UTF-8, in either format.

Parameters:

  • content (String) —

    UTF-8 content

  • format (Symbol) —

    :json or :yaml

Returns:

Raises:

  • (ArgumentError) —

    if the content is not a valid document

  • (JSON::ParserError, Psych::SyntaxError) —

    if the content cannot be parsed

Since:

  • 5.5.0



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 86

def self.parse(content, format:)
  FileData.check_encoding(content)
  case format
  when :json
    # A file that is being rewritten in place can be empty for a moment. Failing to parse
    # keeps its last good data until it is complete.
    raise ArgumentError, "the file has no content, which is not a JSON document" if FileData.blank?(content)

    raw = JSON.parse(content)
  when :yaml
    # The YAML parser rejects some white space, such as a tab, that is still no content.
    return NONE if FileData.blank?(content)

    raw = YAML.safe_load(content)
    return NONE if raw.nil?
  else
    raise ArgumentError, "unknown file format #{format.inspect}"
  end
  raise ArgumentError, "file content must be an object" unless raw.is_a?(Hash)

  data = FileData.symbolize_keys(raw)
  Document.new(
    flags: section(data, :flags),
    flag_values: section(data, :flagValues),
    segments: section(data, :segments)
  )
end

.read(path) ⇒ Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads and parses one data file.

Parameters:

  • path (String)

Returns:

Raises:

  • (ReadError) —

    if the file cannot be read or parsed

Since:

  • 5.5.0



121
122
123
124
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 121

def self.read(path)
  content = FileData.read_file(path)
  FileData.parse_file(path, content)
end