Class: Rubyhexagon::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/post.rb,
lib/rubyhexagon/api/post.rb,
lib/rubyhexagon/post/file.rb,
lib/rubyhexagon/post/score.rb,
lib/rubyhexagon/post/sample.rb,
lib/rubyhexagon/post/preview.rb

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Defined Under Namespace

Classes: File, Preview, Sample, Score

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



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

def initialize(post)
  raise ArgumentError, "#{post.class} is not a Hash" unless post.is_a?(Hash)
  raise ArgumentError, 'Not a proper post Hash' if post.key?(:post)
  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[created_at updated_at file sample preview score flags
          rating relationships].include?(k)
      __send__("setup_#{k}".to_sym, v)
    elsif %i[id tags change_seq sources fav_count description
             comment_count is_favorited].include?(k)
      instance_variable_set("@#{k}", v)
    end
  end
end

Instance Attribute Details

#approverObject (readonly)

Since:

  • 1.0.0



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

def approver
  @approver
end

#change_seqObject (readonly)

Since:

  • 1.0.0



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

def change_seq
  @change_seq
end

#childrenObject (readonly)

Since:

  • 1.0.0



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

def children
  @children
end

#comment_countObject (readonly)

Since:

  • 1.0.0



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

def comment_count
  @comment_count
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



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

def created_at
  @created_at
end

#descriptionObject (readonly)

Since:

  • 1.0.0



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

def description
  @description
end

#fav_countObject (readonly)

Since:

  • 1.0.0



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

def fav_count
  @fav_count
end

#fileE621::Post::File (readonly)

File of post

Examples:

Get post’s file

post.file #=> E621::Post::File

Returns:

  • (E621::Post::File)

    file content of this post

Since:

  • 1.0.0



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

def file
  @file
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

#locked_tagsObject (readonly)

Since:

  • 1.0.0



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

def locked_tags
  @locked_tags
end

#parentObject (readonly)

Since:

  • 1.0.0



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

def parent
  @parent
end

#previewE621::Post::Preview (readonly)

Preview of post

Examples:

Get post’s preview

post.preview #=> E621::Post::Preview

Returns:

  • (E621::Post::Preview)

    preview content of this post

Since:

  • 1.0.0



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

def preview
  @preview
end

#ratingObject (readonly)

Since:

  • 1.0.0



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

def rating
  @rating
end

#sampleE621::Post::Sample (readonly)

Sample of post

Examples:

Get post’s sample

post.sample #=> E621::Post::Sample

Returns:

  • (E621::Post::Sample)

    sample content of this post

Since:

  • 1.0.0



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

def sample
  @sample
end

#scoreE621::Post::Score (readonly)

Score for post

Examples:

Get post’s score

post.score #=> E621::Post::Score

Returns:

  • (E621::Post::Score)

    score information of this post

Since:

  • 1.0.0



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

def score
  @score
end

#sourcesObject (readonly)

Since:

  • 1.0.0



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

def sources
  @sources
end

#tagsObject (readonly)

Since:

  • 1.0.0



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

def tags
  @tags
end

#updated_atTime (readonly)

Time post was last updated

Examples:

Get last update time

post.updated_at #=> Time

Returns:

  • (Time)

    last update time of post

Since:

  • 1.0.0



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

def updated_at
  @updated_at
end

#uploaderObject (readonly)

Since:

  • 1.0.0



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

def uploader
  @uploader
end

Class Method Details

.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



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(:posts, query)[:posts].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(:posts, id: id)[:post])
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



113
114
115
# File 'lib/rubyhexagon/post.rb', line 113

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

#active_children?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



159
160
161
# File 'lib/rubyhexagon/post.rb', line 159

def active_children?
  @has_active_children
end

#children?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



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

def children?
  @has_children
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



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

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

#explicit?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



151
152
153
# File 'lib/rubyhexagon/post.rb', line 151

def explicit?
  @rating == :explicit
end

#favorited?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



163
164
165
# File 'lib/rubyhexagon/post.rb', line 163

def favorited?
  @is_favorited
end

#questionable?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



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

def questionable?
  @rating == :questionable
end

#safe?Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



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

def safe?
  @rating == :safe
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



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

def show
  E621::Post.new(E621::API.fetch(:posts, id: @id)[:post])
end

#testObject Also known as: pending?, flagged?, note_locked?, status_locked?, rating_locked?, deleted?

Since:

  • 1.0.0



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

def test
  instance_variable_get("@#{__callee__.to_s.sub('?', '')}")
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



124
125
126
127
128
129
130
# File 'lib/rubyhexagon/post.rb', line 124

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end