fusion_tables_api 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6457a099c117c227e0d8df8c6e0a34fd0a7986e5
4
+ data.tar.gz: 3daee24939615fa42853800d3db11186a168c915
5
+ SHA512:
6
+ metadata.gz: 86c36ae4a06b359428e41f9e69cbcf7e59dcb06fdafa9c37d719e8c09497895d1b6fe8ed61538352f145e53073d4d37093c9295aeb5eff94cb8ae2df2d645fab
7
+ data.tar.gz: 304c0371f04161c141f92333b86997bdb1b42bb35cffe1476ddffdce10917283eafaf8514a69cdcad0c964bd3c84e7f7689f7886f26b5d26abbb45743ef98d30
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_STORE
19
+ *.vim
20
+ *.yml
21
+ ft_script_test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fusion_tables_api.gemspec
4
+ gemspec
data/HISTORY.md ADDED
@@ -0,0 +1,4 @@
1
+ # Version 0.0.1 (04-30-2013)
2
+ * First release
3
+ * Get table list is known to work
4
+ * Truthfully, I was just trying to claim the name for the gem on RubyGems
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Villena
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # FusionTablesAPI
2
+
3
+ This gem allows you to use Google's FusionTables API to create and upload tables
4
+ with a CSV file.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'fusion_tables_api'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install fusion_tables_api
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
31
+
32
+ # License
33
+ Copyright (c) 2013 Michael Villena
34
+
35
+ MIT License
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ "Software"), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
51
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
52
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
53
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
54
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fusion_tables_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fusion_tables_api"
8
+ spec.version = FusionTablesAPI::VERSION
9
+ spec.authors = ["Michael Villena"]
10
+ spec.email = ["mavoir6@gmail.com"]
11
+ spec.description = %q{Create tables in Google Fusion Tables via its API}
12
+ spec.summary = %q{Create tables in Google Fusion Tables via its API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ # Gem dependencies
21
+ spec.add_dependency "google-api-client", "0.6.3"
22
+
23
+ # Development dependencies
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,21 @@
1
+
2
+ require 'fusion_tables_api/version'
3
+ require 'fusion_tables_api/authorization'
4
+ require 'fusion_tables_api/table'
5
+ require 'google/api_client'
6
+ require 'yaml'
7
+
8
+
9
+ class FusionTablesAPI
10
+ GOOGLE_OAUTH_TOKEN_FILE = 'google_access_token.yml'
11
+
12
+ # Need a hash with :client_id, :client_secret or filename that contains the oauth_credentials 'client_id', 'client_secret'
13
+ def initialize(oauth_cred, access_tokens = nil)
14
+ @client = Google::APIClient.new
15
+ @fusion_tables_api = @client.discovered_api('fusiontables')
16
+
17
+ load_authorization_credentials(oauth_cred)
18
+ load_oauth_tokens(access_tokens) if access_tokens
19
+ true
20
+ end
21
+ end
@@ -0,0 +1,66 @@
1
+ class FusionTablesAPI
2
+ ##
3
+ # Set Google API Client authorization credentials
4
+ def set_access_token(access_token)
5
+ @access_token = access_token # Needed to check if access token expires
6
+ @client.authorization.access_token = @access_token
7
+ end
8
+
9
+ def set_refresh_token(refresh_token)
10
+ @client.authorization.refresh_token = refresh_token
11
+ end
12
+
13
+ def store_new_access_token
14
+ @access_token = @client.authorization.access_token
15
+ @oauth_authorization['access_token'] = @access_token
16
+ File.open(GOOGLE_OAUTH_FILENAME, 'w+') { |f| f.write(@oauth_authorization.to_yaml)}
17
+ end
18
+
19
+ # Takes hash or file and loads oauth credentials
20
+ def load_authorization_credentials(cred)
21
+ if cred.is_a?(Hash)
22
+ @client.authorization.client_id = cred[:client_id]
23
+ @client.authorization.client_secret = cred[:client_secret]
24
+ else
25
+ credentials = YAML.load_file(cred)
26
+ @client.authorization.client_id = credentials["client_id"]
27
+ @client.authorization.client_secret = credentials["client_secret"]
28
+ end
29
+ @client.authorization.redirect_uri = "http://localhost/oauth2callback"
30
+ @client.authorization.scope = "https://www.googleapis.com/auth/fusiontables"
31
+ true
32
+ end
33
+
34
+ def load_oauth_tokens(oauth_tokens)
35
+ if oauth_tokens.is_a?(Hash)
36
+ set_access_token(oauth_tokens[:access_token])
37
+ set_refresh_token(oauth_tokens[:refresh_token])
38
+ else
39
+ tokens = YAML.load_file(oauth_tokens)
40
+ set_access_token(tokens['access_token'])
41
+ set_refresh_token(tokens['refresh_token'])
42
+ end
43
+ true
44
+ end
45
+
46
+ # Authorization
47
+ def initial_authorization
48
+ puts "1) Copy and paste the url below into your browser:"
49
+ authorize = @client.authorization.authorization_uri
50
+ puts authorize
51
+ # Launchy.open authorize
52
+ puts ""
53
+
54
+ puts "2) Accept the authorization request from Google in your browser"
55
+ puts "3) Copy and paste the code parameter from the url and enter below:"
56
+ print ">> "
57
+ code = gets.chomp.strip
58
+
59
+ @client.authorization.code = code
60
+ oauth_authorization ||= @client.authorization.fetch_access_token!
61
+
62
+ File.open(GOOGLE_OAUTH_TOKEN_FILE, 'w+') { |f| f.write(oauth_authorization.to_yaml)}
63
+ puts "Google OAuth Tokens written to #{GOOGLE_OAUTH_TOKEN_FILE}"
64
+ true
65
+ end
66
+ end
@@ -0,0 +1,55 @@
1
+ class FusionTablesAPI
2
+ # TODO Might eventually want to create a table class
3
+ ### FusionTables API Table methods ###
4
+
5
+ def get_table_list
6
+ result = @client.execute(api_method: @fusion_tables_api.table.list)
7
+ # if @client.authorization.access_token != @access_token
8
+ # store_new_access_token
9
+ # end
10
+
11
+ jj JSON.parse(result.body)
12
+ JSON.parse(result.body)
13
+ end
14
+
15
+ def create_table(filename)
16
+ csv_file = CSV.read(filename, {headers: true})
17
+ headers = { 'Content-Type' => 'application/json' }
18
+
19
+ column_names = []
20
+ csv_file.headers.each_with_index do |header|
21
+ type = csv_file[0][header].is_i? ? "NUMBER" : "STRING"
22
+ column_names << { "name" => header, "type" => type }
23
+ end
24
+
25
+ request_body = { "columns" => column_names, "isExportable" => false, "name" => File.basename(filename, ".csv") }
26
+ result = @client.execute(api_method: @fusion_tables_api.table.insert, headers: headers, body: request_body.to_json)
27
+ parsed_result = JSON.parse(result.body)
28
+ store_new_access_token if @client.authorization.access_token != @access_token
29
+
30
+ puts "#### \nCreate Table Response:\n#{result.body}\n####\n"
31
+ parsed_result # return
32
+ end
33
+
34
+ def import_rows(filename, table_id, start_line= nil)
35
+ headers = Hash.new
36
+ headers[:content_type] = 'application/octet-stream'
37
+ params = { tableId: table_id, startLine: start_line, isStrict: false } # May need to set isStrict to false
38
+ request_body = File.read(filename)
39
+ import_row_url = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableId/import"
40
+
41
+ result = @client.execute(uri: import_row_url, http_method: :post, body: request_body, headers: headers, parameters: params )
42
+ update_fusion_table_row_count(File.basename(filename, '.csv'), result.data["numRowsReceived"].to_i)
43
+ jj result.data
44
+
45
+ true # return
46
+ end
47
+
48
+ def generate_table(filename, start_line= nil)
49
+ response = create_table(filename)
50
+ import_rows(filename, response["tableId"], start_line)
51
+ end
52
+
53
+ def add_rows_to_existing_table(filename)
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ class FusionTablesAPI
2
+ VERSION = "0.0.2"
3
+ end
data/spec/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fusion_tables_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Villena
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Create tables in Google Fusion Tables via its API
70
+ email:
71
+ - mavoir6@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - HISTORY.md
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - fusion_tables_api.gemspec
83
+ - lib/fusion_tables_api.rb
84
+ - lib/fusion_tables_api/authorization.rb
85
+ - lib/fusion_tables_api/table.rb
86
+ - lib/fusion_tables_api/version.rb
87
+ - spec/.gitkeep
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Create tables in Google Fusion Tables via its API
112
+ test_files:
113
+ - spec/.gitkeep