Class: AMEE::Admin::User
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Attributes inherited from Object
#connection, #created, #modified, #path, #uid
Class Method Summary collapse
- .create(connection, environment_uid, options = {}) ⇒ Object
- .from_json(json) ⇒ Object
- .from_xml(xml) ⇒ Object
- .get(connection, path, options = {}) ⇒ Object
- .parse(connection, response) ⇒ Object
Instance Method Summary collapse
- #delete ⇒ Object
- #full_path ⇒ Object
-
#initialize(data = {}) ⇒ User
constructor
A new instance of User.
- #update(options = {}) ⇒ Object
Methods inherited from Object
Methods included from ParseHelper
#load_xml_doc, #node_value, #xmlpathpreamble
Constructor Details
#initialize(data = {}) ⇒ User
Returns a new instance of User.
12 13 14 15 16 17 18 19 |
# 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 super end |
Instance Attribute Details
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
9 10 11 |
# File 'lib/amee/user.rb', line 9 def api_version @api_version end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
8 9 10 |
# File 'lib/amee/user.rb', line 8 def email @email end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/amee/user.rb', line 7 def name @name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
10 11 12 |
# File 'lib/amee/user.rb', line 10 def status @status end |
#username ⇒ Object (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
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/amee/user.rb', line 84 def self.create(connection, environment_uid, = {}) prefix = environment_uid ? "/environments/#{environment_uid}" : "/admin" unless .is_a?(Hash) raise AMEE::ArgumentError.new("Second argument must be a hash of options!") end # Send data response = connection.post("#{prefix}/users", ).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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/amee/user.rb', line 34 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/amee/user.rb', line 52 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
70 71 72 73 74 75 76 77 |
# File 'lib/amee/user.rb', line 70 def self.get(connection, path, = {}) # Load data from path response = connection.get(path, ).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
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/amee/user.rb', line 21 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
#delete ⇒ Object
97 98 99 100 101 |
# File 'lib/amee/user.rb', line 97 def delete connection.delete(full_path) rescue raise AMEE::BadData.new("Couldn't delete User. Check that your information is correct.") end |
#full_path ⇒ Object
103 104 105 |
# File 'lib/amee/user.rb', line 103 def full_path "/admin/users/#{uid}" end |