fusion_tables_api 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 788fd708130a4c637e396d204f146ff45e516532
4
- data.tar.gz: 80fcf67122e80b21dc0444a44d04f2a723fc7ba2
3
+ metadata.gz: 6535c59c29b723d30f3d724554b419edb37355a3
4
+ data.tar.gz: 7d5d43b0346b487ac7350e45b6e6dc03867c57e4
5
5
  SHA512:
6
- metadata.gz: e2bf5ccec320972e4d87ddf6deb95673970702bf24dfcba928df116dcadb0064ba820b95a2d69b2eb5dc6addffebee2306156ffdcdff3ca18d8268a6943f62b8
7
- data.tar.gz: c25eaad1e965cace106a9ec16208f03fdf00b0a161097f66801e8255b40512fde47f882729ecf427e984a31f546c7e6efc483d48f1dc26c9a008d5654c481e98
6
+ metadata.gz: cac9e478a44ff75087134019e3cbb05cd7bc64a3f0818f92515b99a1db28a3089f791b2be06709e4c88e3a135cb30ed73e65fb9ac93037dd39e0ef646da06e22
7
+ data.tar.gz: 5a886e78e9acf7a6a19907e2ad1ac054ab267fe9c08fb855ad233a63751a1cd573d2bc44a58614629bd64fb9f27897134aa3f9588c90b12ff4dbcaa564c4a4fc
data/HISTORY.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Version 0.0.3 (04-30-2013)
2
+ * Basic functionality
3
+ * Can create table
4
+
1
5
  ## Version 0.0.2 (04-30-2013)
2
6
  * Pushed up a small fix
3
7
 
data/README.md CHANGED
@@ -29,13 +29,22 @@ Or install it yourself as:
29
29
  refresh_token: "refresh-token-would-be-useful-too"
30
30
  }
31
31
 
32
- ft = FusionTablesAPI.new(cred, tokens)
33
- ft.get_table_list
32
+ google_api_client_options {
33
+ application_name: "AppName"
34
+ application_version: "1.0awesome"
35
+ }
36
+
37
+ ft = FusionTablesAPI.new(cred, tokens, google_api_client_options)
38
+ jj ft.get_table_list
34
39
 
35
40
  OR
36
41
 
37
- ft = FusionTablesAPI.new("google_oauth_credentials.yml", "google_access_token.yml")
38
- ft.get_table_list
42
+ ft = FusionTablesAPI.new("google_oauth_credentials.yml", "google_access_token.yml", google_api_client_options)
43
+ jj ft.get_table_list
44
+
45
+ Generating a table:
46
+
47
+ ft.generate_table("the_name_of_csv_file.csv")
39
48
 
40
49
  **TODO: Finish this....**
41
50
 
@@ -47,7 +56,7 @@ OR
47
56
  4. Push to the branch (`git push origin my-new-feature`)
48
57
  5. Create new Pull Request
49
58
 
50
- # License
59
+ ## License
51
60
  Copyright (c) 2013 Michael Villena
52
61
 
53
62
  MIT License
@@ -32,7 +32,6 @@ class FusionTablesAPI
32
32
 
33
33
  def load_oauth_tokens(oauth_tokens)
34
34
  if oauth_tokens.is_a?(Hash)
35
- # TODO Need to involve instance variable authorization_tokens
36
35
  set_access_token(oauth_tokens[:access_token])
37
36
  set_refresh_token(oauth_tokens[:refresh_token])
38
37
  else
@@ -44,6 +43,7 @@ class FusionTablesAPI
44
43
  end
45
44
 
46
45
  # Authorization
46
+ # TODO Need to test this out
47
47
  def initial_authorization
48
48
  puts "1) Copy and paste the url below into your browser:"
49
49
  authorize = @client.authorization.authorization_uri
@@ -57,7 +57,7 @@ class FusionTablesAPI
57
57
  code = gets.chomp.strip
58
58
 
59
59
  @client.authorization.code = code
60
- # TODO Need to this one below
60
+ # TODO Need to test this one below
61
61
  @authorization_tokens ||= @client.authorization.fetch_access_token!
