Class: Harborapp::Entity
- Inherits:
-
Object
- Object
- Harborapp::Entity
- Defined in:
- lib/harborapp/entity.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
Returns the value of attribute list.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#object ⇒ Object
Returns the value of attribute object.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(json) ⇒ Entity
constructor
A new instance of Entity.
Constructor Details
#initialize(json) ⇒ Entity
Returns a new instance of Entity.
9 10 11 12 13 14 15 16 |
# File 'lib/harborapp/entity.rb', line 9 def initialize(json) self. = json["meta"] if json["meta"] if json["list"] self.list = Entity.build_from_list(json["list"]) else self.object = Entity.build_from_hash(json) end end |
Instance Attribute Details
#list ⇒ Object
Returns the value of attribute list.
7 8 9 |
# File 'lib/harborapp/entity.rb', line 7 def list @list end |
#meta ⇒ Object
Returns the value of attribute meta.
7 8 9 |
# File 'lib/harborapp/entity.rb', line 7 def end |
#object ⇒ Object
Returns the value of attribute object.
7 8 9 |
# File 'lib/harborapp/entity.rb', line 7 def object @object end |
Class Method Details
.build_from_hash(hash) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/harborapp/entity.rb', line 30 def self.build_from_hash(hash) # our hash should always include a class attribute that we can use # to map back to a proper Entity type # if !hash["success"].nil? and hash.keys.length == 1 return Harborapp::SuccessResponse.new hash["success"] end hash["new_record"] = false case hash["class"] when "account" Harborapp::Account.new hash when "upload" Harborapp::Upload.new hash else raise NoSuchEntity.new("Unknown return type in API response data: #{hash["class"]}") end end |
.build_from_list(list) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/harborapp/entity.rb', line 18 def self.build_from_list(list) list.map do |e| if e.is_a? Hash self.build_from_hash(e) elsif e.is_a? Array self.build_from_list(e) else # TODO: throw some weird entity error end end end |