Class: GoogleFT::Table
- Inherits:
-
Object
- Object
- GoogleFT::Table
- Defined in:
- lib/google-ft/table.rb,
lib/google-ft/table/column.rb,
lib/google-ft/table/permission.rb
Defined Under Namespace
Classes: Column, Permission
Instance Attribute Summary collapse
-
#columns ⇒ Object
Accessors.
-
#description ⇒ Object
Accessors.
-
#exportable ⇒ Object
Accessors.
-
#id ⇒ Object
Accessors.
-
#name ⇒ Object
Accessors.
-
#permissions ⇒ Object
Returns the value of attribute permissions.
-
#token ⇒ Object
Accessors.
Class Method Summary collapse
-
.get_table_by_id(table_id, token = nil) ⇒ Object
Get a table by it’s ID and return a table object.
-
.show_tables(token) ⇒ Object
Show a list of tables, requires the authentication token.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete a table.
-
#initialize(args = {}) ⇒ Table
constructor
Create a new table object.
-
#insert(rows) ⇒ Object
Insert rows into a table.
-
#post_args ⇒ Object
Return the JSON string of our table arguments.
-
#save ⇒ Object
Save this table to Google.
-
#set_permissions(permission) ⇒ Object
Set permissions for a table.
Constructor Details
#initialize(args = {}) ⇒ Table
Create a new table object.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/google-ft/table.rb', line 60 def initialize(args = {}) # Clean-up args. args = args.symbolize.delete_if {|k,v| ![:id, :name, :columns, :description, :exportable, :token].include?(k)} # Create nicely formated columns. self.columns = args[:columns].nil? ? [] : args[:columns].each.collect do |col| name = col[:name] || col['name'] type = col[:type] || col['type'] GoogleFT::Table::Column.new(name, type) end # Set other attributes. self.token = args[:token] self.id = args[:id] self.name = args[:name].gsub(/[^a-zA-Z0-9_\-]/, '_') self.description = args[:description] || '' self.exportable = args[:exportable].nil? ? false : args[:exportable] end |
Instance Attribute Details
#columns ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def columns @columns end |
#description ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def description @description end |
#exportable ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def exportable @exportable end |
#id ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def id @id end |
#name ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def name @name end |
#permissions ⇒ Object
Returns the value of attribute permissions.
57 58 59 |
# File 'lib/google-ft/table.rb', line 57 def @permissions end |
#token ⇒ Object
Accessors.
56 57 58 |
# File 'lib/google-ft/table.rb', line 56 def token @token end |
Class Method Details
.get_table_by_id(table_id, token = nil) ⇒ Object
Get a table by it’s ID and return a table object.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/google-ft/table.rb', line 32 def get_table_by_id(table_id, token = nil) args = { :uri => "#{$FT_BASE_URI}/#{table_id}", :method => 'get' } # Add the auth token if it was provided. args[:headers] = {'Authorization' => "Bearer #{token}"} unless token.nil? # Get and parse results. result = GoogleFT.get_and_parse_response(args) GoogleFT::Table.new( :token => token, :id => result[:tableId], :name => result[:name], :columns => result[:columns], :description => result[:description], :exportable => result[:isExportable] ) end |
.show_tables(token) ⇒ Object
Show a list of tables, requires the authentication token.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/google-ft/table.rb', line 8 def show_tables(token) args = { :uri => "#{$FT_BASE_URI}", :method => 'get', :headers => { 'Authorization' => "Bearer #{token}" } } result = GoogleFT.get_and_parse_response(args) return [] if result[:items].nil? result[:items].each.collect do |table| table = table.symbolize GoogleFT::Table.new( :token => token, :id => table[:tableId], :name => table[:name], :columns => table[:columns], :description => table[:description], :exportable => table[:isExportable] ) end end |
Instance Method Details
#delete ⇒ Object
Delete a table.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/google-ft/table.rb', line 80 def delete args = { :uri => "#{$FT_BASE_URI}/#{self.id}", :headers => { 'Authorization' => "Bearer #{self.token}", }, :data => '', :method => 'delete' } GoogleFT.get_and_parse_response(args) true end |
#insert(rows) ⇒ Object
Insert rows into a table.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/google-ft/table.rb', line 141 def insert(rows) # Get the SQL-ish statement from arg hash. inserts = [] # Go through each row. rows.each do |row| # Get all of the column/value pairs. columns = [] values = [] row.each do |column,value| columns.push(column) values.push(value) end # Add this insert line. inserts.push("INSERT INTO #{self.id} (#{columns.join(',')}) VALUES (#{values.each.collect {|v| GoogleFT.to_google_ft_format(v)}.join(',')});") end # Post the insert's to Google. args = { :uri => 'https://www.googleapis.com/fusiontables/v1/query', :headers => { 'Authorization' => "Bearer #{self.token}" }, :method => 'post', :data => "sql=#{inserts.join("\n")}" } GoogleFT.get_and_parse_response(args) end |
#post_args ⇒ Object
Return the JSON string of our table arguments.
Used for creating and updating tables.
131 132 133 134 135 136 137 138 |
# File 'lib/google-ft/table.rb', line 131 def post_args { :name => self.name, :columns => self.columns.each.collect {|col| {:name => col.name, :type => col.type}}, :isExportable => self.exportable, :description => self.description }.to_json end |
#save ⇒ Object
Save this table to Google.
Used for creating or updating tables.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/google-ft/table.rb', line 95 def save # If ID exists, we are updating a table, # otherwise, we are creating it. method = self.id.nil? ? 'post' : 'put' uri = 'https://www.googleapis.com/fusiontables/v1/tables' uri += "/#{self.id}" unless self.id.nil? args = { :uri => uri, :headers => { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{self.token}" }, :data => post_args, :method => method } result = GoogleFT.get_and_parse_response(args) self.id = result[:tableId] result end |
#set_permissions(permission) ⇒ Object
Set permissions for a table.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/google-ft/table.rb', line 116 def () args = { :uri => "https://www.googleapis.com/drive/v2/files/#{self.id}/permissions", :headers => { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{self.token}" }, :method => 'post', :data => .post_args } GoogleFT.get_and_parse_response(args) end |