Class: GoodData::Profile

Inherits:
Rest::Resource show all
Defined in:
lib/gooddata/models/profile.rb

Constant Summary collapse

EMPTY_OBJECT =
{
  'accountSetting' => {
    'companyName' => nil,
    'country' => nil,
    'created' => nil,
    'firstName' => nil,
    'lastName' => nil,
    'login' => nil,
    'phoneNumber' => nil,
    'position' => nil,
    'timezone' => nil,
    'updated' => nil,
    'links' => {
      'projects' => nil,
      'self' => nil
    },
    'email' => nil,
    'authenticationModes' => []
  }
}
ASSIGNABLE_MEMBERS =
[
  :company,
  :country,
  :email,
  :login,
  :first_name,
  :last_name,
  :phone,
  :position,
  :timezone
]
PROFILE_PATH =
'/gdc/account/profile/%s'

Instance Attribute Summary collapse

Attributes inherited from Rest::Object

#client, #project

Class Method Summary collapse

Instance Method Summary collapse

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::RootKeyGetter

#root_key

Constructor Details

#initialize(json) ⇒ Profile

Creates new instance



116
117
118
119
# File 'lib/gooddata/models/profile.rb', line 116

def initialize(json)
  @json = json
  @dirty = false
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



15
16
17
# File 'lib/gooddata/models/profile.rb', line 15

def json
  @json
end

#userObject (readonly)

Returns the value of attribute user.



15
16
17
# File 'lib/gooddata/models/profile.rb', line 15

def user
  @user
end

Class Method Details

.[](id, opts = { client: GoodData.connection }) ⇒ Object

Get profile by ID or URI

Parameters:

  • id

    ID or URI of user to be found

  • opts (Hash) (defaults to: { client: GoodData.connection })

    Additional optional options

Options Hash (opts):

Returns:

  • GoodData::Profile User Profile



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gooddata/models/profile.rb', line 59

def [](id, opts = { client: GoodData.connection })
  return id if id.instance_of?(GoodData::Profile) || id.respond_to?(:profile?) && id.profile?

  if id.to_s !~ %r{^(\/gdc\/account\/profile\/)?[a-zA-Z\d]+$}
    fail(ArgumentError, 'wrong type of argument. Should be either profile ID or path')
  end

  id = id.match(/[a-zA-Z\d]+$/)[0] if id =~ %r{/}

  c = client(opts)
  fail ArgumentError, 'No :client specified' if c.nil?

  response = c.get(PROFILE_PATH % id)
  c.factory.create(Profile, response)
end

.create(attributes) ⇒ GoodData::Profile

Creates new instance from hash with attributes

Parameters:

  • attributes (Hash)

    Hash with initial attributes

Returns:



79
80
81
82
83
# File 'lib/gooddata/models/profile.rb', line 79

def create(attributes)
  res = create_object(attributes)
  res.save!
  res
end

.create_object(attributes) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/gooddata/models/profile.rb', line 85

def create_object(attributes)
  json = GoodData::Helpers.deep_dup(EMPTY_OBJECT)
  json['accountSetting']['links']['self'] = attributes[:uri] if attributes[:uri]
  res = client.create(GoodData::Profile, json)

  attributes.each do |k, v|
    res.send("#{k}=", v) if ASSIGNABLE_MEMBERS.include? k
  end
  res
end

.currentGoodData::Profile

Gets user currently logged in

Returns:



108
109
110
# File 'lib/gooddata/models/profile.rb', line 108

def current
  client.user
end

.diff(item1, item2) ⇒ Object



96
97
98
99
100
# File 'lib/gooddata/models/profile.rb', line 96

def diff(item1, item2)
  x = diff_list([item1], [item2])
  return {} if x[:changed].empty?
  x[:changed].first[:diff]
end

.diff_list(list1, list2) ⇒ Object



102
103
104
# File 'lib/gooddata/models/profile.rb', line 102

def diff_list(list1, list2)
  GoodData::Helpers.diff(list1, list2, key: :login)
end

Instance Method Details

#!=(other) ⇒ Boolean

Checks objects for non-equality

Parameters:

Returns:

  • (Boolean)

    True if different else false



134
135
136
# File 'lib/gooddata/models/profile.rb', line 134

def !=(other)
  !(self == other)
end

#==(other) ⇒ Boolean

Checks objects for equality

Parameters:

Returns:

  • (Boolean)

    True if same else false



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

def ==(other)
  return false unless other.respond_to?(:to_hash)
  to_hash == other.to_hash
end

#authentication_modesObject



399
400
401
# File 'lib/gooddata/models/profile.rb', line 399

