Class: Gcloud::Bigquery::Data

Inherits:
Array
  • Object
show all
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

QueryData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#gapiObject



32
33
34
# File 'lib/gcloud/bigquery/data.rb', line 32

def gapi
  @gapi
end

#tableObject



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.



96
97
98
99
100
101
102
103
104
105
# File 'lib/gcloud/bigquery/data.rb', line 96

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gcloud/bigquery/data.rb', line 107

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



84
85
86
87
88
89
90
91
# File 'lib/gcloud/bigquery/data.rb', line 84

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

#etagObject

A hash of this page of results.



54
55
56
# File 'lib/gcloud/bigquery/data.rb', line 54

def etag
  @gapi["etag"]
end

#kindObject

The resource type of the API response.



43
44
45
# File 'lib/gcloud/bigquery/data.rb', line 43

def kind
  @gapi["kind"]
end

#nextObject



69
70
71
72
73
# File 'lib/gcloud/bigquery/data.rb', line 69

def next
  return nil unless next?
  ensure_table!
  table.data token: token
end

#next?Boolean

Is there a next page of data?

Returns:

  • (Boolean)


65
66
67
# File 'lib/gcloud/bigquery/data.rb', line 65

def next?
  !token.nil?
end

#rawObject

Represents Table Data as a list of positional values (array of arrays). No type conversion is made, e.g. numbers are formatted as strings.



78
79
80
# File 'lib/gcloud/bigquery/data.rb', line 78

def raw
  Array(gapi["rows"]).map { |row| row["f"].map { |f| f["v"] } }
end

#tokenObject

A token used for paging results.



49
50
51
# File 'lib/gcloud/bigquery/data.rb', line 49

def token
  @gapi["pageToken"]
end

#totalObject

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