Sha256: 0ce08f984597fb67fc59fc150ad29a1671304fd5be308f1a878f5d70bf1d73df

Contents?: true

Size: 662 Bytes

Versions: 2

Compression:

Stored size: 662 Bytes

Contents

module Anilistrb
  class AnilistObj
    attr_reader :data, :attributes

    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

    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

    def to_s
      data
    end
        
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Anilistrb-0.1.2 lib/Anilistrb/AnilistObj.rb
Anilistrb-0.1.1 lib/Anilistrb/AnilistObj.rb