class TPX_2_2::Exchange

An object representing a TPX file, holding all associated data.

Constants

DEFAULT_MAX_FILESIZE
ELEMENT_LISTS
MANDATORY_ATTRIBUTES

Public Class Methods

import(input_hash) click to toggle source

Imports a tpx file from a given hash.

@param [Hash] input_hash The hash to import.

# File lib/tpx/2_2/exchange.rb, line 58
def import(input_hash)
  Validator.validate!(input_hash)
  self.new(input_hash)
end
import_file(filename) click to toggle source

Imports a tpx file from a given filename.

@param [String] filename The filename to import.

# File lib/tpx/2_2/exchange.rb, line 42
def import_file(filename)
  h = Oj.load_file(filename)
  import(h)
end
import_s(str) click to toggle source

Imports a tpx file from a given json string.

@param [String] str The string of json to import.

# File lib/tpx/2_2/exchange.rb, line 50
def import_s(str)
  h = Oj.load(str)
  import(h)
end
init_file(filename) click to toggle source

Initialize a TPX_2_2::Exchange from a given filename.

@param [String] filename The filename containing the exchange. This does not call Validator.validate!

# File lib/tpx/2_2/exchange.rb, line 34
def init_file(filename)
  h = Oj.load_file(filename)
  self.new(h)
end
new(input_hash) click to toggle source

Overrides the default initialize to validate the input data.

@param [Hash] input_hash The input hash.

@return [DataModel] The returned object.

Calls superclass method TPX_2_2::DataModel::new
# File lib/tpx/2_2/exchange.rb, line 69
def initialize(input_hash)
  input_hash = ::HashWithIndifferentAccess.new(input_hash)
  input_hash[:schema_version_s] = TPX_2_2::CURRENT_SCHEMA_VERSION
  input_hash[:last_updated_t] ||= Time.now.utc.to_i
  input_hash[:observable_dictionary_c_array] ||= []
  input_hash[:source_description_s] ||= ''

  has_one_list = false
  ELEMENT_LISTS.keys.each do |list_key|
    has_list = false
    has_manifest = false
    if input_hash.has_key?(list_key) && input_hash.has_key?(ELEMENT_LISTS[list_key][2])
      raise ValidationError, "Only one of #{list_key} or #{ELEMENT_LISTS[list_key][2]} should be supplied to #{self.class}#initialize input_hash."
    elsif input_hash.has_key?(list_key)
      has_one_list = true
      input_hash[list_key] = ELEMENT_LISTS[list_key][0].new(input_hash[list_key])
    elsif input_hash.has_key?(ELEMENT_LISTS[list_key][2])
      has_one_list = true
    end
  end

  unless has_one_list
    raise ValidationError, "At list one list element (#{ELEMENT_LISTS.keys}) should be supplied to #{self.class}#initialize."
  end

  super input_hash
end

Public Instance Methods

<<(element) click to toggle source

Overrides default << method to add data to the exchange. Checks that added elements are of the correct class.

@param element [Object] Element to add to the exchange.

@return [Object] The updated Exchange.

# File lib/tpx/2_2/exchange.rb, line 103
def <<(element)
  if element.is_a? Array
    element.each do |e|
      self << e
    end
    return self
  end

  element_type_supported = false

  ELEMENT_LISTS.each do |list_key, list_def|
    list_type, list_element, list_manifest_key, list_manifest_file_type = *list_def
    if element.class == list_element
      self[list_key] ||= list_type.new([])
      self[list_key] << element
      element_type_supported = true
    end
  end

  unless element_type_supported
    raise ValidationError, "Element provided to #{self.class}#<< has invalid object type (#{element.class})!"
  end

  return self
end
_to_h_scrub(h) click to toggle source

Returns the exchange with empty elements deleted.

@param [Hash] h The hash from which to scrub empty elements.

@return [Object] The hash with deleted empty elements.

# File lib/tpx/2_2/exchange.rb, line 134
def _to_h_scrub(h)
  h_scrub = h.dup
  [:observable_dictionary_c_array, :element_observable_c_array, :asn_c_array, :collection_c_array].each do |key|
    h_scrub.delete(key) if h_scrub.has_key? key && h_scrub[key].blank?
  end
  return h_scrub
end
to_h() click to toggle source

Alias for _to_h_scrub.

Calls superclass method TPX_2_2::DataModel#to_h
# File lib/tpx/2_2/exchange.rb, line 143
def to_h
  _to_h_scrub(super)
end
to_hash() click to toggle source

Alias for _to_h_scrub.

Calls superclass method
# File lib/tpx/2_2/exchange.rb, line 148
def to_hash
  _to_h_scrub(super)
end
to_tpx_file(filepath, options={}) click to toggle source

Exports the current exchange to a tpx file.

@param [String] filepath The file to be exported. @param [Hash] options Additional options to be passed to the json exporter.

# File lib/tpx/2_2/exchange.rb, line 156
def to_tpx_file(filepath, options={})
  data = (manifest_files_count > 1) ? self.to_manifest(filepath) : self
  Oj.to_file(filepath, data, options.merge({mode: :compat}))
  @manifest_files_count = nil
end

Private Instance Methods

enumerable?(container) click to toggle source
# File lib/tpx/2_2/exchange.rb, line 165
def enumerable?(container)
  container.respond_to? :each
end
estimated_tpx_file_size() click to toggle source
# File lib/tpx/2_2/exchange.rb, line 177
def estimated_tpx_file_size
  Oj.dump(self).size
end
manifest_files_count() click to toggle source
# File lib/tpx/2_2/exchange.rb, line 173
def manifest_files_count
  @manifest_files_count ||= estimated_tpx_file_size / DEFAULT_MAX_FILESIZE
end
serializable?(element) click to toggle source
# File lib/tpx/2_2/exchange.rb, line 169
def serializable?(element)
  element.respond_to? :to_hash
end
split_section(section) click to toggle source
# File lib/tpx/2_2/exchange.rb, line 181
def split_section(section)
  split_count = section.size / manifest_files_count
  section.each_slice(split_count > 1 ? split_count : 1).to_a
end
to_manifest(manifest_file_path) click to toggle source
# File lib/tpx/2_2/exchange.rb, line 198
def to_manifest(manifest_file_path)
  exchange_hash = self.to_hash
  manifest_hash = {}

  keys = ['observable_dictionary_c_array', 'element_observable_c_array', 'asn_c_array', 'collection_c_array']
  path = File.dirname(manifest_file_path)

  exchange_hash.each do |key, val|
    manifest_hash[key] = val unless keys.include?(key)
  end

  ELEMENT_LISTS.each do |list_key, list_def|
    list_type, list_element, list_manifest_key, list_manifest_file_type = *list_def
    if exchange_hash.has_key? list_key.to_s
      manifest_hash[list_manifest_key.to_s] = to_manifest_section(exchange_hash[list_key.to_s], list_manifest_file_type.to_s, path) unless exchange_hash[list_key.to_s].empty?
    end
  end

  manifest_hash
end
to_manifest_section(exchange_section, manifest_file_type, path) click to toggle source
# File lib/tpx/2_2/exchange.rb, line 186
def to_manifest_section(exchange_section, manifest_file_type, path)
  subsections = split_section(exchange_section)
  files = []

  subsections.each_with_index do |item, index|
    # save subsection as TPX file
    files << "#{manifest_file_type}_#{index + 1}.json"
    Oj.to_file(File.join(path, files.last), item)
  end
  files
end