ft 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/bin/ft +36 -0
  2. data/ft.gemspec +24 -0
  3. data/lib/ft.rb +33 -0
  4. data/test/cli.rb +49 -0
  5. data/test/ft.rb +18 -0
  6. metadata +93 -0
data/bin/ft ADDED
@@ -0,0 +1,36 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require "clap"
5
+ require "terminal-table/import"
6
+ require File.expand_path("../lib/ft", File.dirname(__FILE__))
7
+
8
+ Clap.run ARGV,
9
+ "query" => -> {
10
+ format = nil
11
+
12
+ args = Clap.run ARGV[1..-1],
13
+ "-f" => -> f { format = f }
14
+
15
+ ft = FusionTables::Connection.new
16
+
17
+ begin
18
+ result = ft.query(args[0])
19
+ rescue FusionTables::Error => e
20
+ $stderr.puts(e.message)
21
+ exit 1
22
+ end
23
+
24
+ format ||= "table" if $stdout.tty?
25
+
26
+ if format == "table"
27
+ result << Array.new(result[0].size) if result.size == 1
28
+ puts table(*result)
29
+ else
30
+ CSV do |csv|
31
+ result.each do |row|
32
+ csv << row
33
+ end
34
+ end
35
+ end
36
+ }
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "ft"
3
+ s.version = "0.0.1"
4
+ s.summary = "Low-level interface to Google's Fusion Tables + CLI tool"
5
+ s.authors = ["Damian Janowski"]
6
+ s.email = ["djanowski@dimaion.com"]
7
+ s.homepage = "http://github.com/djanowski/ft"
8
+
9
+ s.executables.push("ft")
10
+
11
+ s.add_dependency("clap")
12
+ s.add_dependency("terminal-table")
13
+ s.add_dependency("net-http-persistent")
14
+
15
+ s.files = Dir[
16
+ "*.gemspec",
17
+ "LICENSE",
18
+ "README*",
19
+ "Rakefile",
20
+ "bin/*",
21
+ "lib/**/*",
22
+ "test/**/*",
23
+ ]
24
+ end
@@ -0,0 +1,33 @@
1
+ require "csv"
2
+ require "net/http/persistent"
3
+
4
+ class FusionTables
5
+ Error = Class.new(RuntimeError)
6
+
7
+ class Connection
8
+ URL = URI.parse("http://tables.googlelabs.com/api/query")
9
+
10
+ def http
11
+ @http ||= Net::HTTP::Persistent.new("fusiontables").tap do |http|
12
+ http.headers["Content-Type"] = "application/x-www-form-urlencoded"
13
+ end
14
+ end
15
+
16
+ def query(sql)
17
+ url = URL.dup
18
+
19
+ url.query = "sql=#{URI.escape(sql)}"
20
+ res = http.request(url)
21
+
22
+ case res
23
+ when Net::HTTPOK
24
+ CSV.parse(res.body.force_encoding(Encoding::UTF_8))
25
+ when Net::HTTPBadRequest
26
+ message = CGI.unescapeHTML(res.body[%r[<title>(.*)</title>]i, 1])
27
+ raise Error.new("#{message}. SQL was: #{sql}")
28
+ else
29
+ raise "Got #{res.class}: #{res.body}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ require "cutest"
2
+ require "shellwords"
3
+ require "open3"
4
+
5
+ ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
6
+
7
+ def root(*args)
8
+ File.join(ROOT, *args)
9
+ end
10
+
11
+ def ft(*args)
12
+ sh("ruby #{root "bin/ft"} #{Shellwords.join args}")
13
+ end
14
+
15
+ def sh(cmd)
16
+ Open3.capture3(cmd)
17
+ end
18
+
19
+ test "query" do |conn|
20
+ out, err, _ = ft("query", "SELECT 'Name', 'Order' FROM 1310767")
21
+
22
+ assert err.empty?
23
+
24
+ lines = out.split("\n")
25
+
26
+ assert_equal lines[0], "Name,Order"
27
+ assert_equal lines[1], "rake,1"
28
+ assert_equal lines[2], "rack,2"
29
+ end
30
+
31
+ test "query -f table" do |conn|
32
+ out, err, _ = ft("query", "-f", "table", "SELECT 'Name', 'Order' FROM 1310767")
33
+
34
+ assert err.empty?
35
+
36
+ lines = out.split("\n")
37
+
38
+ assert_equal lines[1], "| Name | Order |"
39
+ assert_equal lines[3], "| rake | 1 |"
40
+ assert_equal lines[4], "| rack | 2 |"
41
+ end
42
+
43
+ test "errors" do |conn|
44
+ out, err, status = ft("query", "SELECT foo FROM 1310767")
45
+
46
+ assert out.empty?
47
+ assert_equal err, "Column `foo' does not exist. SQL was: SELECT foo FROM 1310767\n"
48
+ assert_equal status.exitstatus, 1
49
+ end
@@ -0,0 +1,18 @@
1
+ require "cutest"
2
+ require "./lib/ft"
3
+
4
+ setup do
5
+ FusionTables::Connection.new
6
+ end
7
+
8
+ test "SELECT from public tables" do |conn|
9
+ result = conn.query("SELECT Name FROM 1310767")
10
+
11
+ assert_equal result[0][0], "Name"
12
+ end
13
+
14
+ test "raises errors" do |conn|
15
+ assert_raise FusionTables::Error do
16
+ conn.query("SELECT foo FROM 1310767")
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ft
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Damian Janowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-18 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: clap
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: net-http-persistent
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description:
50
+ email:
51
+ - djanowski@dimaion.com
52
+ executables:
53
+ - ft
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - ft.gemspec
60
+ - bin/ft
61
+ - lib/ft.rb
62
+ - test/cli.rb
63
+ - test/ft.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/djanowski/ft
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.6.2
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Low-level interface to Google's Fusion Tables + CLI tool
92
+ test_files: []
93
+