Class: FusionTablesAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/fusion_tables_api.rb,
lib/fusion_tables_api/table.rb,
lib/fusion_tables_api/version.rb,
lib/fusion_tables_api/authorization.rb

Constant Summary collapse

GOOGLE_OAUTH_TOKEN_FILE =
'google_access_token.yml'
VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_cred, access_tokens = nil, google_api_client_options = {}) ⇒ FusionTablesAPI

filename of file that contains the oauth credentials ‘client_id’, ‘client_secret’ See here github.com/google/google-api-ruby-client/blob/master/lib/google/api_client.rb filename of file that contains the oauth tkens ‘access_token’, ‘refresh_token’



19
20
21
22
23
24
25
26
# File 'lib/fusion_tables_api.rb', line 19

def initialize(oauth_cred, access_tokens = nil, google_api_client_options = {})
  @client = Google::APIClient.new(google_api_client_options)
  @fusion_tables_api = @client.discovered_api('fusiontables') # Should comment this out when testing

  load_authorization_credentials(oauth_cred)
  load_oauth_tokens(access_tokens) if access_tokens
  true
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/fusion_tables_api.rb', line 11

def client
  @client
end

Instance Method Details

#add_rows_to_existing_table(filename) ⇒ Object



59
60
# File 'lib/fusion_tables_api/table.rb', line 59

def add_rows_to_existing_table(filename)
end

#create_table(filename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fusion_tables_api/table.rb', line 21

def create_table(filename)
  csv_file = CSV.read(filename, {headers: true})
  headers = { 'Content-Type' => 'application/json' }

  column_names = []
  csv_file.headers.each_with_index do |header|
    type = csv_file[0][header].is_i? ? "NUMBER" : "STRING"
    column_names << { "name" => header, "type" => type }
  end

  request_body = { "columns" => column_names, "isExportable" => false, "name" => File.basename(filename, ".csv") }
  result = @client.execute(api_method: @fusion_tables_api.table.insert, headers: headers, body: request_body.to_json)
  store_new_access_token if @authorization_tokens && 
    @client.authorization.access_token != @authorization_tokens['access_token']
  JSON.parse(result.body)
end

#generate_table(filename, start_line = 0) ⇒ Object



53
54
55
56
57
# File 'lib/fusion_tables_api/table.rb', line 53

def generate_table(filename, start_line= 0)
  create_response = create_table(filename)
  import_response = import_rows(filename, create_response["tableId"], start_line)
  [create_response, import_response]
end

#get_table_listObject

TODO Might eventually want to create a table class FusionTables API Table methods ###



14
15
16
17
18
19
# File 'lib/fusion_tables_api/table.rb', line 14

def get_table_list
  result = @client.execute(api_method: @fusion_tables_api.table.list)
  store_new_access_token if @authorization_tokens && 
    @client.authorization.access_token != @authorization_tokens['access_token']
  JSON.parse(result.body)
end

#import_rows(filename, table_id, start_line = 0) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fusion_tables_api/table.rb', line 38

def import_rows(filename, table_id, start_line= 0)
  headers = Hash.new
  headers[:content_type] = 'application/octet-stream'
  params = { 
    tableId: table_id, 
    startLine: start_line, 
    isStrict: false 
  } 
  request_body = File.read(filename)
  import_row_url = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableId/import"

  result = @client.execute(uri: import_row_url, http_method: :post, body: request_body, headers: headers, parameters: params )
  result.data
end

#initial_authorizationObject

Authorization



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fusion_tables_api/authorization.rb', line 48

def initial_authorization
  puts "1) Copy and paste the url below into your browser:" 
  authorize_uri = @client.authorization.authorization_uri
  puts authorize_uri
  # Launchy.open authorize
  puts ""

  puts "2) Accept the authorization request from Google in your browser"
  puts "3) Copy and paste the code parameter from the url and enter below:"
  print ">> "
  code = gets.chomp.strip

  @client.authorization.code = code
  # TODO Need to test this one below
  @authorization_tokens ||= @client.authorization.fetch_access_token!

  File.open(GOOGLE_OAUTH_TOKEN_FILE, 'w+') { |f| f.write(@authorization_tokens.to_yaml)}
  puts "Google OAuth Tokens written to #{GOOGLE_OAUTH_TOKEN_FILE}"
  true
end

#load_authorization_credentials(cred) ⇒ Object

Takes hash or file and loads oauth credentials



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fusion_tables_api/authorization.rb', line 21

def load_authorization_credentials(cred)
  if cred.is_a?(Hash)
    @client.authorization.client_id = cred[:client_id]
    @client.authorization.client_secret = cred[:client_secret]
  else
    credentials = YAML.load_file(cred)
    @client.authorization.client_id = credentials["client_id"]
    @client.authorization.client_secret = credentials["client_secret"]
  end
  @client.authorization.redirect_uri = "http://localhost/oauth2callback"
  @client.authorization.scope = "https://www.googleapis.com/auth/fusiontables" 
  true
end

#load_oauth_tokens(oauth_tokens) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fusion_tables_api/authorization.rb', line 35

def load_oauth_tokens(oauth_tokens)
  if oauth_tokens.is_a?(Hash)
    set_access_token(oauth_tokens[:access_token])
    set_refresh_token(oauth_tokens[:refresh_token])
  else
    @authorization_tokens = YAML.load_file(oauth_tokens)
    set_access_token(@authorization_tokens['access_token'])
    set_refresh_token(@authorization_tokens['refresh_token'])
  end
  true
end

#set_access_token(access_token) ⇒ Object

Set Google API Client authorization credentials



4
5
6
7
# File 'lib/fusion_tables_api/authorization.rb', line 4

def set_access_token(access_token)
  @access_token = access_token # Needed to check if access token expires
  @client.authorization.access_token = @access_token
end

#set_refresh_token(refresh_token) ⇒ Object



9
10
11
# File 'lib/fusion_tables_api/authorization.rb', line 9

def set_refresh_token(refresh_token)
  @client.authorization.refresh_token = refresh_token
end

#store_new_access_tokenObject

TODO Why not rewrite the oauth tokens file that may be given?



14
15
16
17
18
# File 'lib/fusion_tables_api/authorization.rb', line 14

def store_new_access_token
    @authorization_tokens['access_token'] = @client.authorization.access_token 
    File.open(GOOGLE_OAUTH_TOKEN_FILE, 'w+') { |f| f.write(@authorization_tokens.to_yaml)}
    true
end