Class: Mode::Sdk::Table

Inherits:
Object
  • Object
show all
Extended by:
WarehouseUtil
Defined in:
lib/mode/sdk/table.rb

Overview

Represents a table stored in the Mode public data warehouse

Examples:

table = Mode::Sdk::Table.new('sf_film_locations')
table.columns = [
  { name: 'movie_title',  type: 'string' },
  { name: 'release_year', type: 'integer' },
  { name: 'location',     type: 'string' }
]
table.create

Defined Under Namespace

Classes: InvalidError

Constant Summary collapse

NAME_PATTERN =

Pattern to determine whether a String is a valid table name

/\A[a-z][a-z0-9_]{2,62}[a-z0-9]\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WarehouseUtil

normalize_name

Constructor Details

#initialize(name, options = {}) ⇒ Mode::Sdk::Table

Construct a new Table instance

Parameters:

  • name (String)

    valid table name

  • options (optional, Hash) (defaults to: {})

    hash of options

Options Hash (options):

  • :owner (String)

    the username or alias of the Mode account associated with the table (defaults to currently authenticated account)


48
49
50
51
# File 'lib/mode/sdk/table.rb', line 48

def initialize(name, options = {})
  @name    = name
  @options = options
end

Instance Attribute Details

#columnsArray<Hash>

an array of hashes defining column names and types

Returns:

  • (Array<Hash>)

    the current value of columns


26
27
28
# File 'lib/mode/sdk/table.rb', line 26

def columns
  @columns
end

#descriptionString

a brief table description

Returns:

  • (String)

    the current value of description


26
27
28
# File 'lib/mode/sdk/table.rb', line 26

def description
  @description
end

#nameString (readonly)

the name of the table

Returns:

  • (String)

    the current value of name


26
27
28
# File 'lib/mode/sdk/table.rb', line 26

def name
  @name
end

#upload_tokenString

the token of the Mode::Sdk::Upload containing the CSV with which to populate the table

Returns:

  • (String)

    the current value of upload_token


26
27
28
# File 'lib/mode/sdk/table.rb', line 26

def upload_token
  @upload_token
end

Instance Method Details

#collection_pathString

The REST collection path for this table

Returns:

  • (String)

    the path


113
114
115
# File 'lib/mode/sdk/table.rb', line 113

def collection_path
  "/api/#{owner}/tables"
end

#column_setMode::Sdk::ColumnSet

Returns the column set

Returns:


127
128
129
# File 'lib/mode/sdk/table.rb', line 127

def column_set
  @column_set ||= Mode::Sdk::ColumnSet.new(columns)
end

#create(options = {}) ⇒ Mode::Sdk::Client::Response

Create the table in Mode

Parameters:

  • options (optional, Hash) (defaults to: {})

    hash of options

Options Hash (options):

  • :clean (true, false)

    whether to clean table data

Returns:


70
71
72
# File 'lib/mode/sdk/table.rb', line 70

def create(options = {})
  upsert(:create, options)
end

#exists?true, false

Determine whether a table with this name already exists for the given Mode account

Returns:

  • (true, false)

    whether the table exists


58
59
60
# File 'lib/mode/sdk/table.rb', line 58

def exists?
  Mode::Sdk::Client.head(resource_path, expect: [200, 404]).code == 200
end

#full_nameString

The full name of the table, including owner schema

Returns:

  • (String)

    the full name


105
106
107
# File 'lib/mode/sdk/table.rb', line 105

def full_name
  [owner, name].join('.')
end

#replace(options = {}) ⇒ Mode::Sdk::Client::Response

Replace an existing table in Mode

Parameters:

  • options (optional, Hash) (defaults to: {})

    hash of options

Options Hash (options):

  • :clean (true, false)

    whether to clean table data

Returns:


82
83
84
# File 'lib/mode/sdk/table.rb', line 82

def replace(options = {})
  upsert(:replace, options)
end

#resource_pathString

The REST resource path for this table

Returns:

  • (String)

    the path


121
122
123
# File 'lib/mode/sdk/table.rb', line 121

def resource_path
  [collection_path, name].join('/')
end