module Google::Cloud::Bigquery::External

def self.from_gapi gapi

Other tags:
    Private: - Google API Client object.
def self.from_gapi gapi
  external_format = source_format_for gapi.source_uris,
                                      gapi.source_format
  raise ArgumentError, "Unable to determine external table format" if external_format.nil?
  external_class = table_class_for external_format
  external_class.from_gapi gapi
end

def self.from_urls urls, format = nil

Other tags:
    Private: - New External from URLs and format
def self.from_urls urls, format = nil
  external_format = source_format_for urls, format
  raise ArgumentError, "Unable to determine external table format" if external_format.nil?
  external_class = table_class_for external_format
  external_class.new.tap do |e|
    e.gapi.source_uris = Array(urls)
    e.gapi.source_format = external_format
  end
end

def self.source_format_for urls, format

Other tags:
    Private: - Determine source_format from inputs
def self.source_format_for urls, format
  val = {
    "avro"                   => "AVRO",
    "bigtable"               => "BIGTABLE",
    "csv"                    => "CSV",
    "backup"                 => "DATASTORE_BACKUP",
    "datastore"              => "DATASTORE_BACKUP",
    "datastore_backup"       => "DATASTORE_BACKUP",
    "sheets"                 => "GOOGLE_SHEETS",
    "google_sheets"          => "GOOGLE_SHEETS",
    "json"                   => "NEWLINE_DELIMITED_JSON",
    "newline_delimited_json" => "NEWLINE_DELIMITED_JSON",
    "orc"                    => "ORC",
    "parquet"                => "PARQUET"
  }[format.to_s.downcase]
  return val unless val.nil?
  Array(urls).each do |url|
    return "AVRO" if url.end_with? ".avro"
    return "BIGTABLE" if url.start_with? "https://googleapis.com/bigtable/projects/"
    return "CSV" if url.end_with? ".csv"
    return "DATASTORE_BACKUP" if url.end_with? ".backup_info"
    return "GOOGLE_SHEETS" if url.start_with? "https://docs.google.com/spreadsheets/"
    return "NEWLINE_DELIMITED_JSON" if url.end_with? ".json"
    return "PARQUET" if url.end_with? ".parquet"
  end
  nil
end

def self.table_class_for format

Other tags:
    Private: - Determine table class from source_format
def self.table_class_for format
  case format
  when "AVRO"                   then External::AvroSource
  when "BIGTABLE"               then External::BigtableSource
  when "CSV"                    then External::CsvSource
  when "GOOGLE_SHEETS"          then External::SheetsSource
  when "NEWLINE_DELIMITED_JSON" then External::JsonSource
  when "PARQUET"                then External::ParquetSource
  else
    # DATASTORE_BACKUP, ORC
    External::DataSource
  end
end