Class: AMEE::Admin::User

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

Instance Attribute Summary collapse

Attributes inherited from Object

#connection, #created, #modified, #path, #uid

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ User

Returns a new instance of User.



12
13
14
15
16
17
18
19
20
# File 'lib/amee/user.rb', line 12

def initialize(data = {})
  @username = data[:username]
  @name = data[:name]
  @email = data[:email]
  @status = data[:status]
  @api_version  = data[:api_version].to_f rescue nil
  @environment_uid = data[:environment_uid]
  super
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/amee/user.rb', line 7

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/amee/user.rb', line 6

def username
  @username
end

Class Method Details

.create(connection, environment_uid, options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/amee/user.rb', line 87

def self.create(connection, environment_uid, options = {})
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
  end
  # Send data
  response = connection.post("/environments/#{environment_uid}/users", options).body
  # Parse response
  User.parse(connection, response)
rescue
  raise AMEE::BadData.new("Couldn't create User. Check that your information is correct.\n#{response}")
end

.from_json(json) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amee/user.rb', line 35

def self.from_json(json)
  # Read JSON
  doc = JSON.parse(json)
  data = {}
  data[:environment_uid] = doc['user']['environment']['uid']
  data[:uid] = doc['user']['uid']
  data[:created] = DateTime.parse(doc['user']['created'])
  data[:modified] = DateTime.parse(doc['user']['modified'])
  data[:username] = doc['user']['username']
  data[:name] = doc['user']['name']
  data[:email] = doc['user']['email']
  data[:api_version] = doc['user']['apiVersion']
  data[:status] = doc['user']['status']
  # Create object
  User.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load User from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/amee/user.rb', line 54

def self.from_xml(xml)
  # Parse data from response into hash
  doc = REXML::Document.new(xml)
  data = {}
  data[:environment_uid] = REXML::XPath.first(doc, "//Environment/@uid").to_s
  data[:uid] = REXML::XPath.first(doc, "//User/@uid").to_s
  data[:created] = DateTime.parse(REXML::XPath.first(doc, "//User/@created").to_s)
  data[:modified] = DateTime.parse(REXML::XPath.first(doc, "//User/@modified").to_s)
  data[:username] = REXML::XPath.first(doc, "//User/Username").text
  data[:name] = REXML::XPath.first(doc, "//User/Name").text
  data[:email] = REXML::XPath.first(doc, "//User/Email").text
  data[:api_version] = REXML::XPath.first(doc, "//User/ApiVersion").text
  data[:status] = REXML::XPath.first(doc, "//User/Status").text
  # Create object
  User.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load User from XML. Check that your URL is correct.\n#{xml}")
end

.get(connection, path, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/amee/user.rb', line 73

def self.get(connection, path, options = {})
  # Load data from path
  response = connection.get(path, options).body
  # Parse response
  User.parse(connection, response)
rescue
  raise AMEE::BadData.new("Couldn't load User. Check that your URL is correct.\n#{response}")
end

.parse(connection, response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/amee/user.rb', line 22

def self.parse(connection, response)
  # Parse data from response
  if response.is_json?
    user = User.from_json(response)
  else
    user = User.from_xml(response)
  end
  # Store connection in object for future use
  user.connection = connection
  # Done
  return user
end

Instance Method Details

#deleteObject



99
100
101
102
103
# File 'lib/amee/user.rb', line 99

def delete
  connection.delete(full_path)
rescue
  raise AMEE::BadData.new("Couldn't delete User. Check that your information is correct.")
end

#full_pathObject



105
106
107
# File 'lib/amee/user.rb', line 105

def full_path
  "/environments/#{@environment_uid}/users/#{uid}"
end

#update(options = {}) ⇒ Object



82
83
84
85
# File 'lib/amee/user.rb', line 82

def update(options = {})
  connection.put(full_path, options).body
  AMEE::Admin::User.get(connection, full_path)
end