Module: GoodData::Model::ToWire

Defined in:
lib/gooddata/models/blueprint/to_wire.rb

Class Method Summary collapse

Class Method Details

.anchor_to_wire(_project, dataset) ⇒ Hash

Converts anchor to wire format. There is difference between datsets that do not have anchor and those that do. Even if there is no acnhor you stil have to generate. If there is anchor it behaves exactly like am attribute



18
19
20
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 18

def self.anchor_to_wire(_project, dataset)
  attribute_to_wire(dataset, DatasetBlueprint.anchor(dataset))
end

.attribute_to_wire(dataset, attribute) ⇒ Hash

Converts atttribute to wire format.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 38

def self.attribute_to_wire(dataset, attribute)
  ls = DatasetBlueprint.labels_for_attribute(dataset, attribute)
  labels = ls.map do |l|
    {
      label: {
        identifier: l[:id],
        title: GoodData::Model.title(l),
        type: l[:gd_type] || Model::DEFAULT_TYPE,
        dataType: GoodData::Model.normalize_gd_data_type(l[:gd_data_type]) || Model::DEFAULT_ATTRIBUTE_DATATYPE
      }
    }
  end
  {}.tap do |a|
    a[:attribute] = {}
    a[:attribute][:identifier] = attribute[:id]
    a[:attribute][:title] = Model.title(attribute)
    a[:attribute][:folder] = attribute[:folder] || dataset[:folder] || GoodData::Model.title(dataset)
    a[:attribute][:labels] = labels unless labels.empty?
    a[:attribute][:description] = GoodData::Model.description(attribute) if GoodData::Model.description(attribute)

    if attribute[:order_by]
      label, direction = attribute[:order_by].split(' - ')
      a[:attribute][:sortOrder] = {
        attributeSortOrder: {
          label: label,
          direction: direction
        }
      }
    end

    if attribute[:grain]
      a[:attribute][:grain] = attribute[:grain].map do |g|
        case g.keys.first
        when :date
          { dateDimension: g.values.first }
        else
          g
        end
      end
    end

    if attribute[:relations]
      a[:attribute][:relations] = attribute[:relations]
    end

    default = ls.find { |l| l[:default_label] }
    a[:attribute][:defaultLabel] = (default && default[:id]) || ls.first[:id] unless ls.empty?
  end
end

.attributes_to_wire(_project, dataset) ⇒ Hash

Converts atttribute to wire format.



27
28
29
30
31
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 27

def self.attributes_to_wire(_project, dataset)
  DatasetBlueprint.attributes(dataset).map do |a|
    attribute_to_wire(dataset, a)
  end
end

.bridges_to_wire(_project, bridge) ⇒ Hash

Converts bridges to wire format.



159
160
161
162
163
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 159

def self.bridges_to_wire(_project, bridge)
  DatasetBlueprint.bridges(bridge).map do |r|
    r[:dataset]
  end
end

.dataset_to_wire(project, dataset) ⇒ Hash

Converts dataset to wire format.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 93

def self.dataset_to_wire(project, dataset)
  {
    dataset: {
      identifier: dataset[:id],
      title: GoodData::Model.title(dataset),
      anchor: anchor_to_wire(project, dataset),
      attributes: attributes_to_wire(project, dataset),
      facts: DatasetBlueprint.facts(dataset).map { |f| fact_to_wire(dataset, f) },
      references: references_to_wire(project, dataset),
      bridges: bridges_to_wire(project, dataset)
    }
  }
end

.date_dimension_to_wire(project, dataset) ⇒ Hash

Converts date dimension to wire format.



112
113
114
115
116
117
118
119
120
121
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 112

def self.date_dimension_to_wire(project, dataset)
  payload = {}.tap do |dd|
    dd[:name] = dataset[:id]
    dd[:urn] = dataset[:urn] if dataset[:urn]
    dd[:title] = GoodData::Model.title(dataset)
    dd[:identifierPrefix] = dataset[:identifier_prefix] if dataset[:identifier_prefix]
    dd[:bridges] = bridges_to_wire(project, dataset)
  end
  { dateDimension: payload }
end

.fact_to_wire(dataset, fact) ⇒ Hash

Converts fact to wire format.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 128

def self.fact_to_wire(dataset, fact)
  payload = {
    fact: {
      identifier: fact[:id],
      title: GoodData::Model.title(fact),
      folder: fact[:folder] || dataset[:folder] || GoodData::Model.title(dataset),
      dataType: GoodData::Model.normalize_gd_data_type(fact[:gd_data_type]) || DEFAULT_FACT_DATATYPE,
      type: fact[:type]
    }
  }
  payload.tap do |p|
    p[:fact][:description] = GoodData::Model.description(fact) if GoodData::Model.description(fact)
    p[:fact][:restricted] = fact[:restricted] if fact[:restricted]
  end
end

.references_to_wire(_project, dataset) ⇒ Hash

Converts references to wire format.



149
150
151
152
153
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 149

def self.references_to_wire(_project, dataset)
  DatasetBlueprint.references(dataset).map do |r|
    r[:dataset]
  end
end

.to_wire(what) ⇒ Hash

Entry method. Converts ProjectBlueprint representation into wire format which is understood by the API



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/gooddata/models/blueprint/to_wire.rb', line 171

def self.to_wire(what)
  {
    diffRequest: {
      targetModel: {
        projectModel: {
          modelMetadata: { containCA: what[:include_ca] },
          datasets: (what[:datasets] || []).map { |d| dataset_to_wire(what, d) },
          dateDimensions: (what[:date_dimensions] || []).map { |d| date_dimension_to_wire(what, d) }
        }
      }
    }
  }
end