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

Methods inherited from Object

#expire_cache, get_and_parse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ User

Returns a new instance of User.



15
16
17
18
19
20
21
22
# File 'lib/amee/user.rb', line 15

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

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



12
13
14
# File 'lib/amee/user.rb', line 12

def api_version
  @api_version
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/amee/user.rb', line 13

def status
  @status
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Class Method Details

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



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

def self.create(connection, environment_uid, options = {})
  prefix = environment_uid ? "/environments/#{environment_uid}" : "/admin"
  unless options.is_a?(Hash)
    raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
  end
  # Send data
  response = connection.post("#{prefix}/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



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

def self.from_json(json)
  # Read JSON
  doc = JSON.parse(json)
  data = {}
  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



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

def self.from_xml(xml)
  # Parse data from response into hash
  doc = REXML::Document.new(xml)
  data = {}
  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



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

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



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

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

#full_pathObject



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

def full_path
  "/admin/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