Class: Gcloud::Bigquery::Data
- Inherits:
-
Array
- Object
- Array
- Gcloud::Bigquery::Data
- Defined in:
- lib/gcloud/bigquery/data.rb
Overview
# Data
Represents Table Data as a list of name/value pairs. Also contains metadata such as etag and total.
Direct Known Subclasses
Instance Attribute Summary collapse
Class Method Summary collapse
-
.format_rows(rows, fields) ⇒ Object
rubocop:disable all Disabled rubocop because this implementation will not last.
- .format_values(field_types, values) ⇒ Object
- .from_response(resp, table) ⇒ Object
Instance Method Summary collapse
- #all(request_limit: nil) {|row| ... } ⇒ Enumerator
-
#etag ⇒ Object
A hash of this page of results.
-
#initialize(arr = []) ⇒ Data
constructor
A new instance of Data.
-
#kind ⇒ Object
The resource type of the API response.
-
#next ⇒ Data
Retrieve the next page of data.
-
#next? ⇒ Boolean
Whether there is a next page of data.
-
#raw ⇒ Object
Represents Table Data as a list of positional values (array of arrays).
-
#token ⇒ Object
A token used for paging results.
-
#total ⇒ Object
The total number of rows in the complete table.
Constructor Details
#initialize(arr = []) ⇒ Data
Returns a new instance of Data.
35 36 37 38 39 |
# File 'lib/gcloud/bigquery/data.rb', line 35 def initialize arr = [] @table = nil @gapi = {} super arr end |
Instance Attribute Details
#gapi ⇒ Object
32 33 34 |
# File 'lib/gcloud/bigquery/data.rb', line 32 def gapi @gapi end |
#table ⇒ Object
28 29 30 |
# File 'lib/gcloud/bigquery/data.rb', line 28 def table @table end |
Class Method Details
.format_rows(rows, fields) ⇒ Object
rubocop:disable all Disabled rubocop because this implementation will not last.
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/gcloud/bigquery/data.rb', line 194 def self.format_rows rows, fields headers = Array(fields).map { |f| f["name"] } field_types = Array(fields).map { |f| f["type"] } Array(rows).map do |row| values = row["f"].map { |f| f["v"] } formatted_values = format_values field_types, values Hash[headers.zip formatted_values] end end |
.format_values(field_types, values) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/gcloud/bigquery/data.rb', line 205 def self.format_values field_types, values field_types.zip(values).map do |type, value| begin if value.nil? nil elsif type == "INTEGER" Integer value elsif type == "FLOAT" Float value elsif type == "BOOLEAN" (value == "true" ? true : (value == "false" ? false : nil)) else value end rescue value end end end |
.from_response(resp, table) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/gcloud/bigquery/data.rb', line 182 def self.from_response resp, table formatted_rows = format_rows resp.data["rows"], table.fields data = new formatted_rows data.table = table data.gapi = resp.data data end |
Instance Method Details
#all(request_limit: nil) {|row| ... } ⇒ Enumerator
Retrieves all rows by repeatedly loading #next until #next? returns false. Calls the given block once for each row, which is passed as the parameter.
An Enumerator is returned if no block is given.
This method may make several API calls until all rows are retrieved. Be sure to use as narrow a search criteria as possible. Please use with caution.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gcloud/bigquery/data.rb', line 158 def all request_limit: nil request_limit = request_limit.to_i if request_limit return enum_for(:all, request_limit: request_limit) unless block_given? results = self loop do results.each { |r| yield r } if request_limit request_limit -= 1 break if request_limit < 0 end break unless results.next? results = results.next end end |
#etag ⇒ Object
A hash of this page of results.
54 55 56 |
# File 'lib/gcloud/bigquery/data.rb', line 54 def etag @gapi["etag"] end |
#kind ⇒ Object
The resource type of the API response.
43 44 45 |
# File 'lib/gcloud/bigquery/data.rb', line 43 def kind @gapi["kind"] end |
#next ⇒ Data
Retrieve the next page of data.
101 102 103 104 105 |
# File 'lib/gcloud/bigquery/data.rb', line 101 def next return nil unless next? ensure_table! table.data token: token end |
#next? ⇒ Boolean
Whether there is a next page of data.
80 81 82 |
# File 'lib/gcloud/bigquery/data.rb', line 80 def next? !token.nil? end |
#raw ⇒ Object
Represents Table Data as a list of positional values (array of arrays). No type conversion is made, e.g. numbers are formatted as strings.
176 177 178 |
# File 'lib/gcloud/bigquery/data.rb', line 176 def raw Array(gapi["rows"]).map { |row| row["f"].map { |f| f["v"] } } end |
#token ⇒ Object
A token used for paging results.
49 50 51 |
# File 'lib/gcloud/bigquery/data.rb', line 49 def token @gapi["pageToken"] end |
#total ⇒ Object
The total number of rows in the complete table.
59 60 61 |
# File 'lib/gcloud/bigquery/data.rb', line 59 def total @gapi["totalRows"] end |