Sha256: bb2e22e9eb93d16bb95a5bdb2ae729a84e547bed6d431429fe6f1f3a12cd7e34

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

#!/usr/bin/env ruby
# encoding: utf-8

require_relative "http"
require_relative "constants"

module Twoffein
  Profile = Struct.new(:quest,
      :drink,
      :rank,
      :rank_title,
      :drunken,
      :bluttwoffeinkonzentration,
      :first_login,
      :screen_name)

  class Profile
    def initialize(hash=nil)
      return super(*hash) if hash.nil?
      hash.each { |key,val| self[key] = val if members.include? key }
    end

    def self.get(profile="")
      new(HTTP.get("profile", :profile => profile))
    end

    def to_s
      hash = instance_hash
      max_length = hash.keys.map { |k| k.length }.max

      hash.map { |attr, value|
        attr = attr.to_sym

        if attr == :first_login
          value = human_readable_time(value)
        end

        postfix = ":"
        attr = human_readable_key(attr) + postfix

        "#{attr.to_s.ljust(max_length+postfix.length+1)}#{value}"
      }.join("\n")
    end

    private

    def instance_hash
      members.reduce({}) do |hash, ivar|
        hash.merge({ivar.to_sym => self[ivar]})
      end
    end

    def human_readable_key(key)
      key.to_s.gsub('_', ' ').split(/(\W)/).map(&:capitalize).join
    end

    def human_readable_time(value)
      Time.at(value.to_i).strftime("%Y-%m-%d %H:%M")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twoffein-client-0.1.2 lib/twoffein-client/profile.rb
twoffein-client-0.1.1 lib/twoffein-client/profile.rb
twoffein-client-0.1.0 lib/twoffein-client/profile.rb