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.



9
10
11
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 9

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

Instance Method Details

#bulk(&block) ⇒ Object

Process mutliple commands in a single HTTP request



63
64
65
66
67
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 63

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

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



21
22
23
24
25
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 21

def create_table(name, columns = [], table_options = {})
  body = {:name => name, :columns => columns}.merge(:description => "Features", :isExportable => true).merge(table_options).to_json
  response = request(:post, 'https://www.googleapis.com/fusiontables/v2/tables', :body => body)
  return parse_reponse(response)['tableId']
end

#delete_row(table_id, row_id) ⇒ Object



52
53
54
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 52

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



43
44
45
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 43

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

#delete_table(table_id) ⇒ Object



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

def delete_table(table_id)
  request(:delete, "https://www.googleapis.com/fusiontables/v2/tables/#{table_id}")
end

#drive_serviceObject



103
104
105
106
107
108
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 103

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



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

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



110
111
112
113
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 110

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

#insert_style(table_id, style) ⇒ Object



47
48
49
50
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 47

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

#parse_reponse(response) ⇒ Object



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

def parse_reponse(response)
  JSON.parse(response.body)
end

#replace_rows(table_id, csv) ⇒ Object



83
84
85
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 83

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

#request(method, url, header: {}, body: {}, params: {}) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 69

def request(method, url, header: {}, body: {}, params: {})
  headers = @authorization.apply('Content-Type' => 'application/json')
  headers.merge!(header)
  headers.merge!(:params => params)
  return RestClient::Request.execute(:method => method, :url => url, :headers => headers, :payload => body)
rescue RestClient::ExceptionWithResponse => e
  puts e.response
  raise e
end

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



56
57
58
59
60
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 56

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

#select(query) ⇒ Object



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

def select(query)
  parse_reponse request(:get, "https://www.googleapis.com/fusiontables/v2/query", :params => {:sql => query})
end

#share_table(table_id) ⇒ Object



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

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



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

def style_ids(table_id)
  styles(table_id).collect {|t| t['styleId'] }
end

#styles(table_id) ⇒ Object



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

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

#table_idsObject



13
14
15
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 13

def table_ids
  tables.collect {|t| t['tableId'] }
end

#tablesObject



17
18
19
# File 'lib/spatial_features/has_fusion_table_features/service.rb', line 17

def tables
  parse_reponse(request(:get, 'https://www.googleapis.com/fusiontables/v2/tables')).fetch('items', [])
end

#upload_rows(table_id, csv) ⇒ Object



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

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