fusion_tables 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,10 @@ fusion_tables
4
4
 
5
5
  This gem lets you easily interact with [Google Fusion Tables API](http://www.google.com/fusiontables/Home) from your Ruby application via a plain SQL interface, or an object orientated interface.
6
6
 
7
-
7
+ v0.4 updates
8
+ ------------
9
+ Note that v0.4 onwards of this gem returns geometry as GeoJSON rather than KML by default.
10
+
8
11
 
9
12
  Demo and examples
10
13
  ------------------
@@ -39,7 +42,8 @@ require 'fusion_tables'
39
42
 
40
43
  # Connect to service
41
44
  @ft = GData::Client::FusionTables.new
42
- @ft.clientlogin(username, password)
45
+ @ft.clientlogin(username, password)
46
+ @ft.set_api_key(api_key) # obtained from the google api console
43
47
 
44
48
 
45
49
  # 1. SQL interface
@@ -109,6 +113,7 @@ Known Issues
109
113
  * The Google gdata_19 gem conflicts with the GData2 gem. Only current fix is to uninstall GData2.
110
114
  * You have to make a table public before you can display it on a map. This can only be done via FT web interface.
111
115
 
116
+
112
117
  Note on Patches/Pull Requests
113
118
  ------------------------------
114
119
 
@@ -118,7 +123,7 @@ Note on Patches/Pull Requests
118
123
  future version unintentionally.
119
124
  * Commit, do not mess with rakefile, version, or history.
120
125
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
121
- * Send me a pull request. Bonus points for topic branches.
126
+ * Send me a pull request.
122
127
 
123
128
 
124
129
 
@@ -135,3 +140,4 @@ Largely based on Tom Verbeure's [work for MTBGuru](http://code.google.com/p/mtbg
135
140
  * jmannau
136
141
  * tomykaira
137
142
  * fallanic
143
+ * derekeder
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["Simon Tokumine", "Tom Verbeure"]
13
13
  gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
14
  gem.add_dependency "gdata_19", ">= 1.1.2"
15
+ gem.add_dependency "json", ">= 1.7.6"
15
16
  gem.files.exclude 'pkg'
16
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
18
  end
@@ -44,7 +45,7 @@ task :test => :check_dependencies
44
45
 
45
46
  task :default => :test
46
47
 
47
- require 'rake/rdoctask'
48
+ require 'rdoc/task'
48
49
  Rake::RDocTask.new do |rdoc|
49
50
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
51
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
@@ -1,75 +1,66 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{fusion_tables}
8
- s.version = "0.3.2"
7
+ s.name = "fusion_tables"
8
+ s.version = "0.4.0"
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{2012-11-20}
13
- s.description = %q{A simple Google Fusion Tables API wrapper. Supports bulk inserts and most API functions}
14
- s.email = %q{simon@tinypla.net}
12
+ s.date = "2013-01-14"
13
+ s.description = "A simple Google Fusion Tables API wrapper. Supports bulk inserts and most API functions"
14
+ s.email = "simon@tinypla.net"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.md"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
- ".gitignore",
21
- "CHANGELOG",
22
- "LICENSE",
23
- "README.md",
24
- "Rakefile",
25
- "TODO.md",
26
- "VERSION",
27
- "examples/boris_bikes.rb",
28
- "examples/compare_tweets.rb",
29
- "examples/credentials.example.yml",
30
- "fusion_tables.gemspec",
31
- "lib/fusion_tables.rb",
32
- "lib/fusion_tables/client/fusion_tables.rb",
33
- "lib/fusion_tables/data/data.rb",
34
- "lib/fusion_tables/data/table.rb",
35
- "lib/fusion_tables/ext/fusion_tables.rb",
36
- "test/README",
37
- "test/helper.rb",
38
- "test/test_client.rb",
39
- "test/test_config.yml.sample",
40
- "test/test_ext.rb",
41
- "test/test_sql.rb",
42
- "test/test_table.rb"
43
- ]
44
- s.homepage = %q{http://github.com/tokumine/fusion_tables}
45
- s.rdoc_options = ["--charset=UTF-8"]
46
- s.require_paths = ["lib"]
47
- s.rubygems_version = %q{1.3.6}
48
- s.summary = %q{Google Fusion Tables API wrapper}
49
- s.test_files = [
20
+ "CHANGELOG",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "TODO.md",
25
+ "VERSION",
26
+ "examples/boris_bikes.rb",
27
+ "examples/compare_tweets.rb",
28
+ "examples/credentials.example.yml",
29
+ "fusion_tables.gemspec",
30
+ "lib/fusion_tables.rb",
31
+ "lib/fusion_tables/client/fusion_tables.rb",
32
+ "lib/fusion_tables/data/data.rb",
33
+ "lib/fusion_tables/data/table.rb",
34
+ "lib/fusion_tables/ext/fusion_tables.rb",
35
+ "test/README",
50
36
  "test/helper.rb",
51
- "test/test_client.rb",
52
- "test/test_ext.rb",
53
- "test/test_sql.rb",
54
- "test/test_table.rb",
55
- "examples/boris_bikes.rb",
56
- "examples/compare_tweets.rb"
37
+ "test/test_client.rb",
38
+ "test/test_config.yml.sample",
39
+ "test/test_ext.rb",
40
+ "test/test_sql.rb",
41
+ "test/test_table.rb"
57
42
  ]
43
+ s.homepage = "http://github.com/tokumine/fusion_tables"
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = "1.8.24"
46
+ s.summary = "Google Fusion Tables API wrapper"
58
47
 
59
48
  if s.respond_to? :specification_version then
60
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
61
49
  s.specification_version = 3
62
50
 
63
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
52
  s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
65
53
  s.add_runtime_dependency(%q<gdata_19>, [">= 1.1.2"])
54
+ s.add_runtime_dependency(%q<json>, [">= 1.7.6"])
66
55
  else
67
56
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
68
57
  s.add_dependency(%q<gdata_19>, [">= 1.1.2"])
58
+ s.add_dependency(%q<json>, [">= 1.7.6"])
69
59
  end
70
60
  else
71
61
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
72
62
  s.add_dependency(%q<gdata_19>, [">= 1.1.2"])
63
+ s.add_dependency(%q<json>, [">= 1.7.6"])
73
64
  end
74
65
  end
75
66
 
data/lib/fusion_tables.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'csv'
2
2
  require 'gdata'
3
+ require 'json'
3
4
  require 'fusion_tables/client/fusion_tables'
4
5
  require 'fusion_tables/ext/fusion_tables'
5
6
  require 'fusion_tables/data/data'
@@ -16,29 +16,31 @@ module GData
16
16
  module Client
17
17
  class FusionTables < Base
18
18
 
19
- SERVICE_URL = "https://tables.googlelabs.com/api/query"
19
+ #SERVICE_URL = "https://www.google.com/fusiontables/api/query"
20
+ SERVICE_URL = "https://www.googleapis.com/fusiontables/v1/query"
20
21
  DATATYPES = %w(number string location datetime)
21
22
 
22
23
  def initialize(options = {})
23
24
  options[:clientlogin_service] ||= 'fusiontables'
24
- options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded' }
25
+ options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded'}
25
26
  super(options)
26
27
  end
27
28
 
28
29
  def sql_encode(sql)
30
+ # puts sql
29
31
  "sql=" + CGI::escape(sql)
30
32
  end
31
33
 
32
34
  def sql_get(sql)
33
- resp = self.get(SERVICE_URL + "?" + sql_encode(sql))
35
+ resp = self.get(SERVICE_URL + "?" + sql_encode(sql) + "&key=#{@api_key}")
34
36
  end
35
37
 
36
38
  def sql_post(sql)
37
- resp = self.post(SERVICE_URL, sql_encode(sql))
39
+ resp = self.post(SERVICE_URL, sql_encode(sql) + "&key=#{@api_key}")
38
40
  end
39
41
 
40
42
  def sql_put(sql)
41
- resp = self.put(SERVICE_URL, sql_encode(sql))
43
+ resp = self.put(SERVICE_URL, sql_encode(sql) + "&key=#{@api_key}")
42
44
  end
43
45
 
44
46
  # Overrides auth_handler= so if the authentication changes,
@@ -19,7 +19,26 @@ module GData
19
19
  # Helper method to run FT SQL and return FT data object
20
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
22
+ json_resp = JSON.parse(self.send(http_req, sql).body)
23
+
24
+ # probably a much cleaner way to do this
25
+ rows = json_resp['rows']
26
+ columns = json_resp['columns']
27
+ correlated = []
28
+ (0...rows.length).each do|row|
29
+ h = {}
30
+ (0...columns.length).each do|column|
31
+ h[columns[column].gsub(/\s+/, "_").downcase.to_sym] = rows[row][column]
32
+ end
33
+ correlated << h
34
+ end
35
+ # puts correlated.inspect
36
+
37
+ correlated
38
+ end
39
+
40
+ def set_api_key(api_key)
41
+ @api_key = api_key
23
42
  end
24
43
 
25
44
  # Show a list of fusion tables
@@ -67,7 +86,8 @@ module GData
67
86
  raise "unknown column type" if resp.body == "Unknown column type."
68
87
 
69
88
  # construct table object and return
70
- table_id = resp.body.split("\n")[1].chomp
89
+ json_resp = JSON.parse(resp.body)
90
+ table_id = json_resp['rows'][0][0]
71
91
  table = GData::Client::FusionTables::Table.new(self, :table_id => table_id, :name => table_name)
72
92
  table.get_headers
73
93
  table
@@ -96,7 +116,7 @@ module GData
96
116
  delete_count = 0
97
117
  ids.each do |id|
98
118
  resp = self.sql_post("DROP TABLE #{id}")
99
- delete_count += 1 if resp.body.strip.downcase == 'ok'
119
+ delete_count += 1 if resp.status_code == 200
100
120
  end
101
121
  delete_count
102
122
  end
data/test/helper.rb CHANGED
@@ -31,4 +31,8 @@ class Test::Unit::TestCase
31
31
  def table_name
32
32
  @config_file['table_name']
33
33
  end
34
+
35
+ def api_key
36
+ @config_file['api_key']
37
+ end
34
38
  end
@@ -1,3 +1,4 @@
1
1
  username: test
2
2
  password: test
3
- table_name: "test_table"
3
+ table_name: "test_table"
4
+ api_key: "your API key (don't forget to enable FT in your Google API console)"
data/test/test_ext.rb CHANGED
@@ -7,6 +7,7 @@ class TestExt < Test::Unit::TestCase
7
7
  init_config
8
8
  @ft = GData::Client::FusionTables.new
9
9
  @ft.clientlogin(username, password)
10
+ @ft.set_api_key(api_key)
10
11
  end
11
12
 
12
13
  teardown do
@@ -27,7 +28,7 @@ class TestExt < Test::Unit::TestCase
27
28
 
28
29
  should "accept symbol for name and type" do
29
30
  @table = @ft.create_table "test_table", [{:name => :test_col, :type => :string }]
30
- first_column = @table.describe[0]
31
+ first_column = @table.describe.first
31
32
  assert_equal 'test_col', first_column[:name]
32
33
  assert_equal 'string', first_column[:type]
33
34
  end
data/test/test_sql.rb CHANGED
@@ -7,6 +7,7 @@ class TestTable < Test::Unit::TestCase
7
7
  init_config
8
8
  @ft = GData::Client::FusionTables.new
9
9
  @ft.clientlogin(username, password)
10
+ @ft.set_api_key(api_key)
10
11
  @table = @ft.create_table "test", [{:name => 'firstname', :type => 'string'},
11
12
  {:name => 'phone', :type => 'number'},
12
13
  {:name => 'dob', :type => 'datetime'},
@@ -69,13 +70,13 @@ class TestTable < Test::Unit::TestCase
69
70
  # should "be able to query geographic data" do
70
71
  # @table = @ft.create_table "test", [{:name => 'name', :type => 'string'},
71
72
  # {:name => 'geo', :type => 'location'}]
72
- #
73
+
73
74
  # @ft.execute "INSERT INTO #{@table.id} (name, geo) VALUES ('tokyo', '35.6894 139.6917');
74
75
  # INSERT INTO #{@table.id} (name, geo) VALUES ('osaka', '34.6937 135.5021');
75
76
  # INSERT INTO #{@table.id} (name, geo) VALUES ('fukuoka', '33.5903 130.4017');
76
77
  # INSERT INTO #{@table.id} (name, geo) VALUES ('kyoto', '35.0116 135.7680');
77
78
  # INSERT INTO #{@table.id} (name, geo) VALUES ('nagoya', '35.1814 136.9063');"
78
- #
79
+
79
80
  # # get cities nearest to Nara
80
81
  # res = @ft.execute "SELECT * FROM #{@table.id} ORDER BY ST_DISTANCE(geo, LATLNG(35.6894,139.6917)) LIMIT 10"
81
82
  # puts res.inspect
data/test/test_table.rb CHANGED
@@ -7,6 +7,7 @@ class TestTable < Test::Unit::TestCase
7
7
  init_config
8
8
  @ft = GData::Client::FusionTables.new
9
9
  @ft.clientlogin(username, password)
10
+ @ft.set_api_key(api_key)
10
11
  @table = @ft.create_table "test", [{:name => 'firstname', :type => 'string'},
11
12
  {:name => 'phone', :type => 'number'},
12
13
  {:name => 'dob', :type => 'datetime'},
@@ -73,7 +74,7 @@ class TestTable < Test::Unit::TestCase
73
74
  }
74
75
 
75
76
  @table.insert data
76
- assert_equal [{:firstname=>"Person-0", :phone=>"12", :dob=>"08-10-2010 20:15:01", :house=>"<Point><coordinates>1,1,0</coordinates></Point>"}, {:firstname=>"Person-1", :phone=>"12", :dob=>"08-10-2010 20:15:01", :house=>"<Point><coordinates>1,1,0</coordinates></Point>"}], @table.select
77
+ assert_equal [{:firstname=>"Person-0", :phone=>"12", :dob=>"08-10-2010 20:15:01", :house=>{"geometry"=>{"type"=>"Point", "coordinates"=>[1.0, 1.0, 0.0]}}}, {:firstname=>"Person-1", :phone=>"12", :dob=>"08-10-2010 20:15:01", :house=>{"geometry"=>{"type"=>"Point", "coordinates"=>[1.0, 1.0, 0.0]}}}], @table.select
77
78
  end
78
79
 
79
80
  should "be able to truncate all rows and start again" do
metadata CHANGED
@@ -1,60 +1,74 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: fusion_tables
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 3
8
- - 2
9
- version: 0.3.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Simon Tokumine
13
9
  - Tom Verbeure
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2012-11-20 00:00:00 +00:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
13
+ date: 2013-01-14 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: thoughtbot-shoulda
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
31
23
  type: :development
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
34
32
  name: gdata_19
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 1.1.2
39
+ type: :runtime
35
40
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- segments:
41
- - 1
42
- - 1
43
- - 2
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
44
46
  version: 1.1.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.6
45
55
  type: :runtime
46
- version_requirements: *id002
47
- description: A simple Google Fusion Tables API wrapper. Supports bulk inserts and most API functions
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 1.7.6
63
+ description: A simple Google Fusion Tables API wrapper. Supports bulk inserts and
64
+ most API functions
48
65
  email: simon@tinypla.net
49
66
  executables: []
50
-
51
67
  extensions: []
52
-
53
- extra_rdoc_files:
68
+ extra_rdoc_files:
54
69
  - LICENSE
55
70
  - README.md
56
- files:
57
- - .gitignore
71
+ files:
58
72
  - CHANGELOG
59
73
  - LICENSE
60
74
  - README.md
@@ -77,41 +91,28 @@ files:
77
91
  - test/test_ext.rb
78
92
  - test/test_sql.rb
79
93
  - test/test_table.rb
80
- has_rdoc: true
81
94
  homepage: http://github.com/tokumine/fusion_tables
82
95
  licenses: []
83
-
84
96
  post_install_message:
85
- rdoc_options:
86
- - --charset=UTF-8
87
- require_paths:
97
+ rdoc_options: []
98
+ require_paths:
88
99
  - lib
89
- required_ruby_version: !ruby/object:Gem::Requirement
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- segments:
94
- - 0
95
- version: "0"
96
- required_rubygems_version: !ruby/object:Gem::Requirement
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- segments:
101
- - 0
102
- version: "0"
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
103
112
  requirements: []
104
-
105
113
  rubyforge_project:
106
- rubygems_version: 1.3.6
114
+ rubygems_version: 1.8.24
107
115
  signing_key:
108
116
  specification_version: 3
109
117
  summary: Google Fusion Tables API wrapper
110
- test_files:
111
- - test/helper.rb
112
- - test/test_client.rb
113
- - test/test_ext.rb
114
- - test/test_sql.rb
115
- - test/test_table.rb
116
- - examples/boris_bikes.rb
117
- - examples/compare_tweets.rb
118
+ test_files: []
data/.gitignore DELETED
@@ -1,30 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## Aptana
17
- .project
18
-
19
- ## PROJECT::GENERAL
20
- coverage
21
- rdoc
22
- examples/credentials.yml
23
- pkg/*
24
-
25
- ## PROJECT::TEST
26
- test/test_config.yml
27
-
28
-
29
- ## PROJECT::SPECIFIC
30
-