Class: E621::Search::Posts

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/search/posts.rb

Overview

Class to hold methods for Post searches.

Author:

  • Maxine Michalski

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.deleted(page = 1) ⇒ Array<Post>

Retrieve a list of deleted post, with deletion reason. Only id and del_reason is present. This method accepts blocks and only returns one page without blocks

Parameters:

  • page (Integer) (defaults to: 1)

    page of deleted posts to return

Returns:

  • (Array<Post>)

    an array of deleted posts

Author:

  • Maxine Michalski

Since:

  • 1.0.0



37
38
39
40
41
42
43
44
45
# File 'lib/rubyhexagon/search/posts.rb', line 37

def self.deleted(page = 1)
  d = fetch_deleted(page)
  while block_given? && d != []
    d.each { |del| yield del }
    d = fetch_deleted(page)
    page += 1
  end
  d
end

.fetch_posts(parameters) ⇒ Array<Post>

Helper function for posts

Returns:

  • (Array<Post>)

    array of posts

Author:

  • Maxine Michalski

Since:

  • 1.0.0



106
107
108
109
110
# File 'lib/rubyhexagon/search/posts.rb', line 106

def self.fetch_posts(parameters)
  API.new.fetch('post', 'index', parameters).map do |data|
    Post.new(data[:id]).show(data)
  end
end

.list(tags, limit = 320, before_id = 2_000_000) ⇒ Array<Post>

Retrieve a list of post data, which is filtered by arguments. This method requires a block to be passed to it!

This method accepts blocks and only returns one page without blocks

Parameters:

  • tags (String)

    this can be any request that can be done on e621 too

  • limit (Integer) (defaults to: 320)

    number of posts to return. This has a hard limit of 320

  • before_id (Integer) (defaults to: 2_000_000)

    start post ID is lesser than this ID

Returns:

  • (Array<Post>)

    an array of posts

Author:

  • Maxine Michalski

Since:

  • 1.0.0



61
62
63
64
65
66
67
68
69
70
# File 'lib/rubyhexagon/search/posts.rb', line 61

def self.list(tags, limit = 320, before_id = 2_000_000)
  parameters = { tags: tags, limit: limit, before_id: before_id }
  posts = fetch_posts(parameters)
  while block_given? && posts != []
    posts.each { |post| yield post }
    posts = fetch_posts(parameters)
    parameters[:before_id] = posts.last.nil? ? 0 : posts.last.id
  end
  posts
end

Fetch popular posts by day, week or month

Parameters:

  • timespan (Symbol) (defaults to: :day)

    can be :day, :week or :month

Returns:

Raises:

  • ArgumentError if no valid symbol is given

Author:

  • Maxine Michalski

Since:

  • 1.0.0



80
81
82
83
84
85
86
87
# File 'lib/rubyhexagon/search/posts.rb', line 80

def self.popular(timespan = :day)
  unless %i[day week month].include?(timespan)
    raise ArgumentError, "Unknown time span: #{timespan}"
  end
  API.new.fetch('post', "popular_by_#{timespan}", {}).map do |post|
    Post.new(post[:id]).show(post)
  end
end