Class: GData::Client::FusionTables
- Inherits:
-
Base
- Object
- Base
- GData::Client::FusionTables
- Defined in:
- lib/fusion_tables/data/data.rb,
lib/fusion_tables/data/table.rb,
lib/fusion_tables/ext/fusion_tables.rb,
lib/fusion_tables/client/fusion_tables.rb
Defined Under Namespace
Constant Summary collapse
- SERVICE_URL =
"http://tables.googlelabs.com/api/query"
- DATATYPES =
%w(number string location datetime)
Instance Method Summary collapse
-
#auth_handler=(handler) ⇒ Object
Overrides auth_handler= so if the authentication changes, the session cookie is cleared.
-
#create_table(table_name, columns) ⇒ Object
Create a new table.
-
#drop(options) ⇒ Object
Drops Fusion Tables.
-
#execute(sql) ⇒ Object
Helper method to run FT SQL and return FT data object.
-
#initialize(options = {}) ⇒ FusionTables
constructor
A new instance of FusionTables.
-
#make_request(method, url, body = '', retries = 10) ⇒ Object
Overrides make_request to handle 500 redirects with a session cookie.
-
#prepare_headers ⇒ Object
Custom prepare_headers to include the session cookie if it exists.
-
#show_tables ⇒ Object
Show a list of fusion tables.
- #sql_encode(sql) ⇒ Object
- #sql_get(sql) ⇒ Object
- #sql_post(sql) ⇒ Object
- #sql_put(sql) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FusionTables
Returns a new instance of FusionTables.
22 23 24 25 26 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 22 def initialize( = {}) [:clientlogin_service] ||= 'fusiontables' [:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded' } super() end |
Instance Method Details
#auth_handler=(handler) ⇒ Object
Overrides auth_handler= so if the authentication changes, the session cookie is cleared.
46 47 48 49 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 46 def auth_handler=(handler) @session_cookie = nil return super(handler) end |
#create_table(table_name, columns) ⇒ Object
Create a new table. Return the corresponding table
Columns specified as [=> ‘my_col_name’, :type => ‘my_type’]
Type must be one of:
-
number
-
string
-
location
-
datetime
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fusion_tables/ext/fusion_tables.rb', line 46 def create_table(table_name, columns) # Sanity check name table_name = table_name.strip.gsub(/ /,'_') # ensure all column types are valid columns.each do |col| if !DATATYPES.include? col[:type].downcase raise ArgumentError, "Ensure input types are: 'number', 'string', 'location' or 'datetime'" end end # generate sql fields = columns.map{ |col| "'#{col[:name]}': #{col[:type].upcase}" }.join(", ") sql = "CREATE TABLE #{table_name} (#{fields})" # create table resp = self.sql_post(sql) raise "unknown column type" if resp.body == "Unknown column type." # construct table object and return table_id = resp.body.split("\n")[1].chomp.to_i table = GData::Client::FusionTables::Table.new(self, :table_id => table_id, :name => table_name) table.get_headers table end |
#drop(options) ⇒ Object
Drops Fusion Tables
options can be:
-
an integer for single drop
-
array of integers for multi drop
-
a regex against table_name for flexible multi_drop
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/fusion_tables/ext/fusion_tables.rb', line 81 def drop() # collect ids ids = [] ids << if .class == Integer || .class == String || Fixnum ids = if .class == Array if .class == Regexp tables = show_tables ids = tables.map { |table| table.id if =~ table.name }.compact end # drop tables delete_count = 0 ids.each do |id| resp = self.sql_post("DROP TABLE #{id}") delete_count += 1 if resp.body.strip.downcase == 'ok' end delete_count end |
#execute(sql) ⇒ Object
Helper method to run FT SQL and return FT data object
20 21 22 23 |
# File 'lib/fusion_tables/ext/fusion_tables.rb', line 20 def execute(sql) http_req = sql.upcase.match(/^(DESCRIBE|SHOW|SELECT)/) ? :sql_get : :sql_post GData::Client::FusionTables::Data.parse(self.send(http_req, sql)).body end |
#make_request(method, url, body = '', retries = 10) ⇒ Object
Overrides make_request to handle 500 redirects with a session cookie.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 52 def make_request(method, url, body = '', retries = 10) begin response = super(method, url, body) rescue GData::Client::ServerError => e if e.response.status_code == 500 and retries > 0 sleep_time = 11 - retries sleep sleep_time # <= Fusion tables has rate of 5 calls per second. Be nice, get longer @session_cookie = e.response.headers['set-cookie'] return self.make_request(method, url, body, retries - 1) else return e.response end end end |
#prepare_headers ⇒ Object
Custom prepare_headers to include the session cookie if it exists
68 69 70 71 72 73 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 68 def prepare_headers if @session_cookie @headers['cookie'] = @session_cookie end super end |
#show_tables ⇒ Object
Show a list of fusion tables
26 27 28 29 30 31 32 33 |
# File 'lib/fusion_tables/ext/fusion_tables.rb', line 26 def show_tables data = self.execute "SHOW TABLES" data.inject([]) do |x, row| x << GData::Client::FusionTables::Table.new(self, row) x end end |
#sql_encode(sql) ⇒ Object
28 29 30 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 28 def sql_encode(sql) "sql=" + CGI::escape(sql) end |
#sql_get(sql) ⇒ Object
32 33 34 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 32 def sql_get(sql) resp = self.get(SERVICE_URL + "?" + sql_encode(sql)) end |
#sql_post(sql) ⇒ Object
36 37 38 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 36 def sql_post(sql) resp = self.post(SERVICE_URL, sql_encode(sql)) end |
#sql_put(sql) ⇒ Object
40 41 42 |
# File 'lib/fusion_tables/client/fusion_tables.rb', line 40 def sql_put(sql) resp = self.put(SERVICE_URL, sql_encode(sql)) end |