Class: VectorAmp::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_amp/source.rb

Overview

Base value object for ingestion source definitions.

Source objects can be passed to client.sources.create_source(source) to create a source or to dataset.ingest_source(source) once they include an id returned by the API.

Constant Summary collapse

SUPPORTED_SOURCE_TYPES =
%w[s3 web gcs gdrive file_upload jira confluence].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_type:, name:, config:, description: nil, metadata: nil, id: nil) ⇒ Source

Create a source value object.

Parameters:

  • source_type (String, Symbol)

    one of s3, web, gdrive, or file_upload.

  • name (String)

    source display name.

  • config (Hash)

    source-specific config sent to the API.

  • description (String, nil) (defaults to: nil)

    optional description.

  • metadata (Hash, nil) (defaults to: nil)

    optional metadata.

  • id (String, nil) (defaults to: nil)

    optional API source id.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vector_amp/source.rb', line 31

def initialize(source_type:, name:, config:, description: nil, metadata: nil, id: nil)
  raise ArgumentError, "source_type is required" if source_type.nil? || source_type.to_s.empty?
  raise ArgumentError, "name is required" if name.nil? || name.to_s.empty?
  raise ArgumentError, "config must be a Hash" unless config.is_a?(Hash)

  @id = id
  @source_type = source_type.to_s
  @name = name.to_s
  @description = description
  @config = config
  @metadata = 
end

Instance Attribute Details

#configString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def config
  @config
end

#descriptionString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def description
  @description
end

#idString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def id
  @id
end

#metadataString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def 
  @metadata
end

#nameString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def name
  @name
end

#source_typeString, ... (readonly)

Returns:

  • (String, nil)

    API source id when returned by the API.

  • (String)

    source type (s3, web, gdrive, or file_upload).

  • (String)

    source display name.

  • (String, nil)

    optional source description.

  • (Hash)

    source-specific configuration.

  • (Hash, nil)

    optional source metadata.



21
22
23
# File 'lib/vector_amp/source.rb', line 21

def source_type
  @source_type
end

Class Method Details

.from_api(data) ⇒ GenericSource

Build a generic source object from an API response hash.

Parameters:

  • data (Hash)

    source payload returned by the API.

Returns:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vector_amp/source.rb', line 47

def self.from_api(data)
  hash = normalize_hash(data)
  GenericSource.new(
    id: hash["id"],
    source_type: hash.fetch("source_type"),
    name: hash.fetch("name"),
    description: hash["description"],
    config: hash.fetch("config", {}),
    metadata: hash["metadata"]
  )
end

.normalize_hash(value) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/vector_amp/source.rb', line 96

def self.normalize_hash(value)
  case value
  when Hash
    value.each_with_object({}) { |(key, item), memo| memo[key.to_s] = item }
  else
    {}
  end
end

Instance Method Details

#[](key) ⇒ Object?

Read a source attribute by string or symbol key.

Parameters:

  • key (String, Symbol)

    attribute name.

Returns:

  • (Object, nil)


62
63
64
# File 'lib/vector_amp/source.rb', line 62

def [](key)
  to_h[key.to_sym] || to_h[key.to_s]
end

#inspectObject



92
93
94
# File 'lib/vector_amp/source.rb', line 92

def inspect
  "#<#{self.class} id=#{id.inspect} source_type=#{source_type.inspect} name=#{name.inspect}>"
end

#to_create_bodyHash

Convert this source to an API create-source request body.

Returns:

  • (Hash)


82
83
84
85
86
87
88
89
90
# File 'lib/vector_amp/source.rb', line 82

def to_create_body
  Utils.compact_hash(
    source_type: source_type,
    name: name,
    description: description,
    config: config,
    metadata: 
  )
end

#to_hHash Also known as: to_hash

Convert this source to a hash including the id when present.

Returns:

  • (Hash)


68
69
70
71
72
73
74
75
76
77
# File 'lib/vector_amp/source.rb', line 68

def to_h
  Utils.compact_hash(
    id: id,
    source_type: source_type,
    name: name,
    description: description,
    config: config,
    metadata: 
  )
end