Class: Putio::Api::Object

Inherits:
Object
  • Object
show all
Extended by:
Client
Defined in:
lib/putio-ruby/api/object.rb

Direct Known Subclasses

File, Transfer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instantiate_object(hash) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/putio-ruby/api/object.rb', line 26

def self.instantiate_object(hash)
  obj = self.new
  hash.each do |k,v|
    obj.set_attribute(k.to_s, v)
  end
  obj
end

.parse_collection(hash) ⇒ Object



14
15
16
17
18
19
# File 'lib/putio-ruby/api/object.rb', line 14

def self.parse_collection(hash)
  objects = hash[plural_name.to_sym]
  objects.map do |obj|
    instantiate_object obj
  end
end

.parse_object(hash) ⇒ Object



21
22
23
24
# File 'lib/putio-ruby/api/object.rb', line 21

def self.parse_object(hash)
  object = hash[singular_name.to_sym]
  instantiate_object object
end

.plural_nameObject



6
7
8
# File 'lib/putio-ruby/api/object.rb', line 6

def self.plural_name
  singular_name.pluralize
end

.singular_nameObject



10
11
12
# File 'lib/putio-ruby/api/object.rb', line 10

def self.singular_name
  self.name.demodulize.underscore.downcase
end

Instance Method Details

#clientObject



34
35
36
# File 'lib/putio-ruby/api/object.rb', line 34

def client
  self.class.client
end

#set_attribute(attr, value) ⇒ Object



38
39
40
41
42
# File 'lib/putio-ruby/api/object.rb', line 38

def set_attribute(attr, value)
  return unless attr.present?
  self.singleton_class.instance_eval { attr_reader "#{attr}".to_sym }
  self.instance_variable_set("@#{attr}", value)
end