Class: Rubyhexagon::Post

Inherits:
Object
  • Object
show all
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.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Defined Under Namespace

Classes: Flag, Image, Note, Preview, Sample, TagItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post) ⇒ Object

Initializer for Post

Examples:

Get new post instance

E621::Post.new(id: 1) #=> E621::Post instance

Parameters:

  • post (Hash)

    post data

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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
  setup_author(post)
  setup_files(post)
end

Instance Attribute Details

#artistsArray<E621::Artist> (readonly)

Post artists

in this post

Examples:

Get post artists

post.artists #=> Array<E621::Artist>

Returns:

  • (Array<E621::Artist>)

    array of artists, that work on the image

Since:

  • 1.0.0



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

def artists
  @artists
end

#authorE621::User (readonly)

Post author

Examples:

Get post creator

post.author #=> E621::User

Returns:

  • (E621::User)

    user object of who uploaded this post

Since:

  • 1.0.0



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

def author
  @author
end

#childrenArray<E621::Post> (readonly)

Post children

Examples:

Get post’s children

post.children #=> [] or Array<E621::Post>

Returns:

Since:

  • 1.0.0



103
104
105
# File 'lib/rubyhexagon/post.rb', line 103

def children
  @children
end

#created_atTime (readonly)

Time post was created at

Examples:

Get creation time

post.created_at #=> Time

Returns:

  • (Time)

    creation time of post

Since:

  • 1.0.0



43
44
45
# File 'lib/rubyhexagon/post.rb', line 43

def created_at
  @created_at
end

#delreasonString|NilClass (readonly)

Deletion reason of post, if any

Examples:

Get deletion reason of post

post.delreason #=> String or nil

Returns:

  • (String|NilClass)

    reason for deletion, if deleted

Since:

  • 1.0.0



133
134
135
# File 'lib/rubyhexagon/post.rb', line 133

def delreason
  @delreason
end

#descriptionString|NilClass (readonly)

Post desciption

Examples:

Get post description

post.description #=> String|nil

Returns:

  • (String|NilClass)

    description that’s associated with this post

Since:

  • 1.0.0



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

def description
  @description
end

#fav_countInteger (readonly)

Count of users who favorited this post

Examples:

Get favorite count

post.fav_count #=> Integer

Returns:

  • (Integer)

    number of people, who favorited this post

Since:

  • 1.0.0



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

def fav_count
  @fav_count
end

#idInteger (readonly)

Post ID

Examples:

Get post id

post.id #=> Integer

Returns:

  • (Integer)

    id of post information

Since:

  • 1.0.0



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

def id
  @id
end

#imageE621::Image (readonly)

Image of post

Examples:

Get post’s image

post.image #=> E621::Image

Returns:

  • (E621::Image)

    file content of this post

Since:

  • 1.0.0



115
116
117
# File 'lib/rubyhexagon/post.rb', line 115

def image
  @image
end

#md5String (readonly)

Post MD5 checksum

Examples:

Get post’s MD5 checksum

post.md5 #=> String

Returns:

  • (String)

    MD5 checksum associated with this post

Since:

  • 1.0.0



109
110
111
# File 'lib/rubyhexagon/post.rb', line 109

def md5
  @md5
end

#parentE621:post, NilClass (readonly)

Post parent

Examples:

Get parent of post

post.parent #=> E621::Post or nil

Returns:

  • (E621:post)

    parent of this post

  • (NilClass)

Since:

  • 1.0.0



97
98
99
# File 'lib/rubyhexagon/post.rb', line 97

def parent
  @parent
end

#previewE621::Preview (readonly)

Preview of post

Examples:

Get post’s image

post.preview #=> E621::Preview

Returns:

  • (E621::Preview)

    preview data in E621::Preview format

Since:

  • 1.0.0



127
128
129
# File 'lib/rubyhexagon/post.rb', line 127

def preview
  @preview
end

#ratingSymbol (readonly)

