Class: Rubyhexagon::Post
- Inherits:
-
Object
- Object
- Rubyhexagon::Post
- Defined in:
- lib/rubyhexagon/post.rb,
lib/rubyhexagon/api/post.rb,
lib/rubyhexagon/post/flag.rb,
lib/rubyhexagon/post/note.rb,
lib/rubyhexagon/post/image.rb,
lib/rubyhexagon/api/post/flag.rb,
lib/rubyhexagon/api/post/note.rb,
lib/rubyhexagon/post/tag_item.rb,
lib/rubyhexagon/api/post/tag_item.rb
Overview
A class to interact with the e621 web interface.
Defined Under Namespace
Classes: Flag, Image, Note, Preview, Sample, TagItem
Instance Attribute Summary collapse
-
#artists ⇒ Array<E621::Artist>
readonly
Post artists.
-
#author ⇒ E621::User
readonly
Post author.
-
#children ⇒ Array<E621::Post>
readonly
Post children.
-
#created_at ⇒ Time
readonly
Time post was created at.
-
#delreason ⇒ String|NilClass
readonly
Deletion reason of post, if any.
-
#description ⇒ String|NilClass
readonly
Post desciption.
-
#fav_count ⇒ Integer
readonly
Count of users who favorited this post.
-
#id ⇒ Integer
readonly
Post ID.
-
#image ⇒ E621::Image
readonly
Image of post.
-
#md5 ⇒ String
readonly
Post MD5 checksum.
-
#parent ⇒ E621:post, NilClass
readonly
Post parent.
-
#preview ⇒ E621::Preview
readonly
Preview of post.
-
#rating ⇒ Symbol
readonly
Post rating.
-
#sample ⇒ E621::Sample
readonly
Sample of post.
-
#score ⇒ Integer
readonly
Post score.
-
#sources ⇒ Array<String>
readonly
Post sources.
-
#status ⇒ Symbol
readonly
Post status.
-
#tags ⇒ Array<E621::Tag>
readonly
Tags of post.
Class Method Summary collapse
-
.deleted(query) ⇒ Array<E621::Post>
Fetch a list of deleted posts.
-
.list(query) ⇒ Array<E621::Post>
Fetch a list of posts.
-
.popular_by(period) ⇒ Array<E621::Post>
Fetch a list of popular posts.
-
.show(post) ⇒ E621::Post
Fetch data for post.
-
.tags(post) ⇒ Array<E621::Tag>
Fetch tag information.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison method for posts.
-
#children? ⇒ TrueClass|FalseClass
Show if post has children.
-
#comments? ⇒ TrueClass|FalseClass
Show if post has comments or not.
-
#download(which) ⇒ Tempfile
Download a file from a post.
-
#initialize(post) ⇒ Object
constructor
Initializer for Post.
-
#notes ⇒ E621::Post::Note
List of notes, that belong to a post.
-
#notes? ⇒ TrueClass|FalseClass
Has post notes?.
-
#parent? ⇒ TrueClass|FalseClass
Show if post has a parent.
-
#show ⇒ E621::Post
Fetch data for a post.
-
#test(test_var = nil) ⇒ TrueClass|FalseClass
(also: #active?, #flagged?, #pending?, #deleted?, #safe?, #questionable?, #explicit?)
private
Test status with an optional parameter.
-
#to_hash ⇒ Hash
Turn object into a hash representation of itself.
Constructor Details
#initialize(post) ⇒ Object
Initializer for Post
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rubyhexagon/post.rb', line 149 def initialize(post) raise ArgumentError, "#{post.class} is not a Hash" unless post.is_a?(Hash) post[:id] = post[:id].to_i id = post[:id] raise InvalidIDError, "ID out of range: #{id}" unless id.positive? post.each do |k, v| if %i[rating created_at status sources parent_id children description artist tags].include?(k) __send__("setup_#{k}".to_sym, v) elsif %i[id description score fav_count md5 has_comments has_children delreason has_notes].include?(k) instance_variable_set("@#{k}".to_sym, v) end end (post) setup_files(post) end |
Instance Attribute Details
#artists ⇒ Array<E621::Artist> (readonly)
Post artists
in this post
65 66 67 |
# File 'lib/rubyhexagon/post.rb', line 65 def artists @artists end |
#author ⇒ E621::User (readonly)
Post author
37 38 39 |
# File 'lib/rubyhexagon/post.rb', line 37 def @author end |
#children ⇒ Array<E621::Post> (readonly)
Post children
103 104 105 |
# File 'lib/rubyhexagon/post.rb', line 103 def children @children end |
#created_at ⇒ Time (readonly)
Time post was created at
43 44 45 |
# File 'lib/rubyhexagon/post.rb', line 43 def created_at @created_at end |
#delreason ⇒ String|NilClass (readonly)
Deletion reason of post, if any
133 134 135 |
# File 'lib/rubyhexagon/post.rb', line 133 def delreason @delreason end |
#description ⇒ String|NilClass (readonly)
Post desciption
71 72 73 |
# File 'lib/rubyhexagon/post.rb', line 71 def description @description end |
#fav_count ⇒ Integer (readonly)
Count of users who favorited this post
77 78 79 |
# File 'lib/rubyhexagon/post.rb', line 77 def fav_count @fav_count end |
#id ⇒ Integer (readonly)
Post ID
31 32 33 |
# File 'lib/rubyhexagon/post.rb', line 31 def id @id end |
#image ⇒ E621::Image (readonly)
Image of post
115 116 117 |
# File 'lib/rubyhexagon/post.rb', line 115 def image @image end |
#md5 ⇒ String (readonly)
Post MD5 checksum
109 110 111 |
# File 'lib/rubyhexagon/post.rb', line 109 def md5 @md5 end |
#parent ⇒ E621:post, NilClass (readonly)
Post parent
97 98 99 |
# File 'lib/rubyhexagon/post.rb', line 97 def parent @parent end |
#preview ⇒ E621::Preview (readonly)
Preview of post
127 128 129 |
# File 'lib/rubyhexagon/post.rb', line 127 def preview @preview end |
#rating ⇒ Symbol (readonly)
Post rating
90 91 92 |
# File 'lib/rubyhexagon/post.rb', line 90 def @rating end |
#sample ⇒ E621::Sample (readonly)
Sample of post
121 122 123 |
# File 'lib/rubyhexagon/post.rb', line 121 def sample @sample end |
#score ⇒ Integer (readonly)
Post score
83 84 85 |
# File 'lib/rubyhexagon/post.rb', line 83 def score @score end |
#sources ⇒ Array<String> (readonly)
Post sources
57 58 59 |
# File 'lib/rubyhexagon/post.rb', line 57 def sources @sources end |
#status ⇒ Symbol (readonly)
Post status
50 51 52 |
# File 'lib/rubyhexagon/post.rb', line 50 def status @status end |
#tags ⇒ Array<E621::Tag> (readonly)
Tags of post
139 140 141 |
# File 'lib/rubyhexagon/post.rb', line 139 def @tags end |
Class Method Details
.deleted(query) ⇒ Array<E621::Post>
Fetch a list of deleted posts
88 89 90 91 92 93 |
# File 'lib/rubyhexagon/api/post.rb', line 88 def self.deleted(query) raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash) E621::API.fetch(:post, :deleted_index, query).map do |post| new(post) end end |
.list(query) ⇒ Array<E621::Post>
Fetch a list of posts
73 74 75 76 77 78 |
# File 'lib/rubyhexagon/api/post.rb', line 73 def self.list(query) raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash) E621::API.fetch(:post, :index, query).map do |post| new(post) end end |
.popular_by(period) ⇒ Array<E621::Post>
Fetch a list of popular posts
103 104 105 106 107 108 109 110 |
# File 'lib/rubyhexagon/api/post.rb', line 103 def self.popular_by(period) unless %i[day week month].include?(period) raise ArgumentError, 'Period must be day, week or month' end E621::API.fetch(:post, "popular_by_#{period}".to_sym, {}).map do |post| new(post) end end |
.show(post) ⇒ E621::Post
Fetch data for post
35 36 37 38 39 40 41 42 |
# File 'lib/rubyhexagon/api/post.rb', line 35 def self.show(post) unless (post.is_a?(Hash) && post[:id].is_a?(Integer)) || post.is_a?(E621::Post) raise ArgumentError, 'A Hash or post data object are required' end id = post.is_a?(Hash) ? post[:id] : post.id new(E621::API.fetch(:post, :show, id: id)) end |
.tags(post) ⇒ Array<E621::Tag>
This method fetches complete tag information for a post
Fetch tag information
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubyhexagon/api/post.rb', line 54 def self.(post) unless (post.is_a?(Hash) && post[:id].is_a?(Integer)) || post.is_a?(E621::Post) raise ArgumentError, 'A Hash or post data object are required' end id = post.is_a?(Hash) ? post[:id] : post.id E621::API.fetch(:post, :tags, id: id).map do |t| E621::Tag.new(t) end end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
Comparison method for posts
241 242 243 |
# File 'lib/rubyhexagon/post.rb', line 241 def ==(other) other.is_a?(Post) && @id == other.id end |
#children? ⇒ TrueClass|FalseClass
Show if post has children
230 231 232 233 |
# File 'lib/rubyhexagon/post.rb', line 230 def children? return false if @has_children.nil? @has_children end |
#comments? ⇒ TrueClass|FalseClass
Show if post has comments or not
209 210 211 212 |
# File 'lib/rubyhexagon/post.rb', line 209 def comments? return false if @has_comments.nil? @has_comments end |
#download(which) ⇒ Tempfile
Download a file from a post
131 132 133 134 135 136 |
# File 'lib/rubyhexagon/api/post.rb', line 131 def download(which) unless %i[image preview sample].include?(which) raise ArgumentError, 'Unsupported doanload' end E621::API.download(__send__(which).url) end |
#notes ⇒ E621::Post::Note
List of notes, that belong to a post
146 147 148 149 150 |
# File 'lib/rubyhexagon/api/post.rb', line 146 def notes E621::API.fetch(:note, :index, post_id: @id).map do |n| E621::Post::Note.new(n) end end |
#notes? ⇒ TrueClass|FalseClass
Has post notes?
251 252 253 |
# File 'lib/rubyhexagon/post.rb', line 251 def notes? @has_notes end |
#parent? ⇒ TrueClass|FalseClass
Show if post has a parent
220 221 222 |
# File 'lib/rubyhexagon/post.rb', line 220 def parent? !@parent.nil? end |
#show ⇒ E621::Post
Fetch data for a post
120 121 122 |
# File 'lib/rubyhexagon/api/post.rb', line 120 def show E621::Post.new(E621::API.fetch(:post, :show, id: @id)) end |
#test(test_var = nil) ⇒ TrueClass|FalseClass Also known as: active?, flagged?, pending?, deleted?, safe?, questionable?, explicit?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Test status with an optional parameter
176 177 178 179 180 |
# File 'lib/rubyhexagon/post.rb', line 176 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 |
#to_hash ⇒ Hash
Turn object into a hash representation of itself
262 263 264 265 266 267 268 |
# File 'lib/rubyhexagon/post.rb', line 262 def to_hash hash = {} instance_variables.each do |i| hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i)) end hash end |