Class: SpatialFeatures::FusionTables::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/spatial_features/has_fusion_table_features/service.rb

Constant Summary collapse

APPLICATION_NAME =
'Fusion Tables + Spatial Features'
GOOGLE_AUTH_SCOPES =
%w(https://www.googleapis.com/auth/fusiontables https://www.googleapis.com/auth/drive)

Instance Method Summary collapse

Constructor Details

#initialize(service_account_credentials_path) ⇒ Service

Returns a new instance of Service.



7
8
9
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 7

def initialize()
  @authorization = get_authorization(, GOOGLE_AUTH_SCOPES)
end

Instance Method Details

#bulk(&block) ⇒ Object

Process mutliple commands in a single HTTP request



65
66
67
68
69
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 65

def bulk(&block)
  fusion_tables_service.batch do
    block.call(self)
  end
end

#create_table(name, columns = [], table_options = {}) ⇒ Object



19
20
21
22
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 19

def create_table(name, columns = [], table_options = {})
  table_object = {:name => name, :columns => columns, :is_exportable => true}.merge(table_options)
  fusion_tables_service.insert_table(table_object, :fields => 'table_id').table_id
end

#delete_row(table_id, row_id) ⇒ Object



54
55
56
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 54

def delete_row(table_id, row_id)
  fusion_tables_service.sql_query("DELETE FROM #{table_id} WHERE ROWID = #{row_id}")
end

#delete_style(table_id, style_id) ⇒ Object



36
37
38
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 36

def delete_style(table_id, style_id)
  fusion_tables_service.delete_style(table_id, style_id, :fields => nil)
end

#delete_table(table_id) ⇒ Object



24
25
26
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 24

def delete_table(table_id)
  fusion_tables_service.delete_table(table_id)
end

#delete_template(table_id, template_id) ⇒ Object



45
46
47
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 45

def delete_template(table_id, template_id)
  fusion_tables_service.delete_template(table_id, template_id, :fields => nil)
end

#drive_serviceObject



91
92
93
94
95
96
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 91

def drive_service
  @drive_service ||= Google::Apis::DriveV3::DriveService.new.tap do |drive|
    drive.client_options.application_name = APPLICATION_NAME
    drive.authorization = @authorization
  end
end

#fusion_tables_serviceObject



84
85
86
87
88
89
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 84

def fusion_tables_service
  @fusion_tables_service ||= Google::Apis::FusiontablesV2::FusiontablesService.new.tap do |service|
    service.client_options.application_name = APPLICATION_NAME
    service.authorization = @authorization
  end
end

#get_authorization(service_account_credentials_path, scopes) ⇒ Object



98
99
100
101
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 98

def get_authorization(, scopes)
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = 
  return Google::Auth.get_application_default(scopes)
end

#insert_style(table_id, style) ⇒ Object



40
41
42
43
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 40

def insert_style(table_id, style)
  style.reverse_merge! 'name' => 'default_table_style', 'isDefaultForTable' => true
  fusion_tables_service.insert_style(table_id, style, :fields => 'styleId')
end

#insert_template(table_id, template) ⇒ Object



49
50
51
52
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 49

def insert_template(table_id, template)
  template.reverse_merge! 'name' => 'default_table_template'
  fusion_tables_service.insert_template(table_id, template, :fields => 'templateId')
end

#replace_rows(table_id, csv) ⇒ Object



71
72
73
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 71

def replace_rows(table_id, csv)
  fusion_tables_service.replace_table_rows(table_id, :upload_source => csv, :options => {:open_timeout_sec => 1.hour})
end

#row_ids(table_id, conditions = {}) ⇒ Object



58
59
60
61
62
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 58

def row_ids(table_id, conditions = {})
  clause = conditions.collect {|column, value| ActiveRecord::Base.send(:sanitize_sql_array, ["? IN (?)", column, value]) }.join(' AND ')
  where = "WHERE #{clause}" if clause.present?
  return fusion_tables_service.sql_query_get("SELECT rowid FROM #{table_id} #{where}}").rows.flatten
end

#share_table(table_id) ⇒ Object



79
80
81
82
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 79

def share_table(table_id)
  permission = {:type => 'anyone', :role => 'reader', :withLink => true}
  drive_service.create_permission(table_id, permission, :fields => 'id')
end

#style_ids(table_id) ⇒ Object



28
29
30
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 28

def style_ids(table_id)
  styles(table_id).collect(&:style_id)
end

#styles(table_id) ⇒ Object



32
33
34
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 32

def styles(table_id)
  fusion_tables_service.list_styles(table_id).items
end

#table_idsObject



11
12
13
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 11

def table_ids
  tables.collect(&:table_id)
end

#tablesObject



15
16
17
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 15

def tables
  fusion_tables_service.list_tables(max_results: 10000).items || []
end

#upload_rows(table_id, csv) ⇒ Object



75
76
77
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 75

def upload_rows(table_id, csv)
  fusion_tables_service.import_rows(table_id, :upload_source => csv, :options => {:open_timeout_sec => 1.hour})
end