Class: Brutalismbot::Reddit::Post

Inherits:
Base
  • Object
show all
Defined in:
lib/brutalismbot/reddit/post.rb,
lib/brutalismbot/reddit/stub.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Parser

#parse

Constructor Details

This class inherits a constructor from Brutalismbot::Base

Class Method Details

.stub(created_utc: nil, post_id: nil, permalink_id: nil, image_id: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#created_after?(time = nil) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# 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) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# 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) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/brutalismbot/reddit/post.rb', line 18

def created_between?(start, stop)
  created_after?(start) && created_before?(stop)
end

#created_utcObject



22
23
24
# File 'lib/brutalismbot/reddit/post.rb', line 22

def created_utc
  Time.at(data["created_utc"].to_i).utc
end

#dataObject



26
27
28
# File 'lib/brutalismbot/reddit/post.rb', line 26

def data
  @item.fetch("data", {})
end

#fullnameObject



30
31
32
# File 'lib/brutalismbot/reddit/post.rb', line 30

def fullname
  "#{kind}_#{id}"
end

#idObject



34
35
36
# File 'lib/brutalismbot/reddit/post.rb', line 34

def id
  data["id"]
end

#inspectObject



38
39
40
# File 'lib/brutalismbot/reddit/post.rb', line 38

def inspect
  "#<#{self.class} #{data["permalink"]}>"
end

#is_gallery?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/brutalismbot/reddit/post.rb', line 42

def is_gallery?
  data["is_gallery"] || false
end

#is_self?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/brutalismbot/reddit/post.rb', line 46

def is_self?
  data["is_self"] || false
end

#kindObject



50
51
52
# File 'lib/brutalismbot/reddit/post.rb', line 50

def kind
  @item["kind"]
end

#media_metadataObject



54
55
56
# File 'lib/brutalismbot/reddit/post.rb', line 54

def 
  data["media_metadata"]
end

#media_urls(&block) ⇒ Object

Get media URLs for post



76
77
78
79
80
81
82
83
84
# 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

#pathObject

S3 path



88
89
90
# 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


58
59
60
# File 'lib/brutalismbot/reddit/post.rb', line 58

def permalink
  "https://reddit.com#{data["permalink"]}"
end

#preview_imagesObject



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

def preview_images
  data.dig("preview", "images")
end

#titleObject



66
67
68
# File 'lib/brutalismbot/reddit/post.rb', line 66

def title
  CGI.unescape_html(data["title"])
end

#urlObject



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

def url
  data["url"]
end