Class: Rubyhexagon::Pool

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

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Object

Initializer for Pool objects

Parameters:

  • pool (Hash)

    Pool information

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubyhexagon/pool.rb', line 57

def initialize(pool)
  raise ArgumentError, "#{pool.class} is not a Hash" unless pool.is_a?(Hash)
  raise ArgumentError, 'Hash must include :id' if pool[:id].nil?
  pool.each do |k, v|
    if %i[id name post_count is_active is_locked description].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[updated_at created_at].include?(k)
      instance_variable_set("@#{k}".to_sym, Time.at(v[:s]))
    elsif k == :user_id
      @creator = E621::User.new(id: v)
    elsif k == :posts
      @posts = v.map { |post| E621::Post.new(post) }
    end
  end
end

Instance Attribute Details

#created_atTime (readonly)

Returns creation time

Returns:

  • (Time)

    returns creation time

Since:

  • 1.4.0



33
34
35
# File 'lib/rubyhexagon/pool.rb', line 33

def created_at
  @created_at
end

#creatorE621::User (readonly)

Returns the user, who has created this pool

Returns:

  • (E621::User)

    returns the user, who has created this pool

Since:

  • 1.4.0



45
46
47
# File 'lib/rubyhexagon/pool.rb', line 45

def creator
  @creator
end

#descriptionString (readonly)

Returns a description of the current pool

Returns:

  • (String)

    returns a description of the current pool

Since:

  • 1.4.0



39
40
41
# File 'lib/rubyhexagon/pool.rb', line 39

def description
  @description
end

#idInteger (readonly)

Returns this pool’s ID

Returns:

  • (Integer)

    returns this pool’s ID

Since:

  • 1.4.0



27
28
29
# File 'lib/rubyhexagon/pool.rb', line 27

def id
  @id
end

#nameString (readonly)

Returns name of current pool

Returns:

  • (String)

    returns name of current pool

Since:

  • 1.4.0



30
31
32
# File 'lib/rubyhexagon/pool.rb', line 30

def name
  @name
end

#post_countInteger (readonly)

Returns number of posts inside pool

Returns:

  • (Integer)

    returns number of posts inside pool

Since:

  • 1.4.0



42
43
44
# File 'lib/rubyhexagon/pool.rb', line 42

def post_count
  @post_count
end

#postsArray<Post> (readonly)

Returns an array of posts, that belong to this pool

Returns:

  • (Array<Post>)

    returns an array of posts, that belong to this pool

Since:

  • 1.4.0



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

def posts
  @posts
end

#updated_atTime (readonly)

Returns time of last update

Returns:

  • (Time)

    returns time of last update

Since:

  • 1.4.0



36
37
38
# File 'lib/rubyhexagon/pool.rb', line 36

def updated_at
  @updated_at
end

Class Method Details

.list(query) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 1.4.0



37
38
39
40
41
42
# File 'lib/rubyhexagon/api/pool.rb', line 37

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

.show(pool, page = nil) ⇒ Object

Since:

  • 1.4.0



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubyhexagon/api/pool.rb', line 25

def self.show(pool, page = nil)
  unless (pool.is_a?(Hash) && pool[:id].is_a?(Integer)) ||
         pool.is_a?(E621::Pool)
    raise ArgumentError, 'A Hash or pool object are required'
  end
  id = pool.is_a?(Hash) ? pool[:id] : pool.id
  page = page.nil? ? pool[:page] : page[:page]
  E621::API.fetch(:pool, :show, id: id, page: page)[:posts].map do |post|
    E621::Post.new(post)
  end
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for Pool objects

Parameters:

  • other (Object)

    other object to compare

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



101
102
103
# File 'lib/rubyhexagon/pool.rb', line 101

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

#active?TrueClass, FalseClass

Returns active status

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



90
91
92
# File 'lib/rubyhexagon/pool.rb', line 90

def active?
  @is_active
end

#locked?TrueClass, FalseClass

Returns locking status

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



81
82
83
# File 'lib/rubyhexagon/pool.rb', line 81

def locked?
  @is_locked
end

#show(page) ⇒ Object

Since:

  • 1.4.0



44
45
46
47
48
49
# File 'lib/rubyhexagon/api/pool.rb', line 44

def show(page)
  page = page[:page]
  E621::API.fetch(:pool, :show, id: @id, page: page)[:posts].map do |post|
    E621::Post.new(post)
  end
end