Class: Devtunnel::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/devtunnel/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.



8
9
10
11
12
13
14
15
# File 'lib/devtunnel/entity.rb', line 8

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.



6
7
8
# File 'lib/devtunnel/entity.rb', line 6

def list
  @list
end

#metaObject

Returns the value of attribute meta.



6
7
8
# File 'lib/devtunnel/entity.rb', line 6

def meta
  @meta
end

#objectObject

Returns the value of attribute object.



6
7
8
# File 'lib/devtunnel/entity.rb', line 6

def object
  @object
end

Class Method Details

.build_from_hash(hash) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/devtunnel/entity.rb', line 29

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 Devtunnel::SuccessResponse.new hash["success"]
  end

  hash["new_record"] = false
  case hash["class"]
    when "account"
      Devtunnel::Account.new hash
    else
      raise NoSuchEntity.new("Unknown return type in API response data: #{hash["class"]}")
  end
end

.build_from_list(list) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/devtunnel/entity.rb', line 17

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