Class: Harborapp::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/harborapp/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.meta = 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

#listObject

Returns the value of attribute list.



7
8
9
# File 'lib/harborapp/entity.rb', line 7

def list
  @list
end

#metaObject

Returns the value of attribute meta.



7
8
9
# File 'lib/harborapp/entity.rb', line 7

def meta
  @meta
end

#objectObject

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::.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