brutalismbot 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1836d2eecd6d94c8d170223f7c8da3bbfe87909154d82492716d3ac1b3b411e
4
- data.tar.gz: 825519ee45d36652e631310c38c0ce246c9e08e0f86987c4bfa42cc6345daa8a
3
+ metadata.gz: e14bb3fedb5cfd683191d7ec1aa24d05531af880e55889752b0b344ef56fe785
4
+ data.tar.gz: bd3d5795d5026c738cc7977a15ff7b725fe7a7451174effeecba4c9de7019bbb
5
5
  SHA512:
6
- metadata.gz: baed1a146152eb80401cbd75fd2e5265cd99fae63fb588db1ca66e233e52de8c06c6460abd099b6f046b4436923496d94bb7867b645382c81ec8857f9fb1a9f7
7
- data.tar.gz: dde3ca496744cdf5e45c41be689c36587570c3cb4b79d7998a82d3ad002bc79e5e601aa09f21e246bc40bef00d46b2ec4b8affdc8595135f2ccac4cd5bd11617
6
+ metadata.gz: 67e7c40d478a7e5f3ce89d7bd6bb7f96d10318605db269ad5960d1bfc56eac911d3783d0a292e1dfb15b00fe99544d9ef3f563586b49202180d5b727fcf58515
7
+ data.tar.gz: 2b6216569f77784d4937f87f1c8c3b1e8ef47d4030533ff3653896df7e5b95bbf96dec12d2691b0a767a7669c914b6eca48cccd20da331d7ac6e56c60ad85921
data/README.md CHANGED
@@ -22,17 +22,20 @@ bucket = Aws::S3::Bucket.new name: "my-bucket"
22
22
  brutbot = Brutalismbot::S3::Client.new bucket: bucket,
23
23
  prefix: "my/prefix/"
24
24
 
25
- # Get new posts after a given time
26
- brutbot.subreddit.new_posts.after Time.parse("2019-06-01 12:00:00Z")
27
-
28
- # Get current top post
29
- brutbot.subreddit.top_post
30
-
31
25
  # Get latest cached post
32
26
  brutbot.posts.latest
33
27
 
34
- # Get max key in posts
28
+ # Get latest post as S3 Object
35
29
  brutbot.posts.max_key
