Class: GoodData::Profile
- Inherits:
-
Rest::Resource
- Object
- Rest::Object
- Rest::Resource
- GoodData::Profile
- 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, 'language' => nil, 'links' => { 'projects' => nil, 'self' => nil }, 'email' => nil, 'authenticationModes' => [] } }
- ASSIGNABLE_MEMBERS =
[ :company, :country, :email, :login, :first_name, :last_name, :phone, :position, :timezone, :language ]
- PROFILE_PATH =
'/gdc/account/profile/%s'
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Attributes inherited from Rest::Object
Class Method Summary collapse
-
.[](id, opts = { client: GoodData.connection }) ⇒ Object
Get profile by ID or URI.
-
.create(attributes) ⇒ GoodData::Profile
Creates new instance from hash with attributes.
- .create_object(attributes) ⇒ Object
-
.current ⇒ GoodData::Profile
Gets user currently logged in.
- .diff(item1, item2) ⇒ Object
- .diff_list(list1, list2) ⇒ Object
Instance Method Summary collapse
-
#!=(other) ⇒ Boolean
Checks objects for non-equality.
-
#==(other) ⇒ Boolean
Checks objects for equality.
- #authentication_modes ⇒ Object
- #authentication_modes=(modes) ⇒ Object
- #channels ⇒ Object
-
#company ⇒ String
Gets the company name.
-
#company=(val) ⇒ Object
Set the company name.
- #content ⇒ Object
-
#country ⇒ String
Gets the country.
-
#country=(val) ⇒ Object
Set the country.
- #create_channel ⇒ Object
-
#created ⇒ DateTime
Gets date when created.
- #data ⇒ Object
-
#delete ⇒ Object
Deletes this account settings.
-
#diff(user) ⇒ Hash
Gets hash representing diff of profiles.
-
#email ⇒ String
Gets the email.
-
#email=(val) ⇒ Object
Set the email.
-
#first_name ⇒ String
Gets the first name.
-
#first_name=(val) ⇒ Object
Set the first name.
-
#full_name ⇒ Object
(also: #title)
Get full name.
-
#initialize(json) ⇒ Profile
constructor
Creates new instance.
-
#language ⇒ String
Gets the language.
-
#language=(val) ⇒ Object
Set the language.
-
#last_name ⇒ String
Gets the last name.
-
#last_name=(val) ⇒ Object
Set the last name.
- #links ⇒ Object
-
#login ⇒ String
Gets the login.
-
#login=(val) ⇒ Object
Set the login.
- #name ⇒ Object
-
#obj_id ⇒ String
(also: #account_setting_id)
Gets the resource identifier.
- #password ⇒ Object
- #password=(a_password) ⇒ Object
-
#phone ⇒ String
(also: #phone_number)
Gets the phone.
-
#phone=(val) ⇒ Object
(also: #phone_number=)
Set the phone.
-
#position ⇒ String
Gets the position in company.
-
#position=(val) ⇒ Object
Set the position.
-
#projects(limit = nil, offset = nil) ⇒ Array<GoodData::Project>
Gets the array of projects.
-
#save! ⇒ Object
Saves object if dirty, clears dirty flag.
- #sso_provider ⇒ Object
- #sso_provider=(an_sso_provider) ⇒ Object
-
#timezone ⇒ String
Gets the preferred timezone.
-
#timezone=(val) ⇒ Object
Set the timezone.
- #to_hash ⇒ Object
-
#updated ⇒ DateTime
Gets the date when updated.
-
#uri ⇒ String
Gets the resource REST URI.
Methods inherited from Rest::Object
client, default_client, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::RootKeyGetter
Constructor Details
#initialize(json) ⇒ Profile
Creates new instance
118 119 120 121 |
# File 'lib/gooddata/models/profile.rb', line 118 def initialize(json) @json = json @dirty = false end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
15 16 17 |
# File 'lib/gooddata/models/profile.rb', line 15 def json @json end |
#user ⇒ Object (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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gooddata/models/profile.rb', line 61 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
81 82 83 84 85 |
# File 'lib/gooddata/models/profile.rb', line 81 def create(attributes) res = create_object(attributes) res.save! res end |
.create_object(attributes) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gooddata/models/profile.rb', line 87 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 |
.current ⇒ GoodData::Profile
Gets user currently logged in
110 111 112 |
# File 'lib/gooddata/models/profile.rb', line 110 def current client.user end |
.diff(item1, item2) ⇒ Object
98 99 100 101 102 |
# File 'lib/gooddata/models/profile.rb', line 98 def diff(item1, item2) x = diff_list([item1], [item2]) return {} if x[:changed].empty? x[:changed].first[:diff] end |
Instance Method Details
#!=(other) ⇒ Boolean
Checks objects for non-equality
136 137 138 |
# File 'lib/gooddata/models/profile.rb', line 136 def !=(other) !(self == other) end |
#==(other) ⇒ Boolean
Checks objects for equality
127 128 129 130 |
# File 'lib/gooddata/models/profile.rb', line 127 def ==(other) return false unless other.respond_to?(:to_hash) to_hash == other.to_hash end |
#authentication_modes ⇒ Object
445 446 447 |
# File 'lib/gooddata/models/profile.rb', line 445 def authentication_modes @json['accountSetting']['authenticationModes'].map { |x| x.downcase.to_sym } end |
#authentication_modes=(modes) ⇒ Object
449 450 451 452 453 |
# File 'lib/gooddata/models/profile.rb', line 449 def authentication_modes=(modes) modes = Array(modes) @dirty = true @json['accountSetting']['authenticationModes'] = modes.map { |x| x.to_s.upcase } end |
#channels ⇒ Object
476 477 478 |
# File 'lib/gooddata/models/profile.rb', line 476 def channels GoodData::ChannelConfiguration.all(client: client) end |
#company ⇒ String
Gets the company name
151 152 153 |
# File 'lib/gooddata/models/profile.rb', line 151 def company @json['accountSetting']['companyName'] || '' end |
#company=(val) ⇒ Object
Set the company name
158 159 160 161 |
# File 'lib/gooddata/models/profile.rb', line 158 def company=(val) @dirty ||= company != val @json['accountSetting']['companyName'] = val end |
#content ⇒ Object
418 419 420 421 |
# File 'lib/gooddata/models/profile.rb', line 418 def content keys = (data.keys - ['links']) data.slice(*keys) end |
#country ⇒ String
Gets the country
181 182 183 |
# File 'lib/gooddata/models/profile.rb', line 181 def country @json['accountSetting']['country'] || '' end |
#country=(val) ⇒ Object
Set the country
188 189 190 191 |
# File 'lib/gooddata/models/profile.rb', line 188 def country=(val) @dirty ||= country != val @json['accountSetting']['country'] = val end |
#create_channel ⇒ Object
472 473 474 |
# File 'lib/gooddata/models/profile.rb', line 472 def create_channel GoodData::ChannelConfiguration.create(to: email, title: email, client: client) end |
#created ⇒ DateTime
Gets date when created
196 197 198 |
# File 'lib/gooddata/models/profile.rb', line 196 def created DateTime.parse(@json['accountSetting']['created']) end |
#data ⇒ Object
409 410 411 412 |
# File 'lib/gooddata/models/profile.rb', line 409 def data data = @json || {} data['accountSetting'] || {} end |
#delete ⇒ Object
Deletes this account settings
201 202 203 |
# File 'lib/gooddata/models/profile.rb', line 201 def delete client.delete uri end |
#diff(user) ⇒ Hash
Gets hash representing diff of profiles
209 210 211 |
# File 'lib/gooddata/models/profile.rb', line 209 def diff(user) GoodData::Profile.diff(self, user) end |
#email ⇒ String
Gets the email
216 217 218 |
# File 'lib/gooddata/models/profile.rb', line 216 def email @json['accountSetting']['email'].is_a?(String) ? @json['accountSetting']['email'].downcase : '' end |
#email=(val) ⇒ Object
Set the email
223 224 225 226 |
# File 'lib/gooddata/models/profile.rb', line 223 def email=(val) @dirty ||= email != val @json['accountSetting']['email'] = val end |
#first_name ⇒ String
Gets the first name
231 232 233 |
# File 'lib/gooddata/models/profile.rb', line 231 def first_name @json['accountSetting']['firstName'] || '' end |
#first_name=(val) ⇒ Object
Set the first name
238 239 240 241 |
# File 'lib/gooddata/models/profile.rb', line 238 def first_name=(val) @dirty ||= first_name != val @json['accountSetting']['firstName'] = val end |
#full_name ⇒ Object Also known as: title
Get full name
NOTE: This can be tricky to implement correctly for i18n
247 248 249 |
# File 'lib/gooddata/models/profile.rb', line 247 def full_name "#{first_name} #{last_name}" end |
#language ⇒ String
Gets the language
166 167 168 |
# File 'lib/gooddata/models/profile.rb', line 166 def language @json['accountSetting']['language'] || 'en-US' end |
#language=(val) ⇒ Object
Set the language
173 174 175 176 |
# File 'lib/gooddata/models/profile.rb', line 173 def language=(val) @dirty ||= language != val @json['accountSetting']['language'] = val end |
#last_name ⇒ String
Gets the last name
256 257 258 |
# File 'lib/gooddata/models/profile.rb', line 256 def last_name @json['accountSetting']['lastName'] || '' end |
#last_name=(val) ⇒ Object
Set the last name
263 264 265 266 |
# File 'lib/gooddata/models/profile.rb', line 263 def last_name=(val) @dirty ||= last_name != val @json['accountSetting']['lastName'] = val end |
#links ⇒ Object
414 415 416 |
# File 'lib/gooddata/models/profile.rb', line 414 def links data['links'] || {} end |
#login ⇒ String
Gets the login
271 272 273 |
# File 'lib/gooddata/models/profile.rb', line 271 def login @json['accountSetting']['login'] || '' end |
#login=(val) ⇒ Object
Set the login
278 279 280 281 |
# File 'lib/gooddata/models/profile.rb', line 278 def login=(val) @dirty ||= login != val @json['accountSetting']['login'] = val end |
#name ⇒ Object
423 424 425 |
# File 'lib/gooddata/models/profile.rb', line 423 def name (first_name || '') + (last_name || '') end |
#obj_id ⇒ String Also known as: account_setting_id
Gets the resource identifier
286 287 288 |
# File 'lib/gooddata/models/profile.rb', line 286 def obj_id uri.split('/').last end |
#password ⇒ Object
427 428 429 |
# File 'lib/gooddata/models/profile.rb', line 427 def password @json['accountSetting']['password'] end |
#password=(a_password) ⇒ Object
431 432 433 434 |
# File 'lib/gooddata/models/profile.rb', line 431 def password=(a_password) @dirty = true @json['accountSetting']['password'] = a_password end |
#phone ⇒ String Also known as: phone_number
Gets the phone
295 296 297 |
# File 'lib/gooddata/models/profile.rb', line 295 def phone @json['accountSetting']['phoneNumber'] || '' end |
#phone=(val) ⇒ Object Also known as: phone_number=
Set the phone
304 305 306 307 |
# File 'lib/gooddata/models/profile.rb', line 304 def phone=(val) @dirty ||= phone != val @json['accountSetting']['phoneNumber'] = val end |
#position ⇒ String
Gets the position in company
314 315 316 |
# File 'lib/gooddata/models/profile.rb', line 314 def position @json['accountSetting']['position'] || '' end |
#position=(val) ⇒ Object
Set the position
321 322 323 324 |
# File 'lib/gooddata/models/profile.rb', line 321 def position=(val) @dirty ||= position != val @json['accountSetting']['position'] = val end |
#projects(limit = nil, offset = nil) ⇒ Array<GoodData::Project>
Gets the array of projects
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/gooddata/models/profile.rb', line 330 def projects(limit = nil, offset = nil) url = @json['accountSetting']['links']['projects'] all_projects = [] raise ArgumentError, 'Params limit and offset are expected' if !offset.nil? && limit.nil? if limit.nil? url += "?limit=500" loop do projects = client.get url projects['projects']['items'].each do |project| all_projects << client.create(GoodData::Project, project) end if !projects['projects']['paging'].nil? && !projects['projects']['paging']['next'].nil? url = projects['projects']['paging']['next'] else break end end else limit = [limit, 500].min if limit.is_a?(Integer) && limit > 0 url += "?limit=#{limit}" url += "&offset=#{offset}" if !offset.nil? && offset.is_a?(Integer) && offset > 0 projects = client.get url projects['projects']['items'].each do |project| all_projects << client.create(GoodData::Project, project) end end all_projects end |
#save! ⇒ Object
Saves object if dirty, clears dirty flag
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/gooddata/models/profile.rb', line 366 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_provider ⇒ Object
436 437 438 |
# File 'lib/gooddata/models/profile.rb', line 436 def sso_provider @json['accountSetting']['ssoProvider'] end |
#sso_provider=(an_sso_provider) ⇒ Object
440 441 442 443 |
# File 'lib/gooddata/models/profile.rb', line 440 def sso_provider=(an_sso_provider) @dirty = true @json['accountSetting']['ssoProvider'] = an_sso_provider end |
#timezone ⇒ String
Gets the preferred timezone
383 384 385 |
# File 'lib/gooddata/models/profile.rb', line 383 def timezone @json['accountSetting']['timezone'] || '' end |
#timezone=(val) ⇒ Object
Set the timezone
390 391 392 393 |
# File 'lib/gooddata/models/profile.rb', line 390 def timezone=(val) @dirty ||= timezone != val @json['accountSetting']['timezone'] = val end |
#to_hash ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/gooddata/models/profile.rb', line 455 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 |
#updated ⇒ DateTime
Gets the date when updated
398 399 400 |
# File 'lib/gooddata/models/profile.rb', line 398 def updated DateTime.parse(@json['accountSetting']['updated']) end |