fusion_tables 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/README.md +3 -0
- data/VERSION +1 -1
- data/fusion_tables.gemspec +3 -4
- data/lib/fusion_tables/data/table.rb +38 -11
- data/lib/fusion_tables/ext/fusion_tables.rb +39 -36
- data/test/test_ext.rb +34 -22
- data/test/test_sql.rb +4 -0
- data/test/test_table.rb +4 -0
- metadata +3 -4
- data/.document +0 -5
data/.gitignore
CHANGED
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/fusion_tables.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{fusion_tables}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Simon Tokumine", "Tom Verbeure"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2012-11-20}
|
13
13
|
s.description = %q{A simple Google Fusion Tables API wrapper. Supports bulk inserts and most API functions}
|
14
14
|
s.email = %q{simon@tinypla.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
21
|
-
".gitignore",
|
20
|
+
".gitignore",
|
22
21
|
"CHANGELOG",
|
23
22
|
"LICENSE",
|
24
23
|
"README.md",
|
@@ -111,21 +111,12 @@ module GData
|
|
111
111
|
data.inject([]) do |ar,h|
|
112
112
|
ret = {}
|
113
113
|
h.each do |key, value|
|
114
|
-
|
115
|
-
#empty string for nils
|
116
|
-
ret["'#{key.to_s}'"] = "''"
|
117
|
-
else
|
118
|
-
ret["'#{key.to_s}'"] = case get_datatype(key)
|
119
|
-
when "number" then "#{value}"
|
120
|
-
when "datetime" then "'#{value.strftime("%m-%d-%Y %H:%M:%S")}'"
|
121
|
-
else "'#{value.gsub(/\\/, '\&\&').gsub(/'/, "''")}'"
|
122
|
-
end
|
123
|
-
end
|
114
|
+
ret["'#{key}'"] = encode_value(get_datatype(key), value)
|
124
115
|
end
|
125
116
|
ar << ret
|
126
117
|
ar
|
127
118
|
end
|
128
|
-
end
|
119
|
+
end
|
129
120
|
|
130
121
|
#
|
131
122
|
# Returns datatype of given column name
|
@@ -138,6 +129,42 @@ module GData
|
|
138
129
|
end
|
139
130
|
raise ArgumentError, "The column doesn't exist"
|
140
131
|
end
|
132
|
+
|
133
|
+
private
|
134
|
+
def encode_value_as_numeric(value)
|
135
|
+
value.to_s
|
136
|
+
end
|
137
|
+
|
138
|
+
def encode_value_as_datetime(value)
|
139
|
+
encode_value_as_string(value.strftime("%m-%d-%Y %H:%M:%S"))
|
140
|
+
end
|
141
|
+
|
142
|
+
def encode_value_as_string(value)
|
143
|
+
quoted = value.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''")
|
144
|
+
"'#{quoted}'"
|
145
|
+
end
|
146
|
+
|
147
|
+
def encode_value type, value
|
148
|
+
if value.nil?
|
149
|
+
#empty string for nils
|
150
|
+
"''"
|
151
|
+
else
|
152
|
+
case type
|
153
|
+
when "number"
|
154
|
+
encode_value_as_numeric(value)
|
155
|
+
when "datetime"
|
156
|
+
encode_value_as_datetime(value)
|
157
|
+
when "location"
|
158
|
+
if value.is_a?(Numeric)
|
159
|
+
encode_value_as_numeric(value)
|
160
|
+
else
|
161
|
+
encode_value_as_string(value)
|
162
|
+
end
|
163
|
+
else
|
164
|
+
encode_value_as_string(value)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
141
168
|
end
|
142
169
|
end
|
143
170
|
end
|
@@ -14,89 +14,92 @@
|
|
14
14
|
|
15
15
|
module GData
|
16
16
|
module Client
|
17
|
-
class FusionTables < Base
|
18
|
-
|
17
|
+
class FusionTables < Base
|
18
|
+
|
19
19
|
# Helper method to run FT SQL and return FT data object
|
20
|
-
def execute(sql)
|
20
|
+
def execute(sql)
|
21
21
|
http_req = sql.upcase.match(/^(DESCRIBE|SHOW|SELECT)/) ? :sql_get : :sql_post
|
22
|
-
GData::Client::FusionTables::Data.parse(self.send(http_req, sql)).body
|
23
|
-
end
|
24
|
-
|
22
|
+
GData::Client::FusionTables::Data.parse(self.send(http_req, sql)).body
|
23
|
+
end
|
24
|
+
|
25
25
|
# Show a list of fusion tables
|
26
|
-
def show_tables
|
26
|
+
def show_tables
|
27
27
|
data = self.execute "SHOW TABLES"
|
28
|
-
|
28
|
+
|
29
29
|
data.inject([]) do |x, row|
|
30
|
-
x << GData::Client::FusionTables::Table.new(self, row)
|
30
|
+
x << GData::Client::FusionTables::Table.new(self, row)
|
31
31
|
x
|
32
|
-
end
|
32
|
+
end
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Create a new table. Return the corresponding table
|
36
|
-
#
|
36
|
+
#
|
37
37
|
# Columns specified as [{:name => 'my_col_name', :type => 'my_type'}]
|
38
38
|
#
|
39
39
|
# Type must be one of:
|
40
|
-
#
|
41
|
-
# * number
|
42
|
-
# * string
|
40
|
+
#
|
41
|
+
# * number
|
42
|
+
# * string
|
43
43
|
# * location
|
44
44
|
# * datetime
|
45
45
|
#
|
46
46
|
def create_table(table_name, columns)
|
47
|
-
|
47
|
+
|
48
48
|
# Sanity check name
|
49
49
|
table_name = table_name.strip.gsub(/ /,'_')
|
50
|
-
|
50
|
+
|
51
51
|
# ensure all column types are valid
|
52
52
|
columns.each do |col|
|
53
|
+
col[:name] = col[:name].to_s
|
54
|
+
col[:type] = col[:type].to_s
|
55
|
+
|
53
56
|
if !DATATYPES.include? col[:type].downcase
|
54
57
|
raise ArgumentError, "Ensure input types are: 'number', 'string', 'location' or 'datetime'"
|
55
|
-
end
|
58
|
+
end
|
56
59
|
end
|
57
|
-
|
60
|
+
|
58
61
|
# generate sql
|
59
62
|
fields = columns.map{ |col| "'#{col[:name]}': #{col[:type].upcase}" }.join(", ")
|
60
63
|
sql = "CREATE TABLE #{table_name} (#{fields})"
|
61
|
-
|
62
|
-
# create table
|
64
|
+
|
65
|
+
# create table
|
63
66
|
resp = self.sql_post(sql)
|
64
67
|
raise "unknown column type" if resp.body == "Unknown column type."
|
65
|
-
|
66
|
-
# construct table object and return
|
67
|
-
table_id = resp.body.split("\n")[1].chomp
|
68
|
+
|
69
|
+
# construct table object and return
|
70
|
+
table_id = resp.body.split("\n")[1].chomp
|
68
71
|
table = GData::Client::FusionTables::Table.new(self, :table_id => table_id, :name => table_name)
|
69
72
|
table.get_headers
|
70
73
|
table
|
71
74
|
end
|
72
|
-
|
75
|
+
|
73
76
|
# Drops Fusion Tables
|
74
77
|
#
|
75
78
|
# options can be:
|
76
79
|
#
|
77
|
-
# * an integer for single drop
|
80
|
+
# * an integer for single drop
|
78
81
|
# * array of integers for multi drop
|
79
|
-
# * a regex against table_name for flexible multi_drop
|
82
|
+
# * a regex against table_name for flexible multi_drop
|
80
83
|
#
|
81
84
|
def drop(options)
|
82
85
|
# collect ids
|
83
86
|
ids = []
|
84
87
|
ids << options if options.class == Integer || options.class == String || Fixnum
|
85
88
|
ids = options if options.class == Array
|
86
|
-
|
89
|
+
|
87
90
|
if options.class == Regexp
|
88
91
|
tables = show_tables
|
89
92
|
ids = tables.map { |table| table.id if options =~ table.name }.compact
|
90
|
-
end
|
91
|
-
|
92
|
-
# drop tables
|
93
|
+
end
|
94
|
+
|
95
|
+
# drop tables
|
93
96
|
delete_count = 0
|
94
|
-
ids.each do |id|
|
95
|
-
resp = self.sql_post("DROP TABLE #{id}")
|
96
|
-
delete_count += 1 if resp.body.strip.downcase == 'ok'
|
97
|
+
ids.each do |id|
|
98
|
+
resp = self.sql_post("DROP TABLE #{id}")
|
99
|
+
delete_count += 1 if resp.body.strip.downcase == 'ok'
|
97
100
|
end
|
98
|
-
delete_count
|
99
|
-
end
|
101
|
+
delete_count
|
102
|
+
end
|
100
103
|
end
|
101
104
|
end
|
102
105
|
end
|
data/test/test_ext.rb
CHANGED
@@ -1,58 +1,70 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestExt < Test::Unit::TestCase
|
4
|
-
|
4
|
+
|
5
5
|
context "The Fusion Tables helper functions" do
|
6
|
-
setup do
|
6
|
+
setup do
|
7
7
|
init_config
|
8
|
-
@ft = GData::Client::FusionTables.new
|
8
|
+
@ft = GData::Client::FusionTables.new
|
9
9
|
@ft.clientlogin(username, password)
|
10
10
|
end
|
11
|
-
|
12
|
-
|
11
|
+
|
12
|
+
teardown do
|
13
|
+
@ft.drop(@table.id) if @table and @table.id
|
14
|
+
end
|
15
|
+
|
13
16
|
should "raise ArgumentError if supply unknown types to it" do
|
14
17
|
assert_raise ArgumentError do
|
15
18
|
@ft.create_table "test table", [{:name => "test_col", :type => "billys birthday" }]
|
16
19
|
end
|
17
|
-
end
|
18
|
-
|
20
|
+
end
|
21
|
+
|
19
22
|
should "let you create a table if you get everything right" do
|
20
|
-
table = @ft.create_table "test_table", [{:name => "test_col", :type => "string" }]
|
21
|
-
assert_equal GData::Client::FusionTables::Table, table.class
|
22
|
-
@
|
23
|
+
@table = @ft.create_table "test_table", [{:name => "test_col", :type => "string" }]
|
24
|
+
assert_equal GData::Client::FusionTables::Table, @table.class
|
25
|
+
assert @table.id.is_a? String
|
26
|
+
end
|
27
|
+
|
28
|
+
should "accept symbol for name and type" do
|
29
|
+
@table = @ft.create_table "test_table", [{:name => :test_col, :type => :string }]
|
30
|
+
first_column = @table.describe[0]
|
31
|
+
assert_equal 'test_col', first_column[:name]
|
32
|
+
assert_equal 'string', first_column[:type]
|
23
33
|
end
|
24
|
-
|
34
|
+
|
25
35
|
should "correct your table name to a certain degree on create" do
|
26
|
-
table = @ft.create_table "test table", [{:name => "test col", :type => "string" }]
|
27
|
-
assert_equal "test_table", table.name
|
28
|
-
@ft.drop(table.id)
|
36
|
+
@table = @ft.create_table "test table", [{:name => "test col", :type => "string" }]
|
37
|
+
assert_equal "test_table", @table.name
|
29
38
|
end
|
30
|
-
|
39
|
+
|
31
40
|
should "return you a list of your fusion tables" do
|
41
|
+
@table = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
32
42
|
resp = @ft.show_tables
|
33
|
-
|
43
|
+
assert resp.any? { |t| t.name == 'test_table' }
|
34
44
|
end
|
35
|
-
|
45
|
+
|
36
46
|
should "be possible to delete a table with an id" do
|
37
47
|
table = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
38
48
|
assert_equal 1, @ft.drop(table.id)
|
39
49
|
end
|
40
|
-
|
50
|
+
|
41
51
|
should "be possible to delete tables with an array of ids" do
|
42
52
|
table1 = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
43
53
|
table2 = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
44
54
|
assert_equal 2, @ft.drop([table1.id, table2.id])
|
45
55
|
end
|
46
|
-
|
56
|
+
|
47
57
|
should "be possible to delete multiple tables with a regex" do
|
48
58
|
table1 = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
49
59
|
table2 = @ft.create_table "test_table", [{:name => "test col", :type => "string" }]
|
50
60
|
assert_equal 2, @ft.drop(/^test_/)
|
51
61
|
end
|
52
|
-
|
62
|
+
|
53
63
|
should "return zero if passed a silly id" do
|
54
|
-
|
55
|
-
|
64
|
+
assert_raise GData::Client::BadRequestError do
|
65
|
+
assert_equal 0, @ft.drop(235243875629384756)
|
66
|
+
end
|
67
|
+
end
|
56
68
|
end
|
57
69
|
|
58
70
|
end
|
data/test/test_sql.rb
CHANGED
@@ -12,6 +12,10 @@ class TestTable < Test::Unit::TestCase
|
|
12
12
|
{:name => 'dob', :type => 'datetime'},
|
13
13
|
{:name => 'house', :type => 'location'}]
|
14
14
|
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
@ft.drop(@table.id)
|
18
|
+
end
|
15
19
|
|
16
20
|
should "be able to SHOW TABLES" do
|
17
21
|
ret = @ft.execute "SHOW TABLES"
|
data/test/test_table.rb
CHANGED
@@ -12,6 +12,10 @@ class TestTable < Test::Unit::TestCase
|
|
12
12
|
{:name => 'dob', :type => 'datetime'},
|
13
13
|
{:name => 'house', :type => 'location'}]
|
14
14
|
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
@ft.drop(@table.id)
|
18
|
+
end
|
15
19
|
|
16
20
|
should "format data and prep for upload" do
|
17
21
|
data = @table.encode [{:firstname => "\\bob's piz\za",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 2
|
9
|
+
version: 0.3.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Simon Tokumine
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-11-20 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -54,7 +54,6 @@ extra_rdoc_files:
|
|
54
54
|
- LICENSE
|
55
55
|
- README.md
|
56
56
|
files:
|
57
|
-
- .document
|
58
57
|
- .gitignore
|
59
58
|
- CHANGELOG
|
60
59
|
- LICENSE
|