class FusionTablesAPI
Constants
- GOOGLE_OAUTH_TOKEN_FILE
- VERSION
Attributes
client[R]
Public Class Methods
new(oauth_cred, access_tokens = nil, google_api_client_options = {})
click to toggle source
@oauth_cred - Accepts hash with :client_id, :client_secret, or filename of file that contains the oauth credentials 'client_id', 'client_secret' @google_api_client_options - Accepts options for the google_api_client, e.g. :application_name, :application_version See here github.com/google/google-api-ruby-client/blob/master/lib/google/api_client.rb @access_tokens - Accepts hash with :access_token, :refresh_token, or filename of file that contains the oauth tkens 'access_token', 'refresh_token'
# 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
Public Instance Methods
add_rows_to_existing_table(filename)
click to toggle source
# File lib/fusion_tables_api/table.rb, line 59 def add_rows_to_existing_table(filename) end
create_table(filename)
click to toggle source
# 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)
click to toggle source
# 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_list()
click to toggle source
TODO Might eventually want to create a table class
FusionTables API Table methods ###
# 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)
click to toggle source
# 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
load_oauth_tokens(oauth_tokens)
click to toggle source
# 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)
click to toggle source
Set Google API Client authorization credentials
# 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)
click to toggle source
# 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_token()
click to toggle source
TODO Why not rewrite the oauth tokens file that may be given?
# 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