Sha256: 8c73579d1fbe64dbf0ad1ba990a71fd593b7be72ffe66c4654969db2392c9e6a

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

# frozen_string_literal: true

module Renstar
  module APIClient
    # Base API Object class from which other objects inherit
    class APIObject
      def initialize(raw_hash)
        @raw_hash = raw_hash
        raw_hash.each do |key, value|
          if key == 'ts'
            instance_variable_set("@#{key}", Time.at(value))
          else
            instance_variable_set("@#{key}", value)
          end

          define_singleton_method(key) do
            return instance_variable_get("@#{key}")
          end
        end
      end

      def to_h
        @raw_hash
      end

      def human_readable(type)
        @raw_hash.map do |key, value|
          description = APIClient.key_to_description(type, key)
          formatted_value = APIClient.value_to_formatted(type, key, value)
          format("%-35<description>s %<formatted_value>s\n",
                 { description: description, formatted_value: formatted_value })
        end.join
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
renstar-0.4.2 lib/renstar/api_client/api_object.rb
renstar-0.4.1 lib/renstar/api_client/api_object.rb
renstar-0.4.0 lib/renstar/api_client/api_object.rb