Class: Brutalismbot::Reddit::Post
- Defined in:
- lib/brutalismbot/reddit/post.rb,
lib/brutalismbot/reddit/stub.rb
Class Method Summary collapse
Instance Method Summary collapse
- #created_after?(time = nil) ⇒ Boolean
- #created_before?(time = nil) ⇒ Boolean
- #created_between?(start, stop) ⇒ Boolean
- #created_utc ⇒ Object
- #data ⇒ Object
- #fullname ⇒ Object
- #id ⇒ Object
- #inspect ⇒ Object
- #kind ⇒ Object
- #path ⇒ Object
- #permalink ⇒ Object
- #title ⇒ Object
- #to_s3(bucket:, prefix: nil) ⇒ Object
- #to_slack ⇒ Object
- #to_slack_image ⇒ Object
- #to_slack_text ⇒ Object
- #to_twitter ⇒ Object
- #url ⇒ Object
Methods inherited from Base
Methods included from Parser
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 |
# 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", preview: { images: [ { source: { url: "https://preview.redd.it/#{image_id}.jpg", width: 1000, height: 1000, }, }, { source: { url: "https://preview.redd.it/small.jpg", width: 500, height: 500, } } ], }, }, ) end |
Instance Method Details
#created_after?(time = nil) ⇒ Boolean
9 10 11 |
# File 'lib/brutalismbot/reddit/post.rb', line 9 def created_after?(time = nil) time.nil? || created_utc.to_i > time.to_i end |
#created_before?(time = nil) ⇒ Boolean
13 14 15 |
# File 'lib/brutalismbot/reddit/post.rb', line 13 def created_before?(time = nil) time.nil? || created_utc.to_i < time.to_i end |
#created_between?(start, stop) ⇒ Boolean
17 18 19 |
# File 'lib/brutalismbot/reddit/post.rb', line 17 def created_between?(start, stop) created_after?(start) && created_before?(stop) end |
#created_utc ⇒ Object
21 22 23 |
# File 'lib/brutalismbot/reddit/post.rb', line 21 def created_utc Time.at(data["created_utc"].to_i).utc end |
#data ⇒ Object
25 26 27 |
# File 'lib/brutalismbot/reddit/post.rb', line 25 def data @item.fetch("data", {}) end |
#fullname ⇒ Object
29 30 31 |
# File 'lib/brutalismbot/reddit/post.rb', line 29 def fullname "#{kind}_#{id}" end |
#id ⇒ Object
33 34 35 |
# File 'lib/brutalismbot/reddit/post.rb', line 33 def id data["id"] end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/brutalismbot/reddit/post.rb', line 37 def inspect "#<#{self.class} #{data["permalink"]}>" end |
#kind ⇒ Object
41 42 43 |
# File 'lib/brutalismbot/reddit/post.rb', line 41 def kind @item["kind"] end |
#path ⇒ Object
45 46 47 |
# File 'lib/brutalismbot/reddit/post.rb', line 45 def path created_utc.strftime("year=%Y/month=%Y-%m/day=%Y-%m-%d/%s.json") end |
#permalink ⇒ Object
49 50 51 |
# File 'lib/brutalismbot/reddit/post.rb', line 49 def permalink "https://reddit.com#{data["permalink"]}" end |
#title ⇒ Object
53 54 55 |
# File 'lib/brutalismbot/reddit/post.rb', line 53 def title CGI.unescapeHTML(data["title"]) end |
#to_s3(bucket:, prefix: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/brutalismbot/reddit/post.rb', line 57 def to_s3(bucket:, prefix:nil) { bucket: bucket, key: File.join(*[prefix, path].compact), body: to_json, metadata: { id: id, }, } end |
#to_slack ⇒ Object
68 69 70 |
# File 'lib/brutalismbot/reddit/post.rb', line 68 def to_slack url.nil? ? to_slack_text : to_slack_image end |
#to_slack_image ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/brutalismbot/reddit/post.rb', line 72 def to_slack_image { blocks: [ { type: "image", title: { type: "plain_text", text: "/r/brutalism", emoji: true, }, image_url: url, alt_text: title, }, { type: "context", elements: [ { type: "mrkdwn", text: "<#{permalink}|#{title}>", }, ], }, ], } end |
#to_slack_text ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/brutalismbot/reddit/post.rb', line 98 def to_slack_text { blocks: [ { type: "section", text: { type: "mrkdwn", text: "<#{permalink}|#{title}>", }, accessory: { type: "image", image_url: "https://brutalismbot.com/logo-red-ppl.png", alt_text: "/r/brutalism", }, }, ], } end |
#to_twitter ⇒ Object
117 118 119 |
# File 'lib/brutalismbot/reddit/post.rb', line 117 def to_twitter [title, permalink].join("\n") end |
#url ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/brutalismbot/reddit/post.rb', line 121 def url 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.unescapeHTML(source.dig("url")) rescue NoMethodError data["media_metadata"]&.values&.first&.dig("s", "u") end |