class FusionTablesV2

Constants

API
AVAILABLE_COLUMN_TYPES

Attributes

authorization[R]
client[R]
key_path[R]

Public Class Methods

connect(key_path: nil) click to toggle source
# File lib/fusion_tables_v2.rb, line 60
def self.connect(key_path: nil)
  if key_path
    new(key_path)
  else
    false
  end
end
new(key_path) click to toggle source
# File lib/fusion_tables_v2.rb, line 68
def initialize(key_path)
  client = API::FusiontablesService.new
  authorization = get_authorization(key_path)
  client.authorization = authorization
  @key_path = key_path
  @authorization = authorization
  @client = client
end

Public Instance Methods

build_column(column_id: nil, validate_data: false, format_pattern: 'NONE', name:, type:, table:) click to toggle source

baseColumn object Optional identifier of the base column. If present, this column is derived from the specified base column.

baseColumn.columnId integer The ID of the column in the base table from which this column is derived.

baseColumn.tableIndex integer Offset to the entry in the list of base tables in the table definition.

columnId integer Identifier for the column. kind string The kind of item this is. For column, this is always fusiontables#column.

name string Required name of the column. writable type string Required type of the column. Type can be “NUMBER”, “STRING”, “LOCATION”, or “DATETIME”. Acceptable values are:

"DATETIME":
"LOCATION":
"NUMBER":
"STRING":
# File lib/fusion_tables_v2.rb, line 164
def build_column(column_id: nil, validate_data: false, format_pattern: 'NONE', name:, type:, table:)
  column_id ||= table.columns.size
  raise ArgumentError.new('Column ID must be Integer') unless column_id.is_a?(Integer)
  raise ArgumentError.new("Column type must be #{ AVAILABLE_COLUMN_TYPES }") unless AVAILABLE_COLUMN_TYPES.include?(type)
  new_column = Column.new()
  new_column.name = name
  new_column.type = type
  new_column.kind = 'fusiontables#column'
  new_column.validate_data = validate_data
  new_column.format_pattern = format_pattern
  new_column
end
build_table(table_name, &block) click to toggle source
# File lib/fusion_tables_v2.rb, line 89
def build_table(table_name, &block)
  new_table = Table.build(name: table_name, db: self)
  if block_given?
    new_table.instance_eval(&block)
  end
  new_table
end
create_table(table, &block) click to toggle source
# File lib/fusion_tables_v2.rb, line 97
def create_table(table, &block)
  if table.is_a?(Table)
    @client.insert_table(table)
  elsif table.is_a?(String)
    if block_given?
      builded_table = build_table(table, &block)
      @client.insert_table(builded_table)
    else
      raise ArgumentError.new('Please specify migration block')
    end
  else
    raise ArgumentError.new('Table must be String or Table object')
  end
end
drop_table(table_id) click to toggle source
# File lib/fusion_tables_v2.rb, line 112
def drop_table(table_id)
  client.delete_table(table_id)
  true
rescue Google::Apis::ClientError
  "Table doesn't exist"
end
drop_table_by(name:) click to toggle source
# File lib/fusion_tables_v2.rb, line 119
def drop_table_by(name:)
  tables = tables_hash 
  raise ArgumentError.new('There are more than 1 table with this name') if tables.count { |el| el[:name] == name } > 1
  table_id = tables.find { |el| el[:name] == name }[:id]
  drop_table(table_id)
end
get_authorization(key_path) click to toggle source
# File lib/fusion_tables_v2.rb, line 77
def get_authorization(key_path)
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = key_path
  scopes = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/fusiontables']
  authorization = Google::Auth.get_application_default(scopes)
  authorization.fetch_access_token!
  authorization
end
get_table(table_id) click to toggle source
# File lib/fusion_tables_v2.rb, line 126
def get_table(table_id)
  client.get_table(table_id)
rescue Google::Apis::ClientError
  "Table doesn't exist"
end
get_table_by(name:) click to toggle source
# File lib/fusion_tables_v2.rb, line 132
def get_table_by(name:)
  tables = tables_hash 
  if tables.count { |el| el[:name] == name } > 1
    client.list_tables.items.select {|t| t.name == 'my_table' }
  elsif tables.count { |el| el[:name] == name } == 1
    client.get_table(tables.find { |el| el[:name] == name }[:id])
  else
    "Table doesn't exist"
  end
end
import_rows(table_id, file:, delimiter: ",", content_type: 'application/octet-stream', start_line: 0) click to toggle source
# File lib/fusion_tables_v2.rb, line 143
def import_rows(table_id, file:, delimiter: ",", content_type: 'application/octet-stream', start_line: 0)
  client.import_rows(table_id, delimiter: delimiter, upload_source: file, content_type: content_type, start_line: start_line)
end
list_tables() click to toggle source
# File lib/fusion_tables_v2.rb, line 85
def list_tables
  @client.list_tables
end
tables_hash() click to toggle source
# File lib/fusion_tables_v2.rb, line 147
def tables_hash
  items = client.list_tables.items || []
  items.map { |t| { name: t.name, id: t.table_id } }
end