Class: Rubyhexagon::Pool

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

Overview

This class holds post information, fetched from e621, and gives access in a more Ruby like manner.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Object

Initializer for Pool objects

Parameters:

  • id (Integer)

    ID of this pool

Author:

  • Maxine Michalski

Since:

  • 1.4.0



54
55
56
57
58
59
# File 'lib/rubyhexagon/pool.rb', line 54

def initialize(id)
  unless id.is_a?(Integer) && id.positive?
    raise InvalidIDError, "ID out of range: #{id}"
  end
  @id = id
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

#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

#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



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

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

#userUser (readonly)

Returns the user, who has created this pool

Returns:

  • (User)

    returns the user, who has created this pool

Since:

  • 1.4.0



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

def user
  @user
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



124
125
126
127
128
129
130
131
# File 'lib/rubyhexagon/pool.rb', line 124

def ==(other)
  if @name.nil?
    other.is_a?(Pool) && @id == other.id
  else
    other.is_a?(Pool) && @name == other.name && @id == other.id &&
      @updated_at == other.updated_at
  end
end

#active?TrueClass, FalseClass

Returns active status

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

def active?
  @is_active
end

#locked?TrueClass, FalseClass

Returns locking status

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



104
105
106
# File 'lib/rubyhexagon/pool.rb', line 104

def locked?
  @is_locked
end

#show(pool = nil) ⇒ Object

Fetch data from e621 to current pool id

Parameters:

  • pool (Hash) (defaults to: nil)

    pool data, if present

Author:

  • Maxine Michalski

Since:

  • 1.4.0



93
94
95
96
97
# File 'lib/rubyhexagon/pool.rb', line 93

def show(pool = nil)
  tmp = Pool.new(@id)
  tmp.show!(pool)
  tmp
end

#show!(pool = nil) ⇒ Object

Fetch data from e621 to current post id

Parameters:

  • pool (Hash) (defaults to: nil)

    pool data, if present

Author:

  • Maxine Michalski

Since:

  • 1.4.0



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubyhexagon/pool.rb', line 66

def show!(pool = nil)
  @posts = []
  if pool.nil?
    page = 1
    loop do
      pool = API.new.fetch('pool', 'show', id: @id, page: page)
      page += 1
      @posts += pool[:posts].map do |post|
        E621::Post.new(post[:id]).show(post)
      end
      break if pool[:posts] == []
    end
  end
  @name = pool[:name]
  @created_at = Time.at(pool[:created_at][:s].to_i)
  @updated_at = Time.at(pool[:updated_at][:s].to_i)
  @description = pool[:description]
  @user = User.new(id: pool[:user_id])
  @is_locked = pool[:is_locked]
  @is_active = pool[:is_active]
end