Class: Rubyhexagon::User
- Inherits:
-
Object
- Object
- Rubyhexagon::User
- 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.
Defined Under Namespace
Classes: Level
Instance Attribute Summary collapse
-
#artist_tags ⇒ Array<E621::Tag>
readonly
Artists tags, that belong to user.
-
#avatar ⇒ E621::Post
readonly
Avatar post that user chosen.
-
#created_at ⇒ Time
readonly
Time user was created.
-
#id ⇒ Integer
readonly
User ID.
-
#level ⇒ E621::User::Level
readonly
User access level.
-
#name ⇒ String
readonly
User name.
Class Method Summary collapse
-
.list(query) ⇒ E621::User
Fetch a list of users.
-
.show(user) ⇒ E621::User
Fetch data for user.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison method for User objects.
-
#initialize(user) ⇒ Object
constructor
Initializer for User.
-
#show ⇒ E621::User
User information.
-
#to_hash ⇒ Hash
Turn object into a hash representation of itself.
Constructor Details
#initialize(user) ⇒ Object
Initializer for User
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_tags ⇒ Array<E621::Tag> (readonly)
Artists tags, that belong to user
62 63 64 |
# File 'lib/rubyhexagon/user.rb', line 62 def @artist_tags end |
#avatar ⇒ E621::Post (readonly)
Avatar post that user chosen
56 57 58 |
# File 'lib/rubyhexagon/user.rb', line 56 def avatar @avatar end |
#created_at ⇒ Time (readonly)
Time user was created
50 51 52 |
# File 'lib/rubyhexagon/user.rb', line 50 def created_at @created_at end |
#id ⇒ Integer (readonly)
User ID
32 33 34 |
# File 'lib/rubyhexagon/user.rb', line 32 def id @id end |
#level ⇒ E621::User::Level (readonly)
User access level
44 45 46 |
# File 'lib/rubyhexagon/user.rb', line 44 def level @level end |
#name ⇒ String (readonly)
User name
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
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
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
113 114 115 |
# File 'lib/rubyhexagon/user.rb', line 113 def ==(other) other.is_a?(User) && @id == other.id end |
#show ⇒ E621::User
User information
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_hash ⇒ Hash
Turn object into a hash representation of itself
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 |