30
+
31
+ # Get post-time of latest cached post
32
+ limit = brutbot.posts.max_time
33
+
34
+ # Get newest post after a given time
35
+ brutbot.subreddit.posts(:new).since(time: limit).first
36
+
37
+ # Get current top post
38
+ brutbot.subreddit.posts(:top).first
36
39
  ```
37
40
 
38
41
  ## Contributing
@@ -33,7 +33,7 @@ module Brutalismbot
33
33
  response = JSON.parse http.request(request).body
34
34
  children = response.dig("data", "children") || []
35
35
  children.reverse.each do |child|
36
- post = Brutalismbot::Post[child]
36
+ post = Brutalismbot::Post.new child
37
37
  yield post if post.created_after time: @min_time
38
38
  end
39
39
  end
@@ -44,7 +44,7 @@ module Brutalismbot
44
44
  class AuthCollection < Collection
45
45
  def each
46
46
  super do |object|
47
- yield Brutalismbot::Auth[JSON.parse object.get.body.read]
47
+ yield Brutalismbot::Auth.new JSON.parse(object.get.body.read)
48
48
  end
49
49
  end
50
50
 
@@ -66,7 +66,7 @@ module Brutalismbot
66
66
  end
67
67
 
68
68
  def put(auth:, dryrun:nil)
69
- key = "#{@prefix}team=#{auth.team_id}/channel=#{auth.channel_id}/oauth.json"
69
+ key = "#{@prefix}team=#{auth.team_id}/channel=#{auth.incoming_webhook.channel_id}/oauth.json"
70
70
  super key: key, body: auth.to_json, dryrun: dryrun
71
71
  end
72
72
  end
@@ -74,12 +74,12 @@ module Brutalismbot
74
74
  class PostCollection < Collection
75
75
  def each
76
76
  super do |object|
77
- yield Brutalismbot::Post[JSON.parse object.get.body.read]
77
+ yield Brutalismbot::Post.new JSON.parse(object.get.body.read)
78
78
  end
79
79
  end
80
80
 
81
81
  def latest
82
- Brutalismbot::Post[JSON.parse max_key.get.body.read]
82
+ Brutalismbot::Post.new JSON.parse(max_key.get.body.read)
83
83
  end
84
84
 
85
85
  def max_key
@@ -1,3 +1,3 @@
1
1
  module Brutalismbot
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/brutalismbot.rb CHANGED
@@ -29,50 +29,51 @@ module Brutalismbot
29
29
  class Error < StandardError
30
30
  end
31
31
 
32
- class Auth < Hash
33
- def channel_id
34
- dig "incoming_webhook", "channel_id"
32
+ class Auth < OpenStruct
33
+ class IncomingWebhook < OpenStruct; end
34
+
35
+ def incoming_webhook
36
+ IncomingWebhook.new dig("incoming_webhook")
35
37
  end
36
38
 
37
39
  def post(body:, dryrun:nil)
38
- uri = URI.parse webhook_url
40
+ uri = URI.parse incoming_webhook.url
39
41
  ssl = uri.scheme == "https"
40
42
  Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
41
43
  if dryrun
42
- Brutalismbot.logger&.info "POST DRYRUN #{uri}"
44
+ Brutalismbot.logger.info "POST DRYRUN #{uri}"
43
45
  else
44
- Brutalismbot.logger&.info "POST #{uri}"
46
+ Brutalismbot.logger.info "POST #{uri}"
45
47
  req = Net::HTTP::Post.new uri, "content-type" => "application/json"
46
48
  req.body = body
47
49
  http.request req
48
50
  end
49
51
  end
50
52
  end
53
+ end
51
54
 
52
- def team_id
53
- dig "team_id"
55
+ class Post < OpenStruct
56
+ class Data < OpenStruct
54
57
  end
55
58
 
56
- def webhook_url
57
- dig "incoming_webhook", "url"
59
+ def created_utc
60
+ Time.at(data.created_utc.to_i).utc
58
61
  end
59
- end
60
62
 
61
- class Post < Hash
62
63
  def created_after(time:)
63
- created_utc.to_i > time.to_i
64
+ data.created_utc.to_i > time.to_i
64
65
  end
65
66
 
66
- def created_utc
67
- Time.at(dig("data", "created_utc").to_i).utc
67
+ def data
68
+ Data.new dig("data")
68
69
  end
69
70
 
70
71
  def permalink
71
- dig "data", "permalink"
72
+ data.permalink
72
73
  end
73
74
 
74
75
  def title
75
- dig "data", "title"
76
+ data.title
76
77
  end
77
78
 
78
79
  def to_slack
@@ -102,13 +103,13 @@ module Brutalismbot
102
103
  end
103
104
 
104
105
  def url
105
- images = dig "data", "preview", "images"
106
+ images = data.preview.dig "images"
106
107
  source = images.map{|x| x["source"] }.compact.max do |a,b|
107
108
  a.slice("width", "height").values <=> b.slice("width", "height").values
108
109
  end
109
110
  CGI.unescapeHTML source.dig("url")
110
111
  rescue NoMethodError
111
- dig("data", "media_metadata")&.values&.first&.dig("s", "u")
112
+ data.media_metadata&.values&.first&.dig("s", "u")
112
113
  end
113
114
  end
114
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brutalismbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Mancevice
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -118,7 +118,6 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - LICENSE.txt
120
120
  - README.md
121
- - Rakefile
122
121
  - lib/brutalismbot.rb
123
122
  - lib/brutalismbot/r.rb
124
123
  - lib/brutalismbot/s3.rb
@@ -142,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
141
  - !ruby/object:Gem::Version
143
142
  version: '0'
144
143
  requirements: []
145
- rubygems_version: 3.0.3
144
+ rubygems_version: 3.0.4
146
145
  signing_key:
147
146
  specification_version: 4
148
147
  summary: Mirror posts from /r/brutalism to Slack
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec