class Brutalismbot::Reddit::Post

Public Class Methods

stub(created_utc:nil, post_id:nil, permalink_id:nil, image_id:nil) click to toggle source
# File lib/brutalismbot/reddit/stub.rb, line 7
def stub(created_utc:nil, post_id:nil, permalink_id:nil, image_id:nil)
  created_utc  ||= Time.now.utc - rand(86400) - 86400
  post_id      ||= SecureRandom.alphanumeric(6).downcase
  permalink_id ||= SecureRandom.alphanumeric.downcase
  image_id     ||= SecureRandom.alphanumeric
  new(
    kind: "t3",
    data: {
      id:          post_id,
      created_utc: created_utc.to_i,
      permalink:   "/r/brutalism/comments/#{permalink_id}/test/",
      title:       "Post to /r/brutalism",
      url:         "https://image.host/#{image_id}.jpg",
      media_metadata: {
        abcdef: {
          s: {
            u: "https://preview.image.host/#{image_id}_1.jpg",
          },
          p: [
            {x: 1, y: 1, u: "https://preview.image.host/#{image_id}_1.jpg"},
            {x: 2, y: 2, u: "https://preview.image.host/#{image_id}_2.jpg"},
            {x: 3, y: 3, u: "https://preview.image.host/#{image_id}_3.jpg"},
          ],
        },
        ghijkl: {
          s: {
            u: "https://preview.image.host/#{image_id}_2.jpg",
          },
          p: [
            {x: 1, y: 1, u: "https://preview.image.host/#{image_id}_1.jpg"},
            {x: 2, y: 2, u: "https://preview.image.host/#{image_id}_2.jpg"},
            {x: 3, y: 3, u: "https://preview.image.host/#{image_id}_3.jpg"},
          ],
        },
      },
      preview: {
        images: [
          {
            source: {
              url: "https://preview.image.host/#{image_id}_large.jpg",
              width: 1000,
              height: 1000,
            },
          },
        ],
      },
    },
  )
end

Public Instance Methods

created_after?(time = nil) click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 10
def created_after?(time = nil)
  time.nil? || created_utc.to_i > time.to_i
end
created_before?(time = nil) click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 14
def created_before?(time = nil)
  time.nil? || created_utc.to_i < time.to_i
end
created_between?(start, stop) click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 18
def created_between?(start, stop)
  created_after?(start) && created_before?(stop)
end
created_utc() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 22
def created_utc
  Time.at(data["created_utc"].to_i).utc
end
data() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 26
def data
  @item.fetch("data", {})
end
fullname() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 30
def fullname
  "#{kind}_#{id}"
end
id() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 34
def id
  data["id"]
end
inspect() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 38
def inspect
  "#<#{self.class} #{data["permalink"]}>"
end
is_self?() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 46
def is_self?
  data["is_self"] || false
end
kind() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 50
def kind
  @item["kind"]
end
media_metadata() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 54
def media_metadata
  data["media_metadata"]
end
media_urls(&block) click to toggle source

Get media URLs for post

# File lib/brutalismbot/reddit/post.rb, line 76
def media_urls(&block)
  if is_gallery?
    media_urls_gallery(&block)
  elsif preview_images
    media_urls_preview(&block)
  else
    []
  end
end
path() click to toggle source

S3 path

# File lib/brutalismbot/reddit/post.rb, line 88
def path
  created_utc.strftime("year=%Y/month=%Y-%m/day=%Y-%m-%d/%s.json")
end
preview_images() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 62
def preview_images
  data.dig("preview", "images")
end
title() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 66
def title
  CGI.unescape_html(data["title"])
end
url() click to toggle source
# File lib/brutalismbot/reddit/post.rb, line 70
def url
  data["url"]
end

Private Instance Methods

media_urls_preview() { |image| ... } click to toggle source

Get media URLs from previews

# File lib/brutalismbot/reddit/post.rb, line 105
def media_urls_preview(&block)
  preview_images.map do |image|
    url = block_given? ? yield(image) : image.dig("source", "url")
    CGI.unescape_html(url) unless url.nil?
  end.compact
end