Class: Rubyhexagon::Post
- Inherits:
-
Object
- Object
- Rubyhexagon::Post
- Defined in:
- lib/rubyhexagon/post.rb
Overview
This class holds post information, fetched from e621, and gives access in a more Ruby like manner.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#artists ⇒ Array<Artist>
readonly
Array of artists, that work on the image in this post.
-
#author ⇒ User
readonly
User object of who uploaded this post.
-
#children ⇒ Array<Post>
readonly
Array of child posts.
-
#created_at ⇒ Time
readonly
Creation time of post.
-
#description ⇒ String
readonly
Description that's associated with this post.
-
#favorites ⇒ Integer
readonly
Number of people, who favorited this post.
-
#id ⇒ Integer
readonly
Id of post information.
-
#image ⇒ Rubyhexagon::Image
readonly
File content of this post.
-
#md5 ⇒ String
readonly
MD5 checksum associated with this post.
- #parent ⇒ Post, NilClass readonly
-
#preview ⇒ Rubyhexagon::Preview
readonly
Preview data in Rubyhexagon::Preview format.
-
#rating ⇒ Symbol
readonly
Rating of this post.
-
#sample ⇒ Rubyhexagon::Sample
readonly
Sample data in Rubyhexagon::Sample format.
-
#score ⇒ Integer
readonly
Vote score this post holds.
-
#sources ⇒ Array<String>
readonly
Array of sources.
-
#status ⇒ Symbol
readonly
Status of post, can be :active, :flagged, :pending or :deleted.
-
#tag_names ⇒ Array<String>
readonly
Tag names only for convenience.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison method for posts.
-
#favorite! ⇒ Object
Favorite this post.
-
#initialize(id) ⇒ Object
constructor
Initializer for Post.
-
#show(post = nil) ⇒ Object
Fetch data from e621 to current post id.
-
#show!(post = nil) ⇒ Object
Fetch data from e621 to current post id.
-
#tags ⇒ Array<Tag>
Retrieve an Array of this post's tags.
-
#test(test_var = nil) ⇒ TrueClass, FalseClass
(also: #active?, #flagged?, #pending?, #deleted?, #safe?, #questionable?, #explicit?)
Test status with an optional parameter.
-
#unfavorite! ⇒ Object
Unfavorite this post.
-
#vote!(score) ⇒ Object
Send a vote to e621.
-
#votedown! ⇒ Object
Downvotes this post.
-
#voteup! ⇒ Object
Upvotes this post.
Constructor Details
#initialize(id) ⇒ Object
Initializer for Post.
89 90 91 92 93 |
# File 'lib/rubyhexagon/post.rb', line 89 def initialize(id) id = id.to_i raise InvalidIDError, "ID out of range: #{id}" unless id.positive? @id = id end |
Instance Attribute Details
#artists ⇒ Array<Artist> (readonly)
Returns array of artists, that work on the image in this post.
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.
30 31 32 |
# File 'lib/rubyhexagon/post.rb', line 30 def @author end |
#children ⇒ Array<Post> (readonly)
Returns Array of child posts.
65 66 67 |
# File 'lib/rubyhexagon/post.rb', line 65 def children @children end |
#created_at ⇒ Time (readonly)
Returns creation time of post.
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.
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.
51 52 53 |
# File 'lib/rubyhexagon/post.rb', line 51 def favorites @favorites end |
#id ⇒ Integer (readonly)
Returns id of post information.
27 28 29 |
# File 'lib/rubyhexagon/post.rb', line 27 def id @id end |
#image ⇒ Rubyhexagon::Image (readonly)
Returns file content of this post.
71 72 73 |
# File 'lib/rubyhexagon/post.rb', line 71 def image @image end |
#md5 ⇒ String (readonly)
Returns MD5 checksum associated with this post.
68 69 70 |
# File 'lib/rubyhexagon/post.rb', line 68 def md5 @md5 end |
#parent ⇒ Post, NilClass (readonly)
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.
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.
58 59 60 |
# File 'lib/rubyhexagon/post.rb', line 58 def @rating end |
#sample ⇒ Rubyhexagon::Sample (readonly)
Returns sample data in Rubyhexagon::Sample format.
74 75 76 |
# File 'lib/rubyhexagon/post.rb', line 74 def sample @sample end |
#score ⇒ Integer (readonly)
Returns vote score this post holds.
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.
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.
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.
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
216 217 218 |
# File 'lib/rubyhexagon/post.rb', line 216 def ==(other) other.is_a?(Post) && @id == other.id end |
#favorite! ⇒ Object
Favorite this post
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rubyhexagon/post.rb', line 156 def favorite! res = API.new.fetch('favorite', 'create', { id: @id }, 'POST') if res[:reason] == 'access denied' && !res[:success] raise Rubyhexagon::AccessDenied, res[:message] elsif !res[:success] && res[:reason] == 'You\'ve already favorited this post' raise Rubyhexagon::AlreadyFavorited end nil end |
#show(post = nil) ⇒ Object
Fetch data from e621 to current post id
115 116 117 118 119 |
# File 'lib/rubyhexagon/post.rb', line 115 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
100 101 102 103 104 105 106 107 108 |
# File 'lib/rubyhexagon/post.rb', line 100 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 (post) setup_relatives(post) setup_file_data(post) setup_additional_data(post) end |
#tags ⇒ Array<Tag>
Retrieve an Array of this post's tags
183 184 185 186 187 |
# File 'lib/rubyhexagon/post.rb', line 183 def 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
198 199 200 201 202 |
# File 'lib/rubyhexagon/post.rb', line 198 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 |
#unfavorite! ⇒ Object
Unfavorite this post
170 171 172 173 174 175 176 |
# File 'lib/rubyhexagon/post.rb', line 170 def unfavorite! res = API.new.fetch('favorite', 'destroy', { id: @id }, 'POST') if res[:reason] == 'access denied' && !res[:success] raise Rubyhexagon::AccessDenied, res[:message] end nil end |
#vote!(score) ⇒ Object
Send a vote to e621
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rubyhexagon/post.rb', line 126 def vote!(score) unless score.is_a?(Integer) raise ArgumentError, 'Voting requires a positive/negative number' end raise ArgumentError, "Vote can't be zero" if score.zero? score = score.positive? ? 1 : -1 res = API.new.fetch('post', 'vote', { id: @id, score: score }, 'POST') if res[:reason] == 'access denied' && !res[:success] raise Rubyhexagon::AccessDenied, res[:message] end nil end |
#votedown! ⇒ Object
Downvotes this post
149 150 151 |
# File 'lib/rubyhexagon/post.rb', line 149 def votedown! vote(-1) end |
#voteup! ⇒ Object
Upvotes this post
142 143 144 |
# File 'lib/rubyhexagon/post.rb', line 142 def voteup! vote(1) end |