Class: FusionTablesV2
- Inherits:
-
Object
- Object
- FusionTablesV2
- Defined in:
- lib/fusion_tables_v2.rb
Defined Under Namespace
Constant Summary collapse
- API =
Google::Apis::FusiontablesV2
- AVAILABLE_COLUMN_TYPES =
['DATETIME', 'LOCATION', 'NUMBER', 'STRING']
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#key_path ⇒ Object
readonly
Returns the value of attribute key_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_column(column_id: nil, validate_data: false, format_pattern: 'NONE', name:, type:, table:) ⇒ Object
baseColumn object Optional identifier of the base column.
- #build_table(table_name, &block) ⇒ Object
- #create_table(table, &block) ⇒ Object
- #drop_table(table_id) ⇒ Object
- #drop_table_by(name:) ⇒ Object
- #get_authorization(key_path) ⇒ Object
- #get_table(table_id) ⇒ Object
- #get_table_by(name:) ⇒ Object
- #import_rows(table_id, file:, delimiter: ",", content_type: 'application/octet-stream', start_line: 0) ⇒ Object
-
#initialize(key_path) ⇒ FusionTablesV2
constructor
A new instance of FusionTablesV2.
- #list_tables ⇒ Object
- #tables_hash ⇒ Object
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 = (key_path) client. = @key_path = key_path @authorization = @client = client end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
58 59 60 |
# File 'lib/fusion_tables_v2.rb', line 58 def @authorization end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
58 59 60 |
# File 'lib/fusion_tables_v2.rb', line 58 def client @client end |
#key_path ⇒ Object (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":
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
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 (key_path) ENV['GOOGLE_APPLICATION_CREDENTIALS'] = key_path scopes = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/fusiontables'] = Google::Auth.get_application_default(scopes) .fetch_access_token! 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_tables ⇒ Object
85 86 87 |
# File 'lib/fusion_tables_v2.rb', line 85 def list_tables @client.list_tables end |
#tables_hash ⇒ Object
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 |