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.

Parameters:

  • user (Hash)

    user data, fetched from e621

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubyhexagon/user.rb', line 52

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)

Returns an array of artist tags, associated with user.

Returns:

  • (Array<E621::Tag>)

    an array of artist tags, associated with user

Since:

  • 1.0.0



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

def artist_tags
  @artist_tags
end

#avatarE621::Post (readonly)

Returns avatar set by user.

Returns:

Since:

  • 1.0.0



40
41
42
# File 'lib/rubyhexagon/user.rb', line 40

def avatar
  @avatar
end

#created_atTime (readonly)

Returns registration time of this user.

Returns:

  • (Time)

    registration time of this user

Since:

  • 1.0.0



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

def created_at
  @created_at
end

#idInteger (readonly)

Returns id of user.

Returns:

  • (Integer)

    id of user

Since:

  • 1.0.0



28
29
30
# File 'lib/rubyhexagon/user.rb', line 28

def id
  @id
end

#levelLevel (readonly)

Returns level of access, this user holds.

Returns:

  • (Level)

    level of access, this user holds

Since:

  • 1.0.0



34
35
36
# File 'lib/rubyhexagon/user.rb', line 34

def level
  @level
end

#nameString (readonly)

Returns name of user.

Returns:

  • (String)

    name of user

Since:

  • 1.0.0



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

def name
  @name
end

Class Method Details

.list(query) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 1.4.0



41
42
43
44
45
46
# File 'lib/rubyhexagon/api/user.rb', line 41

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

Parameters:

  • user (Hash|E621::User)

    User data to fetch from

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

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

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



76
77
78
# File 'lib/rubyhexagon/user.rb', line 76

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

#showObject

Since:

  • 1.4.0



48
49
50
# File 'lib/rubyhexagon/api/user.rb', line 48

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