62
62
 
63
63
  File.open(GOOGLE_OAUTH_TOKEN_FILE, 'w+') { |f| f.write(oauth_authorization.to_yaml)}
@@ -1,6 +1,6 @@
1
1
  require 'csv'
2
2
 
3
- # TODO May need to change this
3
+ # TODO Will need to change this
4
4
  class String
5
5
  def is_i?
6
6
  !!(self =~/^[-+]?[0-9]+$/)
@@ -13,11 +13,8 @@ class FusionTablesAPI
13
13
 
14
14
  def get_table_list
15
15
  result = @client.execute(api_method: @fusion_tables_api.table.list)
16
- # if @client.authorization.access_token != @access_token
17
- # store_new_access_token
18
- # end
19
-
20
- jj JSON.parse(result.body)
16
+ store_new_access_token if @authorization_tokens &&
17
+ @client.authorization.access_token != @authorization_tokens['access_token']
21
18
  JSON.parse(result.body)
22
19
  end
23
20
 
@@ -33,29 +30,30 @@ class FusionTablesAPI
33
30
 
34
31
  request_body = { "columns" => column_names, "isExportable" => false, "name" => File.basename(filename, ".csv") }
35
32
  result = @client.execute(api_method: @fusion_tables_api.table.insert, headers: headers, body: request_body.to_json)
36
- parsed_result = JSON.parse(result.body)
37
- store_new_access_token if @client.authorization.access_token != @authorization_tokens['access_token']
38
-
39
- puts "#### \nCreate Table Response:\n#{result.body}\n####\n"
40
- parsed_result # return
33
+ store_new_access_token if @authorization_tokens &&
34
+ @client.authorization.access_token != @authorization_tokens['access_token']
35
+ JSON.parse(result.body)
41
36
  end
42
37
 
43
38
  def import_rows(filename, table_id, start_line= 0)
44
39
  headers = Hash.new
45
40
  headers[:content_type] = 'application/octet-stream'
46
- params = { tableId: table_id, startLine: start_line, isStrict: false } # May need to set isStrict to false
41
+ params = {
42
+ tableId: table_id,
43
+ startLine: start_line,
44
+ isStrict: false # May need to set isStrict to false
45
+ }
47
46
  request_body = File.read(filename)
48
47
  import_row_url = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableId/import"
49
48
 
50
49
  result = @client.execute(uri: import_row_url, http_method: :post, body: request_body, headers: headers, parameters: params )
51
- jj result.data
52
-
53
- true # return
50
+ result.data
54
51
  end
55
52
 
56
53
  def generate_table(filename, start_line= 0)
57
- response = create_table(filename)
58
- import_rows(filename, response["tableId"], start_line)
54
+ create_response = create_table(filename)
55
+ import_response = import_rows(filename, create_response["tableId"], start_line)
56
+ [create_response, import_response]
59
57
  end
60
58
 
61
59
  def add_rows_to_existing_table(filename)
@@ -1,3 +1,3 @@
1
1
  class FusionTablesAPI
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,11 +8,17 @@ require 'yaml'
8
8
 
9
9
  class FusionTablesAPI
10
10
  GOOGLE_OAUTH_TOKEN_FILE = 'google_access_token.yml'
11
+ attr_reader :client
11
12
 
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')
13
+ # @oauth_cred - Accepts hash with :client_id, :client_secret, or
14
+ # filename of file that contains the oauth credentials 'client_id', 'client_secret'
15
+ # @google_api_client_options - Accepts options for the google_api_client, e.g. :application_name, :application_version
16
+ # See here https://github.com/google/google-api-ruby-client/blob/master/lib/google/api_client.rb
17
+ # @access_tokens - Accepts hash with :access_token, :refresh_token, or
18
+ # filename of file that contains the oauth tkens 'access_token', 'refresh_token'
19
+ def initialize(oauth_cred, access_tokens = nil, google_api_client_options = {})
20
+ @client = Google::APIClient.new(google_api_client_options)
21
+ @fusion_tables_api = @client.discovered_api('fusiontables') # Should comment this out when testing
16
22
 
