Class: Rubyhexagon::Post

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

Overview

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0

Direct Known Subclasses

DeletedPost

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Object

Initializer for Post.

Parameters:

  • id (Integer) —

    post id on e621

Author:

  • Maxine Michalski

Since:

  • 1.0.0



89
90
91
92
93
94
95
# File 'lib/rubyhexagon/post.rb', line 89

def initialize(id)
  id = id.to_i
  unless id.is_a?(Integer) && id.positive?
    raise InvalidIDError, "ID out of range: #{id}"
  end
  @id = id
end

Instance Attribute Details

#artists ⇒ Array<Artist> (readonly)

Returns array of artists, that work on the image in this post.

Returns:

  • (Array<Artist>) —

    array of artists, that work on the image in this post

Since:

  • 1.0.0



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

def artists
  @artists
end

#author ⇒ User (readonly)

Returns user object of who uploaded this post.

Returns:

  • (User) —

    user object of who uploaded this post

Since:

  • 1.0.0



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

def author
  @author
end

#children ⇒ Array<Post> (readonly)

Returns Array of child posts.

Returns:

  • (Array<Post>) —

    Array of child posts

Since:

  • 1.0.0



65
66
67
# File 'lib/rubyhexagon/post.rb', line 65

def children
  @children
end

#created_at ⇒ Time (readonly)

Returns creation time of post.

Returns:

  • (Time) —

    creation time of post

Since:

  • 1.0.0



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

def created_at
  @created_at
end

#description ⇒ String (readonly)

Returns description that's associated with this post.

Returns:

  • (String) —

    description that's associated with this post

Since:

  • 1.0.0



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

def description
  @description
end

#favorites ⇒ Integer (readonly)

Returns number of people, who favorited this post.

Returns:

  • (Integer) —

    number of people, who favorited this post

Since:

  • 1.0.0



51
52
53
# File 'lib/rubyhexagon/post.rb', line 51

def favorites
  @favorites
end

#id ⇒ Integer (readonly)

Returns id of post information.

Returns:

  • (Integer) —

    id of post information

Since:

  • 1.0.0



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

def id
  @id
end

#image ⇒ Rubyhexagon::Image (readonly)

Returns file content of this post.

Returns:

Since:

  • 1.0.0



71
72
73
# File 'lib/rubyhexagon/post.rb', line 71

def image
  @image
end

#md5 ⇒ String (readonly)

Returns MD5 checksum associated with this post.

Returns:

  • (String) —

    MD5 checksum associated with this post

Since:

  • 1.0.0



68
69
70
# File 'lib/rubyhexagon/post.rb', line 68

def md5
  @md5
end

#parent ⇒ Post, NilClass (readonly)

Returns:

  • (Post) —

    parent of this post

  • (NilClass)

Since:

  • 1.0.0



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

def parent
  @parent
end

#preview ⇒ Rubyhexagon::Preview (readonly)

Returns preview data in Rubyhexagon::Preview format.

Returns:

Since:

  • 1.0.0



77
78
79
# File 'lib/rubyhexagon/post.rb', line 77

def preview
  @preview
end

#rating ⇒ Symbol (readonly)

Returns rating of this post. Can be one of :safe, :questionable, :explicit.

Returns:

  • (Symbol) —

    rating of this post. Can be one of :safe, :questionable, :explicit

Since:

  • 1.0.0



58
59
60
# File 'lib/rubyhexagon/post.rb', line 58

def rating
  @rating
end

#sample ⇒ Rubyhexagon::Sample (readonly)

Returns sample data in Rubyhexagon::Sample format.

Returns:

Since:

  • 1.0.0



74
75
76
# File 'lib/rubyhexagon/post.rb', line 74

def sample
  @sample
end

#score ⇒ Integer (readonly)

Returns vote score this post holds.

Returns:

  • (Integer) —

    vote score this post holds

Since:

  • 1.0.0



54
55
56
# File 'lib/rubyhexagon/post.rb', line 54

def score
  @score
end

#sources ⇒ Array<String> (readonly)

Returns array of sources. This can contain URI in string form.

Returns:

  • (Array<String>) —

    array of sources. This can contain URI in string form

Since:

  • 1.0.0



41
42
43
# File 'lib/rubyhexagon/post.rb', line 41

def sources
  @sources
end

#status ⇒ Symbol (readonly)

Returns status of post, can be :active, :flagged, :pending or :deleted.

Returns:

  • (Symbol) —

    status of post, can be :active, :flagged, :pending or :deleted

Since:

  • 1.0.0



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

def status
  @status
end

#tag_names ⇒ Array<String> (readonly)

Returns tag names only for convenience.

Returns:

  • (Array<String>) —

    tag names only for convenience

Since:

  • 1.0.0



80
81
82
# File 'lib/rubyhexagon/post.rb', line 80

def tag_names
  @tag_names
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for posts

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



161
162
163
# File 'lib/rubyhexagon/post.rb', line 161

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

#show(post = nil) ⇒ Object

Fetch data from e621 to current post id

Parameters:

  • post (Hash) (defaults to: nil) —

    post data, if present

Author:

  • Maxine Michalski

Since:

  • 1.0.0



117
118
119
120
121
# File 'lib/rubyhexagon/post.rb', line 117

def show(post = nil)
  tmp = Post.new(@id)
  tmp.show!(post)
  tmp
end

#show!(post = nil) ⇒ Object

Fetch data from e621 to current post id

Parameters:

  • post (Hash) (defaults to: nil) —

    post data, if present

Author:

  • Maxine Michalski

Since:

  • 1.0.0



102
103
104
105
106
107
108
109
110
# File 'lib/rubyhexagon/post.rb', line 102

def show!(post = nil)
  post = API.new.fetch('post', 'show', id: @id) if post.nil?
  @created_at = Time.at(post[:created_at][:s].to_i)
  @status = post[:status].to_sym
  setup_author(post)
  setup_relatives(post)
  setup_file_data(post)
  setup_additional_data(post)
end

#tags ⇒ Array<Tag>

Retrieve an Array of this post's tags

Returns:

  • (Array<Tag>) —

    array of tags

Author:

  • Maxine Michalski

Since:

  • 1.0.0



128
129
130
131
132
# File 'lib/rubyhexagon/post.rb', line 128

def tags
  API.new.fetch('post', 'tags', id: @id).map do |tag|
    Tag.new(tag[:id]).show(tag)
  end
end

#test(test_var = nil) ⇒ TrueClass, FalseClass Also known as: active?, flagged?, pending?, deleted?, safe?, questionable?, explicit?

Test status with an optional parameter. This is more meant to be used by alias methods

Parameters:

  • test_var (Symbol) (defaults to: nil) —

    variable under test

Returns:

  • (TrueClass)
  • (FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



143
144
145
146
147
# File 'lib/rubyhexagon/post.rb', line 143

def test(test_var = nil)
  test_var ||= __callee__.to_s.sub(/\?/, '').to_sym
  test_ar = %i[safe questionable explicit]
  test_ar.include?(test_var) ? @rating == test_var : @status == test_var
end