Class: GoodData::User

Inherits:
Object
  • Object
show all
Defined in:
lib/gooddata/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ User

Initialize new user

Parameters:

  • attrs (Hash) (defaults to: {})

    user attributes

Options Hash (attrs):

  • :email (String)

    user email/login

  • :firstName (String)

    user first name (Optional)

  • :lastName (String)

    user last name (Optional)

  • :password (String)

    user password (Optional)


54
55
56
57
58
# File 'lib/gooddata/user.rb', line 54

def initialize(attrs = {})
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#companyObject (readonly)

Returns the value of attribute company


8
9
10
# File 'lib/gooddata/user.rb', line 8

def company
  @company
end

#emailObject

Returns the value of attribute email


9
10
11
# File 'lib/gooddata/user.rb', line 9

def email
  @email
end

#firstNameObject

Returns the value of attribute firstName


9
10
11
# File 'lib/gooddata/user.rb', line 9

def firstName
  @firstName
end

#idObject (readonly)

Returns the value of attribute id


8
9
10
# File 'lib/gooddata/user.rb', line 8

def id
  @id
end

#lastNameObject

Returns the value of attribute lastName


9
10
11
# File 'lib/gooddata/user.rb', line 9

def lastName
  @lastName
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.


10
11
12
# File 'lib/gooddata/user.rb', line 10

def password=(value)
  @password = value
end

#rawObject (readonly)

Returns the value of attribute raw


8
9
10
# File 'lib/gooddata/user.rb', line 8

def raw
  @raw
end

#statusObject (readonly)

Returns the value of attribute status


8
9
10
# File 'lib/gooddata/user.rb', line 8

def status
  @status
end

Class Method Details

.allArray<User>

Get all users

Returns:

  • (Array<User>)

    Array of GoodData users


16
17
18
19
20
# File 'lib/gooddata/user.rb', line 16

def self.all
  GoodData.get('/gdc/account/domains/{company}/users')['accountSettings']['items'].map do |user|
    new.parse(user)
  end
end

.create(attrs) ⇒ User

Create GoodData user

Parameters:

  • attrs (Hash)

    user attributes

Options Hash (attrs):

  • :email (String)

    user email/login

  • :firstName (String)

    user first name (Optional)

  • :lastName (String)

    user last name (Optional)

  • :password (String)

    user password (Optional)

Returns:

  • (User)

    user object


28
29
30
31
32
# File 'lib/gooddata/user.rb', line 28

def self.create(attrs)
  user = new(attrs)
  user.save
  user
end

.find(email) ⇒ User

Find user by email

Parameters:

  • email (String)

    user email

Returns:

  • (User)

    user object


39
40
41
42
# File 'lib/gooddata/user.rb', line 39

def self.find(email)
  attrs = GoodData.get('/gdc/account/domains/{company}/users', login: email)['accountSettings']['items'].first
  new.parse(attrs) if attrs
end

Instance Method Details

#as_jsonHash

Get user attributes

Returns:

  • (Hash)

    user attributes


131
132
133
134
135
136
137
138
139
140
# File 'lib/gooddata/user.rb', line 131

def as_json
  {
    id: id,
    email: email,
    firstName: firstName,
    lastName: lastName,
    status: status,
    company: company
  }
end

#deleteObject

Delete GoodData user


123
124
125
# File 'lib/gooddata/user.rb', line 123

def delete
  GoodData.delete("/gdc/account/profile/#{@id}")
end

#parse(attrs) ⇒ User

Parse GoodData user

Parameters:

  • attrs (Hash)

    parsed JSON response for GoodData user

Returns:

  • (User)

    user object


65
66
67
68
69
70
71
72
73
# File 'lib/gooddata/user.rb', line 65

def parse(attrs)
  @id = attrs['accountSetting']['links']['self'].split('/').last
  @email = attrs['accountSetting']['email']
  @firstName = attrs['accountSetting']['firstName']
  @lastName = attrs['accountSetting']['lastName']
  @company = attrs['accountSetting']['companyName']
  @raw = attrs['accountSetting']
  self
end

#parse_project(attrs) ⇒ User

Parse GoodData user from project

Parameters:

  • attrs (Hash)

    parsed JSON response

Returns:

  • (User)

    user object


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

def parse_project(attrs)
  @id = attrs['user']['links']['projectRelUri'].split('/').last
  @email = attrs['user']['content']['email']
  @firstName = attrs['user']['content']['firstname']
  @lastName = attrs['user']['content']['lastname']
  @status = attrs['user']['content']['status']
  @raw = attrs['user']
  self
end

#saveString

Save user to GoodData

Returns:

  • (String)

    user id


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gooddata/user.rb', line 94

def save  # Set random password if not provided

  @password ||= SecureRandom.base64  # Build payload

  payload = {
    accountSetting: {
      login: @email,
      password: @password,
      email: @email,
      verifyPassword: @password,
      firstName: @firstName,
      lastName: @lastName,
      ssoProvider: 'auth.demandbase.com'
    }
  }
  if @id    # Update user if exists

    GoodData.put("/gdc/account/profile/#{@id}", payload)
    @id
  else
    # Create new user otherwise
    response = GoodData.post('/gdc/account/domains/{company}/users', payload)
    @id = response['uri'].split('/').last
  end
end