Post rating

Examples:

Get post rating

post.rating #=> Symbol

Returns:

  • (Symbol)

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

Since:

  • 1.0.0



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

def rating
  @rating
end

#sampleE621::Sample (readonly)

Sample of post

Examples:

Get post’s image

post.sample #=> E621::Sample

Returns:

  • (E621::Sample)

    sample data in E621::Sample format

Since:

  • 1.0.0



121
122
123
# File 'lib/rubyhexagon/post.rb', line 121

def sample
  @sample
end

#scoreInteger (readonly)

Post score

Examples:

Get score assigned to post

post.score #=> Integer

Returns:

  • (Integer)

    vote score this post holds

Since:

  • 1.0.0



83
84
85
# File 'lib/rubyhexagon/post.rb', line 83

def score
  @score
end

#sourcesArray<String> (readonly)

Post sources

Examples:

Get post sources

post.sources #=> Array<String>

Returns:

  • (Array<String>)

    array of sources. This can contain URI in string form

Since:

  • 1.0.0



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

def sources
  @sources
end

#statusSymbol (readonly)

Post status

Examples:

Get post status

post.status #=> Symbol

Returns:

  • (Symbol)

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

Since:

  • 1.0.0



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

def status
  @status
end

#tagsArray<E621::Tag> (readonly)

Tags of post

Examples:

Get tags of post, without web interaction

post.tags #=> Array<E621::Tag>

Returns:

Since:

  • 1.0.0



139
140
141
# File 'lib/rubyhexagon/post.rb', line 139

def tags
  @tags
end

Class Method Details

.deleted(query) ⇒ Array<E621::Post>

Fetch a list of deleted posts

Examples:

Get a list of deleted posts

E621::Post.deleted(page: 1) #=> Array<E621::Post>

Returns:

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Examples:

Get a list of posts

E621::Post.list(tags: 'female') #=> Array<E621::Post>

Returns:

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Fetch a list of popular posts

Examples:

Get popular posts, by day

E621::Post.popular_by(:day) #=> E621::Post

Returns:

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Examples:

Get Post information

E621::Post.show(id: 1) #=> E621::Post

Parameters:

  • post (Hash|E621::Post)

    User data to fetch from

Returns:

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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>

Note:

This method fetches complete tag information for a post

Fetch tag information

Examples:

Get tags for post

E621::Post.tags(id: 1) #=> Array<E621::Tag>

Parameters:

  • post (Hash|E621::Post)

    User data to fetch from

Returns:

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubyhexagon/api/post.rb', line 54

def self.tags(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

Examples:

Compare two unequal posts

Post.new(id: 1) == Post.new(id: 2) #=> false

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

Examples:

Has post children?

post.children? #=> true or false

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

Examples:

Get if post has comments

post.comments? #=> true or false

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

Examples:

Download file

post.download(:image) #=> Tempfile

Returns:

  • (Tempfile)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

#notesE621::Post::Note

List of notes, that belong to a post

Examples:

Get nots for a post

post.notes #=> Array<E621::Post::Note>

Returns:

  • (E621::Post::Note)

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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?

Examples:

Post has notes?

post.notes? #=> true or false

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



251
252
253
# File 'lib/rubyhexagon/post.rb', line 251

def notes?
  @has_notes
end

#parent?TrueClass|FalseClass

Show if post has a parent

Examples:

Has post a parent?

post.parent? #=> true or false

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



220
221
222
# File 'lib/rubyhexagon/post.rb', line 220

def parent?
  !@parent.nil?
end

#showE621::Post

Fetch data for a post

Examples:

Get post data

post.show #=> E621::Post

Returns:

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Examples:

Get rating status

post.explicit? #=> true or false

Parameters:

  • test_var (Symbol) (defaults to: nil)

    variable under test

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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_hashHash

Turn object into a hash representation of itself

Examples:

Turn a User into a Hash

Tag.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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