fusion_tables_v2 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/fusion_tables_v2.rb +176 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c12d23a44f6379b9841f97de1682dadfe8a81acb
4
+ data.tar.gz: 3c0b18a6a25e5869e5960ba323b727e4f5182f2b
5
+ SHA512:
6
+ metadata.gz: 187ba521d19e4cc5ee9eaad51d5b2154607b57d6e0186c064f11d289ca45c461fe3faf810b27d96d783581b1a4c91df8cc9a76a9ef5d75e4b665b2031bf31df7
7
+ data.tar.gz: c078ed980b637d7ff6023bb23029d893de04310f4bd1586c6c7f28175b6fcb0dea873647d4b3e49f9cbb1439d2ba47ae66d22f9d15f12a9af505e099644d1d96
@@ -0,0 +1,176 @@
1
+ require 'google/apis/fusiontables_v2'
2
+
3
+ class FusionTablesV2
4
+ API = Google::Apis::FusiontablesV2
5
+ AVAILABLE_COLUMN_TYPES = ['DATETIME', 'LOCATION', 'NUMBER', 'STRING']
6
+
7
+ class Table < API::Table
8
+ attr_accessor :db
9
+
10
+ # attribution string Attribution assigned to the table. writable
11
+ # attributionLink string Optional link for attribution. writable
12
+ # baseTableIds[] list Base table identifier if this table is a view or merged table.
13
+ # columnPropertiesJsonSchema string Default JSON schema for validating all JSON column properties. writable
14
+ # columns[] list Columns in the table. writable
15
+ # description string Description assigned to the table. writable
16
+ # isExportable boolean Variable for whether table is exportable. writable
17
+ # kind string The kind of item this is. For table, this is always fusiontables#table.
18
+ # name string Name assigned to a table. writable
19
+ # sql string SQL that encodes the table definition for derived tables.
20
+ # tableId string Encrypted unique alphanumeric identifier for the table.
21
+ # tablePropertiesJson string JSON object containing custom table properties. writable
22
+ # tablePropertiesJsonSchema string JSON schema for validating the JSON table properties.
23
+ def self.build(is_exportable: false, name:, db:)
24
+ new_table = new
25
+ new_table.db = db
26
+ new_table.name = name
27
+ new_table.columns = []
28
+ new_table.kind = 'fusiontables#table'
29
+ new_table.is_exportable = is_exportable
30
+ new_table
31
+ end
32
+
33
+ def string(column_name)
34
+ puts db
35
+ columns << db.build_column(name: column_name, type: 'STRING', table: self)
36
+ self
37
+ end
38
+
39
+ def datetime(column_name)
40
+ columns << db.build_column(name: column_name, type: 'DATETIME', table: self)
41
+ self
42
+ end
43
+
44
+ def integer(column_name)
45
+ columns << db.build_column(name: column_name, type: 'NUMBER', table: self)
46
+ self
47
+ end
48
+
49
+ def location(column_name)
50
+ columns << db.build_column(name: column_name, type: 'LOCATION', table: self)
51
+ self
52
+ end
53
+ end
54
+
55
+ class Column < API::Column
56
+ end
57
+
58
+ attr_reader :client, :authorization, :key_path
59
+
60
+ def self.connect(key_path: nil)
61
+ if key_path
62
+ new(key_path)
63
+ else
64
+ false
65
+ end
66
+ end
67
+
68
+ def initialize(key_path)
69
+ client = API::FusiontablesService.new
70
+ authorization = get_authorization(key_path)
71
+ client.authorization = authorization
72
+ @key_path = key_path
73
+ @authorization = authorization
74
+ @client = client
75
+ end
76
+
77
+ def get_authorization(key_path)
78
+ ENV['GOOGLE_APPLICATION_CREDENTIALS'] = key_path
79
+ scopes = ['https://www.googleapis.com/auth/fusiontables']
80
+ authorization = Google::Auth.get_application_default(scopes)
81
+ authorization.fetch_access_token!
82
+ authorization
83
+ end
84
+
85
+ def list_tables
86
+ @client.list_tables
87
+ end
88
+
89
+ def build_table(table_name, &block)
90
+ new_table = Table.build(name: table_name, db: self)
91
+ if block_given?
92
+ new_table.instance_eval(&block)
93
+ end
94
+ new_table
95
+ end
96
+
97
+ def create_table(table, &block)
98
+ if table.is_a?(Table)
99
+ @client.insert_table(table)
100
+ elsif table.is_a?(String)
101
+ if block_given?
102
+ builded_table = build_table(table, &block)
103
+ @client.insert_table(builded_table)
104
+ else
105
+ raise ArgumentError.new('Please specify migration block')
106
+ end
107
+ else
108
+ raise ArgumentError.new('Table must be String or Table object')
109
+ end
110
+ end
111
+
112
+ def drop_table(table_id)
113
+ client.delete_table(table_id)
114
+ true
115
+ rescue Google::Apis::ClientError
116
+ "Table doesn't exist"
117
+ end
118
+
119
+ def drop_table_by(name:)
120
+ tables = tables_hash
121
+ raise ArgumentError.new('There are more than 1 table with this name') if tables.count { |el| el[:name] == name } > 1
122
+ table_id = tables.find { |el| el[:name] == name }[:id]
123
+ drop_table(table_id)
124
+ end
125
+
126
+ def get_table(table_id)
127
+ client.get_table(table_id)
128
+ rescue Google::Apis::ClientError
129
+ "Table doesn't exist"
130
+ end
131
+
132
+ def get_table_by(name:)
133
+ tables = tables_hash
134
+ if tables.count { |el| el[:name] == name } > 1
135
+ client.list_tables.items.select {|t| t.name == 'my_table' }
136
+ elsif tables.count { |el| el[:name] == name } == 1
137
+ client.get_table(tables.find { |el| el[:name] == name }[:id])
138
+ else
139
+ "Table doesn't exist"
140
+ end
141
+ end
142
+
143
+ def import_rows(table_id, file:, delimiter: ",", content_type: 'application/octet-stream', start_line: 0)
144
+ client.import_rows(table_id, delimiter: delimiter, upload_source: file, content_type: content_type, start_line: start_line)
145
+ end
146
+
147
+ def tables_hash
148
+ items = client.list_tables.items || []
149
+ items.map { |t| { name: t.name, id: t.table_id } }
150
+ end
151
+
152
+ # baseColumn object Optional identifier of the base column. If present, this column is derived from the specified base column.
153
+ # baseColumn.columnId integer The ID of the column in the base table from which this column is derived.
154
+ # baseColumn.tableIndex integer Offset to the entry in the list of base tables in the table definition.
155
+ # columnId integer Identifier for the column.
156
+ # kind string The kind of item this is. For column, this is always fusiontables#column.
157
+ # name string Required name of the column. writable
158
+ # type string Required type of the column. Type can be "NUMBER", "STRING", "LOCATION", or "DATETIME".
159
+ # Acceptable values are:
160
+ # "DATETIME":
161
+ # "LOCATION":
162
+ # "NUMBER":
163
+ # "STRING":
164
+ def build_column(column_id: nil, validate_data: false, format_pattern: 'NONE', name:, type:, table:)
165
+ column_id ||= table.columns.size
166
+ raise ArgumentError.new('Column ID must be Integer') unless column_id.is_a?(Integer)
167
+ raise ArgumentError.new("Column type must be #{ AVAILABLE_COLUMN_TYPES }") unless AVAILABLE_COLUMN_TYPES.include?(type)
168
+ new_column = Column.new()
169
+ new_column.name = name
170
+ new_column.type = type
171
+ new_column.kind = 'fusiontables#column'
172
+ new_column.validate_data = validate_data
173
+ new_column.format_pattern = format_pattern
174
+ new_column
175
+ end
176
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fusion_tables_v2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rozenkin Denis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.pre2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.pre2
27
+ description: This gem adding methods to interact with Fusion Tables like DB
28
+ email: rozenkin@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/fusion_tables_v2.rb
34
+ homepage: http://rubygems.org/gems/fusion_tables_v2
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.6
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Fusion Tables API wrapper
58
+ test_files: []