def authentication_modes
  @json['accountSetting']['authenticationModes'].map { |x| x.downcase.to_sym }
end

#authentication_modes=(modes) ⇒ Object



403
404
405
406
407
# File 'lib/gooddata/models/profile.rb', line 403

def authentication_modes=(modes)
  modes = Array(modes)
  @dirty = true
  @json['accountSetting']['authenticationModes'] = modes.map { |x| x.to_s.upcase }
end

#channelsObject



430
431
432
# File 'lib/gooddata/models/profile.rb', line 430

def channels
  GoodData::ChannelConfiguration.all(client: client)
end

#companyString

Gets the company name

Returns:



149
150
151
# File 'lib/gooddata/models/profile.rb', line 149

def company
  @json['accountSetting']['companyName'] || ''
end

#company=(val) ⇒ Object

Set the company name

Parameters:

  • val (String)

    Company name to be set



156
157
158
159
# File 'lib/gooddata/models/profile.rb', line 156

def company=(val)
  @dirty ||= company != val
  @json['accountSetting']['companyName'] = val
end

#contentObject



372
373
374
375
# File 'lib/gooddata/models/profile.rb', line 372

def content
  keys = (data.keys - ['links'])
  data.slice(*keys)
end

#countryString

Gets the country

Returns:



164
165
166
# File 'lib/gooddata/models/profile.rb', line 164

def country
  @json['accountSetting']['country'] || ''
end

#country=(val) ⇒ Object

Set the country

Parameters:

  • val (String)

    Country to be set



171
172
173
174
# File 'lib/gooddata/models/profile.rb', line 171

def country=(val)
  @dirty ||= country != val
  @json['accountSetting']['country'] = val
end

#create_channelObject



426
427
428
# File 'lib/gooddata/models/profile.rb', line 426

def create_channel
  GoodData::ChannelConfiguration.create(to: email, title: email, client: client)
end

#createdDateTime

Gets date when created

Returns:

  • (DateTime)

    Created date



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

def created
  DateTime.parse(@json['accountSetting']['created'])
end

#dataObject



363
364
365
366
# File 'lib/gooddata/models/profile.rb', line 363

def data
  data = @json || {}
  data['accountSetting'] || {}
end

#deleteObject

Deletes this account settings



184
185
186
# File 'lib/gooddata/models/profile.rb', line 184

def delete
  client.delete uri
end

#diff(user) ⇒ Hash

Gets hash representing diff of profiles

Parameters:

Returns:

  • (Hash)

    Hash representing diff



192
193
194
# File 'lib/gooddata/models/profile.rb', line 192

def diff(user)
  GoodData::Profile.diff(self, user)
end

#emailString

Gets the email

Returns:



199
200
201
# File 'lib/gooddata/models/profile.rb', line 199

def email
  @json['accountSetting']['email'] || ''
end

#email=(val) ⇒ Object

Set the email

Parameters:

  • val (String)

    Email to be set



206
207
208
209
# File 'lib/gooddata/models/profile.rb', line 206

def email=(val)
  @dirty ||= email != val
  @json['accountSetting']['email'] = val
end

#first_nameString

Gets the first name

Returns:



214
215
216
# File 'lib/gooddata/models/profile.rb', line 214

def first_name
  @json['accountSetting']['firstName'] || ''
end

#first_name=(val) ⇒ Object

Set the first name

Parameters:

  • val (String)

    First name to be set



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

def first_name=(val)
  @dirty ||= first_name != val
  @json['accountSetting']['firstName'] = val
end

#full_nameObject Also known as: title

Get full name

NOTE: This can be tricky to implement correctly for i18n

Returns:

  • String Full Name



230
231
232
# File 'lib/gooddata/models/profile.rb', line 230

def full_name
  "#{first_name} #{last_name}"
end

#last_nameString

Gets the last name

Returns:



239
240
241
# File 'lib/gooddata/models/profile.rb', line 239

def last_name
  @json['accountSetting']['lastName'] || ''
end

#last_name=(val) ⇒ Object

Set the last name

Parameters:

  • val (String)

    Last name to be set



246
247
248
249
# File 'lib/gooddata/models/profile.rb', line 246

def last_name=(val)
  @dirty ||= last_name != val
  @json['accountSetting']['lastName'] = val
end


368
369
370
# File 'lib/gooddata/models/profile.rb', line 368

def links
  data['links'] || {}
end

#loginString

Gets the login

Returns:



254
255
256
# File 'lib/gooddata/models/profile.rb', line 254

def 
  @json['accountSetting']['login'] || ''
end

#login=(val) ⇒ Object

Set the login

Parameters:

  • val (String)

    Login to be set



261
262
263
264
# File 'lib/gooddata/models/profile.rb', line 261