17
23
  load_authorization_credentials(oauth_cred)
18
24
  load_oauth_tokens(access_tokens) if access_tokens
@@ -0,0 +1,74 @@
1
+ # fusion_tables_api.spec
2
+ require 'spec_helper'
3
+
4
+ describe FusionTablesAPI do
5
+ before do
6
+ @client_cred = { client_id: "FaKE-1D", client_secret: "s3EkR1t" }
7
+ @oauth_tokens = { access_token: "SuM-C0mb0-04-th15", refresh_token: "jUs7_a5-Wld" }
8
+ @google_api_client_options = {
9
+ application_name: "Test",
10
+ application_version: "1.0alpha"
11
+ }
12
+ end
13
+
14
+ describe "when initializing FusionTablesAPI" do
15
+ it "should raise an ArgumentError error if no parameters passed" do
16
+ expect { FusionTablesAPI.new }.to raise_error(ArgumentError)
17
+ end
18
+
19
+ it "should not raise an error if the client credentials are passed" do
20
+ expect { FusionTablesAPI.new(@client_cred, @google_api_client_options) }.to_not raise_error
21
+ end
22
+ end
23
+
24
+ before { @ft_no_tokens = FusionTablesAPI.new(@client_cred, nil, @google_api_client_options) }
25
+
26
+ subject { @ft_no_tokens }
27
+ it { should respond_to(:client) }
28
+
29
+ describe "after being initialized without access_tokens" do
30
+ let(:authorization) { @ft_no_tokens.client.authorization }
31
+ it "should have its client id set" do
32
+ authorization.client_id.should equal(@client_cred[:client_id])
33
+ end
34
+
35
+ it "should have its client secret set" do
36
+ authorization.client_secret.should equal(@client_cred[:client_secret])
37
+ end
38
+
39
+ it "should not have an access token set" do
40
+ authorization.access_token.should be_nil
41
+ end
42
+ end
43
+
44
+
45
+ before { @ft_with_tokens = FusionTablesAPI.new(@client_cred, @oauth_tokens, @google_api_client_options) }
46
+
47
+ describe "after being initialized with tokens" do
48
+ let(:authorization) { @ft_with_tokens.client.authorization }
49
+ it "should have its client id set" do
50
+ authorization.client_id.should equal(@client_cred[:client_id])
51
+ end
52
+
53
+ it "should have its client secret set" do
54
+ authorization.client_secret.should equal(@client_cred[:client_secret])
55
+ end
56
+
57
+ it "should not have an access token set" do
58
+ authorization.access_token.should equal(@oauth_tokens[:access_token])
59
+ end
60
+
61
+ it "should not have an access token set" do
62
+ authorization.refresh_token.should equal(@oauth_tokens[:refresh_token])
63
+ end
64
+ end
65
+
66
+ describe "when initialized without GoogleAPI Client options" do
67
+ it "should not raise an error" do
68
+ expect { FusionTablesAPI.new(@client_cred) }.to_not raise_error
69
+ end
70
+ it "should not raise an error when access_token in argument as well" do
71
+ expect { FusionTablesAPI.new(@client_cred, @oauth_tokens) }.to_not raise_error
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'fusion_tables_api'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fusion_tables_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Villena
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-30 00:00:00.000000000 Z
11
+ date: 2013-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client
@@ -85,6 +85,8 @@ files:
85
85
  - lib/fusion_tables_api/table.rb
86
86
  - lib/fusion_tables_api/version.rb
87
87
  - spec/.gitkeep
88
+ - spec/fusion_tables_api_spec.rb
89
+ - spec/spec_helper.rb
88
90
  homepage: ''
89
91
  licenses:
90
92
  - MIT
@@ -111,3 +113,5 @@ specification_version: 4
111
113
  summary: Create tables in Google Fusion Tables via its API
112
114
  test_files:
113
115
  - spec/.gitkeep
116
+ - spec/fusion_tables_api_spec.rb
117
+ - spec/spec_helper.rb