Class: FusionTablesV2

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

Defined Under Namespace

Classes: Column, Table

Constant Summary collapse

API =
Google::Apis::FusiontablesV2
AVAILABLE_COLUMN_TYPES =
['DATETIME', 'LOCATION', 'NUMBER', 'STRING']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_path) ⇒ FusionTablesV2

Returns a new instance of FusionTablesV2.



68
69
70
71
72
73
74
75
# 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

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



58
59
60
# File 'lib/fusion_tables_v2.rb', line 58

def authorization
  @authorization
end

#clientObject (readonly)

Returns the value of attribute client.



58
59
60
# File 'lib/fusion_tables_v2.rb', line 58

def client
  @client
end

#key_pathObject (readonly)

Returns the value of attribute key_path.



58
59
60
# File 'lib/fusion_tables_v2.rb', line 58

def key_path
  @key_path
end

Class Method Details

.connect(key_path: nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/fusion_tables_v2.rb', line 60

def self.connect(key_path: nil)
  if key_path
    new(key_path)
  else
    false
  end
end

Instance Method Details

#build_column(column_id: nil, validate_data: false, format_pattern: 'NONE', name:, type:, table:) ⇒ Object

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":

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
175
# 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) ⇒ Object



89
90
91
92
93
94
95
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



112
113
114
115
116
117
# 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:) ⇒ Object

Raises:

  • (ArgumentError)


119
120
121
122
123
124
# 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) ⇒ Object



77
78
79
80
81
82
83
# 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) ⇒ Object



126
127
128
129
130
# 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:) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object



143
144
145
# 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_tablesObject



85
86
87
# File 'lib/fusion_tables_v2.rb', line 85

def list_tables
  @client.list_tables
end

#tables_hashObject



147
148
149
150
# 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