Class: Rubyhexagon::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



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

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

.list(tags, limit = 320) ⇒ 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)

    start post ID is lesser than this ID

Returns:

  • (Array<Post>)

    an array of posts

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

def self.list(tags, limit = 320)
  parameters = { tags: tags, limit: limit }
  posts = fetch_posts(parameters)
  while block_given? && posts != []
    posts.each { |post| yield post }
    parameters[:before_id] = posts.last.nil? ? 0 : posts.last.id
    posts = fetch_posts(parameters)
  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



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

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