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/image.rb,
lib/rubyhexagon/api/post/flag.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, Preview, Sample, TagItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post) ⇒ Object

Initializer for Post.

Parameters:

  • post (Hash)

    post data

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubyhexagon/post.rb', line 92

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)

in this post

Returns:

  • (Array<E621::Artist>)

    array of artists, that work on the image

Since:

  • 1.0.0



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

def artists
  @artists
end

#authorE621::User (readonly)

Returns user object of who uploaded this post.

Returns:

  • (E621::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

#childrenArray<E621::Post> (readonly)

Returns Array of child posts.

Returns:

Since:

  • 1.0.0



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

def children
  @children
end

#created_atTime (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

#delreasonString|NilClass (readonly)

Returns reason for deletion, if deleted.

Returns:

  • (String|NilClass)

    reason for deletion, if deleted

Since:

  • 1.0.0



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

def delreason
  @delreason
end

#descriptionString (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

#fav_countInteger (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 fav_count
  @fav_count
end

#idInteger (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

#imageE621::Image (readonly)

Returns file content of this post.

Returns:

  • (E621::Image)

    file content of this post

Since:

  • 1.0.0



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

def image
  @image
end

#md5String (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

#parentE621:post, NilClass (readonly)

Returns:

  • (E621:post)

    parent of this post

  • (NilClass)

Since:

  • 1.0.0



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

def parent
  @parent
end

#previewE621::Preview (readonly)

Returns preview data in E621::Preview format.

Returns:

  • (E621::Preview)

    preview data in E621::Preview format

Since:

  • 1.0.0



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

def preview
  @preview
end

#ratingSymbol (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

#sampleE621::Sample (readonly)

Returns sample data in E621::Sample format.

Returns:

  • (E621::Sample)

    sample data in E621::Sample format

Since:

  • 1.0.0



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

def sample
  @sample
end

#scoreInteger (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

#sourcesArray<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

#statusSymbol (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

#tagsArray<E621::Tag> (readonly)

Returns tags of this post.

Returns:

Since:

  • 1.0.0



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

def tags
  @tags
end

Class Method Details

.deleted(query) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 1.4.0



59
60
61
62
63
64
# File 'lib/rubyhexagon/api/post.rb', line 59

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) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 1.4.0



52
53
54
55
56
57
# File 'lib/rubyhexagon/api/post.rb', line 52

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

Since:

  • 1.4.0



66
67
68
69
70
71
72
73
# File 'lib/rubyhexagon/api/post.rb', line 66

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

Parameters:

  • user (Hash|E621::Post)

    User data to fetch from

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



32
33
34
35
36
37
38
39
# File 'lib/rubyhexagon/api/post.rb', line 32

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) ⇒ Object

Since:

  • 1.4.0



41
42
43
44
45
46
47
48
49
50
# File 'lib/rubyhexagon/api/post.rb', line 41

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

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



166
167
168
# File 'lib/rubyhexagon/post.rb', line 166

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

#children?TrueClass|FalseClass

Show if post has children

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



156
157
158
159
# File 'lib/rubyhexagon/post.rb', line 156

def children?
  return false if @has_children.nil?
  @has_children
end

#comments?TrueClass|FalseClass

Show if post has comments or not

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

def comments?
  return false if @has_comments.nil?
  @has_comments
end

#download(which) ⇒ Object

Since:

  • 1.4.0



79
80
81
82
83
84
# File 'lib/rubyhexagon/api/post.rb', line 79

def download(which)
  unless %i[image preview sample].include?(which)
    raise ArgumentError, 'Unsupported doanload'
  end
  E621::API.download(__send__(which).url)
end

#notesObject

Since:

  • 1.4.0



86
87
88
89
90
# File 'lib/rubyhexagon/api/post.rb', line 86

def notes
  E621::API.fetch(:note, :index, post_id: @id).map do |n|
    E621::Note.new(n)
  end
end

#notes?TrueClass, FalseClass

Has post notes?

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



175
176
177
# File 'lib/rubyhexagon/post.rb', line 175

def notes?
  @has_notes
end

#parent?TrueClass|FalseClass

Show if post has a parent

Returns:

  • (TrueClass|FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



147
148
149
# File 'lib/rubyhexagon/post.rb', line 147

def parent?
  !@parent.nil?
end

#showObject

Since:

  • 1.4.0



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

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?

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



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

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