Class: GoodData::Client

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

Constant Summary collapse

CLIENT_TEMPLATE =
{
  client: {
    id: nil,
    segment: nil,
    project: nil
  }
}

Instance Attribute Summary collapse

Attributes inherited from Rest::Object

#client, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::UriGetter

#uri

Methods included from Mixin::Links

#links

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(data) ⇒ Client

Returns a new instance of Client.



117
118
119
120
121
# File 'lib/gooddata/models/client.rb', line 117

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

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

Class Method Details

.[](id, opts = {}) ⇒ Object



32
33
34
35
36
37
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
# File 'lib/gooddata/models/client.rb', line 32

def [](id, opts = {})
  domain = opts[:domain]
  segment = opts[:segment]
  fail ArgumentError, 'No :domain specified' if domain.nil?
  client = domain.client
  fail ArgumentError, 'No client specified' if client.nil?
  data_product = opts[:data_product] || (segment ? segment.data_product : nil)

  if id == :all
    tenants_uri = base_uri(domain, data_product)
    tenants_uri += "?segment=#{CGI.escape(segment.segment_id)}" if segment

    all_clients = []
    loop do
      res = client.get tenants_uri
      res['clients']['paging']['next']
      res['clients']['items'].each do |i|
        p = i['client']['project']
        tenant = client.create(GoodData::Client, i.merge('domain' => domain))
        tenant.project = p
        all_clients << tenant
      end
      url = res['clients']['paging']['next']
      break unless url
    end

    all_clients
  else
    id = id.respond_to?(:client_id) ? id.client_id : id
    tenant_uri = base_uri(domain, data_product)
    data = client.get(tenant_uri + "/#{CGI.escape(id)}")
    client.create(GoodData::Client, data.merge('domain' => domain))
  end
end

.base_uri(domain, data_product) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/gooddata/models/client.rb', line 107

def base_uri(domain, data_product)
  if data_product
    uri = GoodData::DataProduct::ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: data_product.data_product_id }
  else
    uri = domain.segments_uri
  end
  uri + '/clients'
end

.create(data, options) ⇒ GoodData::Client Also known as: associate

Creates new client from parameters passed.

Should not be called directly. Use GoodData::Segment#create_client.

Parameters:

  • data (Hash)

    Data required to create the client

  • options (Hash)

    Options

Options Hash (data):

Options Hash (options):

Returns:



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

def create(data, options)
  segment = options[:segment]
  domain = segment.domain
  tenant = client.create(GoodData::Client, GoodData::Helpers.stringify_keys(CLIENT_TEMPLATE.merge(domain: domain)), domain: domain)
  tenant.tap do |s|
    s.project = data[:project]
    s.client_id = data[:id]
    s.segment = segment.uri
  end
end

.update_setting(name, value, opts = {}) ⇒ Object Also known as: add_setting



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gooddata/models/client.rb', line 90

def update_setting(name, value, opts = {})
  return nil unless value
  domain = opts[:domain]
  client_id = opts[:client_id]
  data_product_id = opts[:data_product_id]
  uri = data_product_id ? GoodData::DataProduct::ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: data_product_id } : domain.segments_uri
  body = {
    setting: {
      name: "#{name}",
      value: "#{value}"
    }
  }
  domain.client.put(uri + "/clients/#{client_id}/settings/#{name}", body)
  nil
end

Instance Method Details

#client_idString

Segment id getter for the Segment. Called segment_id since id is a reserved word in ruby world

Returns:

  • (String)

    Segment id



126
127
128
# File 'lib/gooddata/models/client.rb', line 126

def client_id
  data['id']
end

#client_id=(a_name) ⇒ Object



130
131
132
133
# File 'lib/gooddata/models/client.rb', line 130

def client_id=(a_name)
  data['id'] = a_name
  self
end

#deleteGoodData::Client

Deletes a client instance on the API.

Returns:



215
216
217
218
# File 'lib/gooddata/models/client.rb', line 215

def delete
  project.delete if project && !project.deleted?
  dissociate
end

#dissociateObject

Deletes a client but maintain their project



221
222
223
# File 'lib/gooddata/models/client.rb', line 221

def dissociate
  client.delete(uri) if uri
end

#projectGoodData::Project

Project this client has set

Returns:



154
155
156
# File 'lib/gooddata/models/client.rb', line 154

def project
  client.projects(project_uri) if project?
end

#project=(a_project) ⇒ GoodData::Cliet

Setter for the project this client has set

Parameters:

Returns:

  • (GoodData::Cliet)

    Returns the instance of the client



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

def project=(a_project)
  data['project'] = a_project.respond_to?(:uri) ? a_project.uri : a_project
  self
end

#project?Boolean

Returns boolean if client has a project provisioned

Returns:

  • (Boolean)

    Returns true if client has a project provisioned. False otherwise



161
162
163
# File 'lib/gooddata/models/client.rb', line 161

def project?
  project_uri != nil
end

#project_uriString

Project URI this client has set

Returns:

  • (String)

    Returns the URI of the project this client has set



147
148
149
# File 'lib/gooddata/models/client.rb', line 147

def project_uri
  data['project']
end

#reload!GoodData::Client

Reloads the client from the URI

Returns:



168
169
170
171
172
# File 'lib/gooddata/models/client.rb', line 168

def reload!
  res = client.get(uri)
  @json = res
  self
end

#saveGoodData::Client

Creates or updates a client instance on the API.

Returns:



201
202
203
204
205
206
207
208
209
210
# File 'lib/gooddata/models/client.rb', line 201

def save
  if uri
    client.put(uri, json)
  else
    data_product = segment.data_product
    res = client.post(self.class.base_uri(domain, data_product), json)
    @json = res
  end
  self
end

#segmentGoodData::Segment

Segment this client is connected to.

Returns:



186
187
188
189
# File 'lib/gooddata/models/client.rb', line 186

def segment
  segment_res = client.get(data['segment'])
  client.create(GoodData::Segment, segment_res)
end

#segment=(a_segment) ⇒ GoodData::Client

Segment id setter which this client is connected to.

Parameters:

  • a_segment (String)

    Id of the segment.

Returns:



178
179
180
181
# File 'lib/gooddata/models/client.rb', line 178

def segment=(a_segment)
  data['segment'] = a_segment.respond_to?(:uri) ? a_segment.uri : a_segment
  self
end

#segment_uriString

Segment URI this client is connected to.

Returns:

  • (String)

    Segment URI



194
195
196
# File 'lib/gooddata/models/client.rb', line 194

def segment_uri
  data['segment']
end

#settingsObject



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/gooddata/models/client.rb', line 225

def settings
  data_product = segment.data_product
  res = client.get(self.class.base_uri(domain, data_product) + "/#{client_id}/settings")
  settings = GoodData::Helpers.get_path(res, %w(settingsList items))
  settings.map do |setting|
    setting = setting['setting']
    {
      name: setting['name'],
      value: setting['value']
    }
  end
end