Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fusion Tables v1 API implementation #21

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -44,7 +44,7 @@ task :test => :check_dependencies

task :default => :test

require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

Expand Down
1 change: 1 addition & 0 deletions lib/fusion_tables.rb
@@ -1,5 +1,6 @@
require 'csv'
require 'gdata'
require 'json'
require 'fusion_tables/client/fusion_tables'
require 'fusion_tables/ext/fusion_tables'
require 'fusion_tables/data/data'
Expand Down
12 changes: 7 additions & 5 deletions lib/fusion_tables/client/fusion_tables.rb
Expand Up @@ -16,29 +16,31 @@ module GData
module Client
class FusionTables < Base

SERVICE_URL = "https://tables.googlelabs.com/api/query"
#SERVICE_URL = "https://www.google.com/fusiontables/api/query"
SERVICE_URL = "https://www.googleapis.com/fusiontables/v1/query"
DATATYPES = %w(number string location datetime)

def initialize(options = {})
options[:clientlogin_service] ||= 'fusiontables'
options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded' }
options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded'}
super(options)
end

def sql_encode(sql)
# puts sql
"sql=" + CGI::escape(sql)
end

def sql_get(sql)
resp = self.get(SERVICE_URL + "?" + sql_encode(sql))
resp = self.get(SERVICE_URL + "?" + sql_encode(sql) + "&key=#{@api_key}")
end

def sql_post(sql)
resp = self.post(SERVICE_URL, sql_encode(sql))
resp = self.post(SERVICE_URL, sql_encode(sql) + "&key=#{@api_key}")
end

def sql_put(sql)
resp = self.put(SERVICE_URL, sql_encode(sql))
resp = self.put(SERVICE_URL, sql_encode(sql) + "&key=#{@api_key}")
end

# Overrides auth_handler= so if the authentication changes,
Expand Down
26 changes: 23 additions & 3 deletions lib/fusion_tables/ext/fusion_tables.rb
Expand Up @@ -19,7 +19,26 @@ class FusionTables < Base
# Helper method to run FT SQL and return FT data object
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
json_resp = JSON.parse(self.send(http_req, sql).body)

# probably a much cleaner way to do this
rows = json_resp['rows']
columns = json_resp['columns']
correlated = []
(0...rows.length).each do|row|
h = {}
(0...columns.length).each do|column|
h[columns[column].gsub(/\s+/, "_").downcase.to_sym] = rows[row][column]
end
correlated << h
end
# puts correlated.inspect

correlated
end

def set_api_key(api_key)
@api_key = api_key
end

# Show a list of fusion tables
Expand Down Expand Up @@ -67,7 +86,8 @@ def create_table(table_name, columns)
raise "unknown column type" if resp.body == "Unknown column type."

# construct table object and return
table_id = resp.body.split("\n")[1].chomp
json_resp = JSON.parse(resp.body)
table_id = json_resp['rows'][0][0]
table = GData::Client::FusionTables::Table.new(self, :table_id => table_id, :name => table_name)
table.get_headers
table
Expand Down Expand Up @@ -96,7 +116,7 @@ def drop(options)
delete_count = 0
ids.each do |id|
resp = self.sql_post("DROP TABLE #{id}")
delete_count += 1 if resp.body.strip.downcase == 'ok'
delete_count += 1 if resp.status_code == 200
end
delete_count
end
Expand Down
4 changes: 4 additions & 0 deletions test/helper.rb
Expand Up @@ -31,4 +31,8 @@ def password
def table_name
@config_file['table_name']
end

def api_key
@config_file['api_key']
end
end
1 change: 1 addition & 0 deletions test/test_config.yml.sample
@@ -1,3 +1,4 @@
username: test
password: test
api_key: xxxxx
table_name: "test_table"
3 changes: 2 additions & 1 deletion test/test_ext.rb
Expand Up @@ -7,6 +7,7 @@ class TestExt < Test::Unit::TestCase
init_config
@ft = GData::Client::FusionTables.new
@ft.clientlogin(username, password)
@ft.set_api_key(api_key)
end

teardown do
Expand All @@ -27,7 +28,7 @@ class TestExt < Test::Unit::TestCase

should "accept symbol for name and type" do
@table = @ft.create_table "test_table", [{:name => :test_col, :type => :string }]
first_column = @table.describe[0]
first_column = @table.describe.first
assert_equal 'test_col', first_column[:name]
assert_equal 'string', first_column[:type]
end
Expand Down
1 change: 1 addition & 0 deletions test/test_sql.rb
Expand Up @@ -7,6 +7,7 @@ class TestTable < Test::Unit::TestCase
init_config
@ft = GData::Client::FusionTables.new
@ft.clientlogin(username, password)
@ft.set_api_key(api_key)
@table = @ft.create_table "test", [{:name => 'firstname', :type => 'string'},
{:name => 'phone', :type => 'number'},
{:name => 'dob', :type => 'datetime'},
Expand Down
1 change: 1 addition & 0 deletions test/test_table.rb
Expand Up @@ -7,6 +7,7 @@ class TestTable < Test::Unit::TestCase
init_config
@ft = GData::Client::FusionTables.new
@ft.clientlogin(username, password)
@ft.set_api_key(api_key)
@table = @ft.create_table "test", [{:name => 'firstname', :type => 'string'},
{:name => 'phone', :type => 'number'},
{:name => 'dob', :type => 'datetime'},
Expand Down