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
# 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",
      preview: {
        images: [
          {
            source: {
              url: "https://preview.image.host/#{image_id}_large.jpg",
              width: 1000,
              height: 1000,
            },
          },
          {
            source: {
              url: "https://preview.image.host/#{image_id}_small.jpg",
              width: 500,
              height: 500,
            }
          }
        ],
      },
    },
  )
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_self?Boolean

Returns:

  • (Boolean)


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

def is_self?
  data["is_self"]
end

#kindObject



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

def kind
  @item["kind"]
end

#media_uriObject



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

def media_uri
  URI.parse(media_url)
end

#media_urlObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/brutalismbot/reddit/post.rb', line 54

def media_url
  # Use URL if it's an image
  if mime_type.start_with?("image/")
    data["url"]

  # Extract preview image URL
  else
    images = data.dig("preview", "images") || {}
    source = images.map{|x| x["source"] }.compact.max do |a,b|
      a.slice("width", "height").values <=> b.slice("width", "height").values
    end
    CGI.unescape_html(source["url"])
  end
end

#mime_typeObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brutalismbot/reddit/post.rb', line 69

def mime_type
  @mime_type ||= begin
    uri = URI.parse(data["url"])
    ssl = uri.scheme == "https"
    Brutalismbot.logger.info("HEAD #{uri}")
    Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
      req = Net::HTTP::Head.new(uri)
      http.request(req)["Content-Type"]
    end
  end
end

#mime_type=(value) ⇒ Object



81
82
83
# File 'lib/brutalismbot/reddit/post.rb', line 81

def mime_type=(value)
  @mime_type = value
end

#pathObject



85
86
87
# File 'lib/brutalismbot/reddit/post.rb', line 85

def path
  created_utc.strftime("year=%Y/month=%Y-%m/day=%Y-%m-%d/%s.json")
end


89
90
91
# File 'lib/brutalismbot/reddit/post.rb', line 89

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

#titleObject



93
94
95
# File 'lib/brutalismbot/reddit/post.rb', line 93

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

#to_s3(bucket: nil, prefix: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/brutalismbot/reddit/post.rb', line 97

def to_s3(bucket:nil, prefix:nil)
  bucket ||= ENV["POSTS_S3_BUCKET"] || "brutalismbot"
  prefix ||= ENV["POSTS_S3_PREFIX"] || "data/v1/posts/"
  {
    bucket: bucket,
    key: File.join(*[prefix, path].compact),
    body: to_json,
  }
end

#to_slackObject



107
108
109
# File 'lib/brutalismbot/reddit/post.rb', line 107

def to_slack
  is_self? ? to_slack_text : to_slack_image
end

#to_twitterObject



111
112
113
114
115
116
# File 'lib/brutalismbot/reddit/post.rb', line 111

def to_twitter
  max = 280 - permalink.length - 1
  status = title.length <= max ? title : "#{title[0...max - 1]}"
  status << "\n#{permalink}"
  {status: status, media_url: is_self? ? nil : media_url}
end

#urlObject



118
119
120
# File 'lib/brutalismbot/reddit/post.rb', line 118

def url
  data["url"]
end