Class: GoodData::DataProduct

Inherits:
Rest::Resource show all
Includes:
Mixin::UriGetter
Defined in:
lib/gooddata/models/data_product.rb

Constant Summary collapse

ALL_DATA_PRODUCTS_PATH =
'/gdc/domains/%{domain_name}/dataproducts'
ONE_DATA_PRODUCT_PATH =
'/gdc/domains/%{domain_name}/dataproducts/%{id}'

Instance Attribute Summary collapse

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::UriGetter

#uri

Methods included from Mixin::ObjId

#obj_id

Methods inherited from Rest::Object

client, default_client, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

#initialize(json) ⇒ DataProduct

Returns a new instance of DataProduct.



57
58
59
60
61
# File 'lib/gooddata/models/data_product.rb', line 57

def initialize(json)
  super
  @json = json
  @domain = json.delete('domain')
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/gooddata/models/data_product.rb', line 13

def domain
  @domain
end

Class Method Details

.[](id, opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gooddata/models/data_product.rb', line 19

def [](id, opts)
  domain = opts[:domain]
  fail ArgumentError, 'No :domain specified' if domain.nil?

  client = domain.client
  fail ArgumentError, 'No client specified' if client.nil?

  if id == :all
    GoodData::DataProduct.all(opts)
  else
    data_products_uri = ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: id }

    result = client.get(data_products_uri)
    client.create(GoodData::DataProduct, result.merge('domain' => domain))
  end
end

.all(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gooddata/models/data_product.rb', line 36

def all(opts = {})
  domain = opts[:domain]
  fail ArgumentError, 'No :domain specified' if domain.nil?

  client = domain.client
  fail ArgumentError, 'No client specified' if client.nil?

  data_products_uri = ALL_DATA_PRODUCTS_PATH % { domain_name: domain.name }

  GoodData::Helpers.get_path(client.get(data_products_uri), %w(dataProducts items)).map do |i|
    client.create(GoodData::DataProduct, i, domain: domain)
  end
end

.create(data = {}, options = {}) ⇒ Object



50
51
52
53
54
# File 'lib/gooddata/models/data_product.rb', line 50

def create(data = {}, options = {})
  fail 'id for data_product has to be provided' if data[:id].blank?
  client = options[:client]
  client.create(GoodData::DataProduct, GoodData::Helpers.stringify_keys(dataProduct: data), domain: options[:domain])
end

Instance Method Details

#clientsObject



63
64
65
66
67
68
69
# File 'lib/gooddata/models/data_product.rb', line 63

def clients
  json = client.get(data['links']['clients'])

  json['clients']['items'].map do |val|
    client.create(GoodData::Client, val, domain: domain)
  end
end

#create_segment(data) ⇒ Object



143
144
145
146
147
# File 'lib/gooddata/models/data_product.rb', line 143

def create_segment(data)
  segment = GoodData::Segment.create(data, domain: domain, client: domain.client)
  segment.data_product = self
  segment.save
end

#data_product_idObject



135
136
137
# File 'lib/gooddata/models/data_product.rb', line 135

def data_product_id
  data['id']
end

#data_product_id=(new_id) ⇒ Object



139
140
141
# File 'lib/gooddata/models/data_product.rb', line 139

def data_product_id=(new_id)
  data['id'] = new_id
end

#delete(options = {}) ⇒ Object



90
91
92
93
94
# File 'lib/gooddata/models/data_product.rb', line 90

def delete(options = {})
  segments.peach { |s| s.delete(options) }
  client.delete(uri) if uri
  self
end

#saveObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/gooddata/models/data_product.rb', line 79

def save
  if @json[:uri]
    client.put(uri, json)
  else
    data_products_uri = ALL_DATA_PRODUCTS_PATH % { domain_name: domain.name }
    res = client.post(data_products_uri, json)
    @json = res
  end
  self
end

#segmentsObject



71
72
73
74
75
76
77
# File 'lib/gooddata/models/data_product.rb', line 71

def segments
  json = client.get(data['links']['segments'])

  json['segments']['items'].map do |val|
    client.create(GoodData::Segment, val, domain: domain, data_product: self)
  end
end

#update_clients(data, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gooddata/models/data_product.rb', line 96

def update_clients(data, options = {})
  if options[:delete_extra] && options[:delete_extra_in_segments]
    fail 'Options delete_extra and delete_extra_in_segments are mutually exclusive.'
  end

  data_products_uri = ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: data_product_id }

  payload = data.map do |datum|
    {
      :client => {
        :id => datum[:id],
        :segment => data_products_uri + '/segments/' + datum[:segment]
      }
    }.tap do |h|
      h[:client][:project] = datum[:project] if datum.key?(:project)
    end
  end

  if options[:delete_extra]
    res = client.post(data_products_uri + '/updateClients?deleteExtra=true', updateClients: { items: payload })
  elsif options[:delete_extra_in_segments]
    segments_to_delete_in = options[:delete_extra_in_segments]
                              .map { |segment| CGI.escape(segment) }
                              .join(',')
    uri = data_products_uri + "/updateClients?deleteExtraInSegments=#{segments_to_delete_in}"
    res = client.post(uri, updateClients: { items: payload })
  else
    res = client.post(data_products_uri + '/updateClients', updateClients: { items: payload })
  end
  data = GoodData::Helpers.get_path(res, ['updateClientsResponse'])
  if data
    result = data.flat_map { |k, v| v.map { |h| GoodData::Helpers.symbolize_keys(h.merge('type' => k)) } }
    result.select { |r| r[:status] == 'DELETED' }.peach { |r| r[:originalProject] && client.delete(r[:originalProject]) } if options[:delete_projects]
    result
  else
    []
  end
end