Class: Rubyhexagon::User

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/user.rb,
lib/rubyhexagon/api/user.rb,
lib/rubyhexagon/user/level.rb

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Defined Under Namespace

Classes: Level

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Object

Initializer for User

Examples:

Get a new user instance

User.new(:id) #=> E621::User instance

Parameters:

  • user (Hash)

    user data, fetched from e621

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubyhexagon/user.rb', line 73

def initialize(user)
  raise ArgumentError, "#{user.class} is not a Hash" unless user.is_a?(Hash)
  raise ArgumentError, 'Hash must include :id' if user[:id].nil?
  user.each do |k, v|
    if %i[id name].include?(k)
      if k == :id && !(v.is_a?(Integer) && v.positive?)
        raise InvalidIDError, "ID out of range: #{v}"
      end
      instance_variable_set("@#{k}".to_sym, v)
    elsif %i[created_at].include?(k)
      instance_variable_set("@#{k}".to_sym, Time.parse(v))
    elsif k == :artist_tags
      @artist_tags = v.map { |t| E621::Tag.new(name: t) }
    elsif k == :avatar_id
      @avatar = E621::Post.new(id: v)
    end
  end
end

Instance Attribute Details

#artist_tagsArray<E621::Tag> (readonly)

Artists tags, that belong to user

Examples:

Get artist tags

user.artist_tags #=> Array<E621::Tag>

Returns:

  • (Array<E621::Tag>)

    an array of artist tags, associated with user

Since:

  • 1.0.0



62
63
64
# File 'lib/rubyhexagon/user.rb', line 62

def artist_tags
  @artist_tags
end

#avatarE621::Post (readonly)

Avatar post that user chosen

Examples:

Get user avatar post

user.avatar #=> E621::Post

Returns:

Since:

  • 1.0.0



56
57
58
# File 'lib/rubyhexagon/user.rb', line 56

def avatar
  @avatar
end

#created_atTime (readonly)

Time user was created

Examples:

Get user creation time

user.created_at #=> Time

Returns:

  • (Time)

    registration time of this user

Since:

  • 1.0.0



50
51
52
# File 'lib/rubyhexagon/user.rb', line 50

def created_at
  @created_at
end

#idInteger (readonly)

User ID

Examples:

Get ID from user

user.id #=> Integer

Returns:

  • (Integer)

    id of user

Since:

  • 1.0.0



32
33
34
# File 'lib/rubyhexagon/user.rb', line 32

def id
  @id
end

#levelE621::User::Level (readonly)

User access level

Examples:

Get user access level

user.level #=> E621::User::Level

Returns:

  • (E621::User::Level)

    level of access, this user holds

Since:

  • 1.0.0



44
45
46
# File 'lib/rubyhexagon/user.rb', line 44

def level
  @level
end

#nameString (readonly)

User name

Examples:

Get name from user

user.name #=> String

Returns:

  • (String)

    name of user

Since:

  • 1.0.0



38
39
40
# File 'lib/rubyhexagon/user.rb', line 38

def name
  @name
end

Class Method Details

.list(query) ⇒ E621::User

Fetch a list of users

Examples:

Get list of users

E621::User.list(name: 'purple') #=> Array<E621::User>

Returns:

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



50
51
52
53
54
55
# File 'lib/rubyhexagon/api/user.rb', line 50

def self.list(query)
  raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash)
  E621::API.fetch(:user, :index, query).map do |user|
    new(user)
  end
end

.show(user) ⇒ E621::User

Fetch data for user

Examples:

Get user information

E621::User.show(id: 1) #=> E621::User

Parameters:

  • user (Hash|E621::User)

    User data to fetch from

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



34
35
36
37
38
39
40
41
# File 'lib/rubyhexagon/api/user.rb', line 34

def self.show(user)
  unless (user.is_a?(Hash) && user[:id].is_a?(Integer)) ||
         user.is_a?(E621::User)
    raise ArgumentError, 'A Hash or user object are required'
  end
  user = user.is_a?(Hash) ? user : { id: user.id }
  new(E621::API.fetch(:user, :show, user))
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for User objects

Examples:

Check two different users

User.new(id: 1) == User.new(id: 2) #=> false

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



113
114
115
# File 'lib/rubyhexagon/user.rb', line 113

def ==(other)
  other.is_a?(User) && @id == other.id
end

#showE621::User

User information

Examples:

Get User Information

user.show #=> E621::User

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



63
64
65
# File 'lib/rubyhexagon/api/user.rb', line 63

def show
  E621::User.new(E621::API.fetch(:user, :show, id: @id))
end

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn a User into a Hash

User.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



99
100
101
102
103
104
105
# File 'lib/rubyhexagon/user.rb', line 99

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end