def login=(val)
  @dirty ||=  != val
  @json['accountSetting']['login'] = val
end

#nameObject



377
378
379
# File 'lib/gooddata/models/profile.rb', line 377

def name
  (first_name || '') + (last_name || '')
end

#obj_idString Also known as: account_setting_id

Gets the resource identifier

Returns:

  • (String)

    Resource identifier



269
270
271
# File 'lib/gooddata/models/profile.rb', line 269

def obj_id
  uri.split('/').last
end

#passwordObject



381
382
383
# File 'lib/gooddata/models/profile.rb', line 381

def password
  @json['accountSetting']['password']
end

#password=(a_password) ⇒ Object



385
386
387
388
# File 'lib/gooddata/models/profile.rb', line 385

def password=(a_password)
  @dirty = true
  @json['accountSetting']['password'] = a_password
end

#phoneString Also known as: phone_number

Gets the phone

Returns:



278
279
280
# File 'lib/gooddata/models/profile.rb', line 278

def phone
  @json['accountSetting']['phoneNumber'] || ''
end

#phone=(val) ⇒ Object Also known as: phone_number=

Set the phone

Parameters:

  • val (String)

    Phone to be set



287
288
289
290
# File 'lib/gooddata/models/profile.rb', line 287

def phone=(val)
  @dirty ||= phone != val
  @json['accountSetting']['phoneNumber'] = val
end

#positionString

Gets the position in company

Returns:

  • (String)

    Position in company



297
298
299
# File 'lib/gooddata/models/profile.rb', line 297

def position
  @json['accountSetting']['position'] || ''
end

#position=(val) ⇒ Object

Set the position

Parameters:

  • val (String)

    Position to be set



304
305
306
307
# File 'lib/gooddata/models/profile.rb', line 304

def position=(val)
  @dirty ||= position != val
  @json['accountSetting']['position'] = val
end

#projectsArray<GoodData::Project>

Gets the array of projects

Returns:

  • (Array<GoodData::Project>)

    Array of project where account settings belongs to



312
313
314
315
316
317
# File 'lib/gooddata/models/profile.rb', line 312

def projects
  projects = client.get @json['accountSetting']['links']['projects']
  projects['projects'].map do |project|
    client.create(GoodData::Project, project)
  end
end

#save!Object

Saves object if dirty, clears dirty flag



320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/gooddata/models/profile.rb', line 320

def save!
  if @dirty
    raw = @json.dup
    raw['accountSetting'].delete('login')

    if uri && !uri.empty?
      url = "/gdc/account/profile/#{obj_id}"
      @json = client.put url, raw
      @dirty = false
    end
  end
  self
end

#sso_providerObject



390
391
392
# File 'lib/gooddata/models/profile.rb', line 390

def sso_provider
  @json['accountSetting']['ssoProvider']
end

#sso_provider=(an_sso_provider) ⇒ Object



394
395
396
397
# File 'lib/gooddata/models/profile.rb', line 394

def sso_provider=(an_sso_provider)
  @dirty = true
  @json['accountSetting']['ssoProvider'] = an_sso_provider
end

#timezoneString

Gets the preferred timezone

Returns:

  • (String)

    Preferred timezone



337
338
339
# File 'lib/gooddata/models/profile.rb', line 337

def timezone
  @json['accountSetting']['timezone'] || ''
end

#timezone=(val) ⇒ Object

Set the timezone

Parameters:

  • val (String)

    Timezone to be set



344
345
346
347
# File 'lib/gooddata/models/profile.rb', line 344

def timezone=(val)
  @dirty ||= timezone != val
  @json['accountSetting']['timezone'] = val
end

#to_hashObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/gooddata/models/profile.rb', line 409

def to_hash
  tmp = GoodData::Helpers.symbolize_keys(content.merge(uri: uri))
  [
    [:companyName, :company],
    [:phoneNumber, :phone],
    [:firstName, :first_name],
    [:lastName, :last_name],
    [:authenticationModes, :authentication_modes],
    [:ssoProvider, :sso_provider]
  ].each do |vals|
    wire, rb = vals
    tmp[rb] = tmp[wire]
    tmp.delete(wire)
  end
  tmp
end

#updatedDateTime

Gets the date when updated

Returns:

  • (DateTime)

    Updated date



352
353
354
# File 'lib/gooddata/models/profile.rb', line 352

def updated
  DateTime.parse(@json['accountSetting']['updated'])
end

#uriString

Gets the resource REST URI

Returns:



359
360
361
# File 'lib/gooddata/models/profile.rb', line 359

def uri
  GoodData::Helpers.get_path(@json, %w(accountSetting links self))
end