Class: Anilistrb::AnilistObj

Inherits:
Object
  • Object
show all
Defined in:
lib/Anilistrb/AnilistObj.rb

Direct Known Subclasses

Media, Medialist, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ AnilistObj

Returns a new instance of AnilistObj.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/Anilistrb/AnilistObj.rb', line 5

def initialize(json)
  @data = json
  @attributes = []
  unless @data.nil?
    @data.each do |var, val|
      snaked = to_snakecase(var)
      self.class.__send__(:attr_accessor, snaked)
      self.__send__("#{snaked}=", val)
      @attributes.push(snaked)
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/Anilistrb/AnilistObj.rb', line 3

def attributes
  @attributes
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/Anilistrb/AnilistObj.rb', line 3

def data
  @data
end

Instance Method Details

#to_sObject



25
26
27
# File 'lib/Anilistrb/AnilistObj.rb', line 25

def to_s
  data
end

#to_snakecase(str) ⇒ Object



18
19
20
21
22
23
# File 'lib/Anilistrb/AnilistObj.rb', line 18

def to_snakecase(str)
  str.gsub(/::/, '/')
     .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .tr('-', '_').downcase
end