elephrame 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 925f58429ac0d48573209e51c2ad09fbc030d9b3ab3eb138f49219ed672aaac6
4
- data.tar.gz: efff93a265abf97c14c8e37a02676bacd2b02967a61d0d422233ddfe097a85f3
3
+ metadata.gz: 443a61168b9ae56b414840dc0390ede87a9f8c5ea8b6a1c2cdac99ee6d69801e
4
+ data.tar.gz: 174eca80ecec2f12d20de89963bc1ae7db831fa58912d202e730d5c7717011e9
5
5
  SHA512:
6
- metadata.gz: 1e5dfe1f3bfc1ded1c5f79dfe8d9f9b498fe916e0276e6b8aaeebd954526447155b7f8430b9ae1ef50666e869d1ca0dbcb28e3d2c454ac43c2a1b26d71521b24
7
- data.tar.gz: 3dc7ba9f4161d0dbccb83925c13094e24756b49db9c466511cca1ab27de73fbe3864fe295b91ccf6df9a18e60d79dc744165d4fd378cd37cc17f1aa4ba4bf39d
6
+ metadata.gz: 6e5d3401b5b81ea38c65f568bea40e95717db38d3fc228cebbd6aec97281341300945ede38bcacacd26b5dcbd049478f4c2515a8da7d51eefbff04da658de84c
7
+ data.tar.gz: 81c1877dd775d094ff19497d17f770f2eac003f39980557062d9a664c2ba62a5f600625108ba214bc4187207fdbaf0a0d3cbe39575aea968921ef10f6127c4b1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elephrame (0.4.1)
4
+ elephrame (0.4.2)
5
5
  moostodon (~> 0.2.0)
6
6
  rufus-scheduler
7
7
 
@@ -33,7 +33,7 @@ GEM
33
33
  buftok (~> 0)
34
34
  http (~> 3.0)
35
35
  oj (~> 3.3)
36
- oj (3.7.4)
36
+ oj (3.6.7)
37
37
  public_suffix (3.0.3)
38
38
  raabro (1.1.6)
39
39
  rake (10.5.0)
data/README.md CHANGED
@@ -69,7 +69,8 @@ Exposed methods from bot object:
69
69
  - `max_retries` (defaults to 5) the maximum amount of times the framework will retry a mastodon request
70
70
  - `failed` a hash that represents the status of the last post or media upload. `failed[:post]` and `failed[:media]`; returns true if it failed, false if it succeeded
71
71
  - `post(content, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: [])` this provides an easy way to post statuses from inside code blocks
72
- - `reply(content, *options)` a shorthand method to reply to the last mention (Note: doesn't automatically @ the other user/s)
72
+ - `reply(content, *options)` a shorthand method to reply to the last mention (Note: only include the @ for the user who @ed the bot)
73
+ - `reply_with_mentions(content, *options)` similar to `reply` but includes all @s (respects #NoBot)
73
74
  - `find_ancestor(id, depth = 10, stop_at = 1)` looks backwards through reply chains for the most recent post the bot made starting at post `id` until it hits `depth` number of posts, or finds `stop_at` number of it's own posts
74
75
  - `no_bot?(account_id)` returns true if user with `account_id` has some form of "#NoBot" in their bio
75
76
 
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.metadata["homepage_uri"] = spec.homepage
22
22
  spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = "https://github.com/theZacAttacks/elephrame/releases"
23
24
  else
24
25
  raise "RubyGems 2.0 or newer is required to protect against " \
25
26
  "public gem pushes."
@@ -34,13 +35,11 @@ Gem::Specification.new do |spec|
34
35
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
36
  spec.require_paths = ["lib"]
36
37
 
38
+ spec.required_ruby_version = '~> 2.0'
37
39
  spec.add_development_dependency "bundler", "~> 1.17"
38
40
  spec.add_development_dependency "rake", "~> 10.0"
39
41
  spec.add_development_dependency "minitest", "~> 5.0"
40
42
 
41
43
  spec.add_dependency 'rufus-scheduler'
42
44
  spec.add_dependency 'moostodon', '~> 0.2.0'
43
-
44
- # until mastodon-api gets a bump this stays commented out
45
- #spec.add_dependency 'mastodon-api', '> 1.1.0'
46
45
  end
@@ -1,4 +1,3 @@
1
- require 'time'
2
1
  require 'net/http'
3
2
 
4
3
  module Elephrame
@@ -6,7 +5,7 @@ module Elephrame
6
5
 
7
6
  ##
8
7
  # a superclass for other bots
9
- # holds common functions and variables
8
+ # holds common methods and variables
10
9
 
11
10
  class BaseBot
12
11
  attr_reader :client, :username, :failed
@@ -115,7 +114,8 @@ module Elephrame
115
114
  block.call
116
115
  return false
117
116
  rescue HTTP::TimeoutError
118
- puts "caught HTTP Timeout error at #{Time.now} retrying #{@max_retries-i} more times"
117
+ puts "caught HTTP Timeout error; retrying #{@max_retries-i} more times"
118
+ sleep 5
119
119
  end
120
120
  end
121
121
  return true
@@ -61,13 +61,9 @@ module Elephrame
61
61
 
62
62
  def initialize prefix = '!', usage = nil
63
63
  super()
64
-
65
- @commands = []
66
- @cmd_hash = {}
67
64
 
68
65
  setup_streaming
69
- set_prefix prefix
70
- set_help usage unless usage.nil?
66
+ setup_command prefix, usage
71
67
  end
72
68
  end
73
69
 
@@ -0,0 +1,108 @@
1
+ module Elephrame
2
+ module Command
3
+ include Elephrame::Reply
4
+
5
+ attr_reader :commands, :prefix
6
+ attr :cmd_hash, :cmd_regex, :not_found
7
+
8
+ ##
9
+ # Initializes the +commands+ array, +cmd_hash+
10
+ #
11
+ # @param prefix [String] sets the command prefix, defaults to '!'
12
+ # @param usage [String] the response to the help command
13
+ #
14
+
15
+ def setup_command(prefix = '!', usage = nil)
16
+ @commands = []
17
+ @cmd_hash = {}
18
+
19
+ set_prefix prefix
20
+ set_help usage unless usage.nil?
21
+ end
22
+
23
+ ##
24
+ # sets the prefix for commands
25
+ #
26
+ # @param pf [String] the prefix
27
+
28
+ def set_prefix pf
29
+ @prefix = pf
30
+ end
31
+
32
+ ##
33
+ # Shortcut method to provide usage docs in response to help command
34
+ #
35
+ # @param usage [String]
36
+
37
+ def set_help usage
38
+ add_command 'help' do |bot, content, status|
39
+ bot.reply("#{usage}")
40
+ end
41
+ end
42
+
43
+ ##
44
+ # Adds the command and block into the bot to process later
45
+ # also sets up the command regex
46
+ #
47
+ # @param cmd [String] a command to add
48
+ # @param block [Proc] the code to execute when +cmd+ is recieved
49
+
50
+ def add_command cmd, &block
51
+ @commands.append cmd unless @commands.include? cmd
52
+ @cmd_hash[cmd.to_sym] = block
53
+
54
+ # build up our regex (this regex should be fine, i guess :shrug:)
55
+ @cmd_regex = /\A#{@prefix}(?<cmd>#{@commands.join('|')})\b(?<data>.*)/m
56
+ end
57
+
58
+ ##
59
+ # What to do if we don't match anything
60
+ #
61
+ # @param block [Proc] a block to run when we don't match a command
62
+
63
+ def if_not_found &block
64
+ @not_found = block
65
+ end
66
+
67
+ ##
68
+ # Starts loop to process any mentions, running command procs set up earlier
69
+ #
70
+ # If a block is passed to this function it gets ran when no commands
71
+ # get matched. Otherwise the framework checks if +not_found+ exists
72
+ # and runs it
73
+
74
+ def run_commands
75
+ @streamer.user do |update|
76
+ next unless update.kind_of? Mastodon::Notification and update.type == 'mention'
77
+
78
+ # set up the status to strip html, if needed
79
+ update.status.class
80
+ .module_eval { alias_method :content, :strip } if @strip_html
81
+ store_mention_data update.status
82
+
83
+ # strip our username out of the status
84
+ post = update.status.content.gsub(/@#{@username} /, '')
85
+
86
+ # see if the post matches our regex, running the stored proc if it does
87
+ matches = @cmd_regex.match(post)
88
+
89
+ unless matches.nil?
90
+ @cmd_hash[matches[:cmd].to_sym]
91
+ .call(self,
92
+ matches[:data].strip,
93
+ update.status)
94
+ else
95
+
96
+ if block_given?
97
+ yield(self, update.status)
98
+ else
99
+ @not_found.call(self, update.status) unless @not_found.nil?
100
+ end
101
+
102
+ end
103
+ end
104
+ end
105
+
106
+ alias_method :run, :run_commands
107
+ end
108
+ end
@@ -0,0 +1,61 @@
1
+ module Elephrame
2
+ module AllInteractions
3
+ include Elephrame::Reply
4
+ attr :on_fave, :on_boost, :on_follow
5
+
6
+ ##
7
+ # Sets on_fave equal to +block+
8
+
9
+ def on_fave &block
10
+ @on_fave = block
11
+ end
12
+
13
+ ##
14
+ # Sets on_boost to +block+
15
+
16
+ def on_boost &block
17
+ @on_boost = block
18
+ end
19
+
20
+ ##
21
+ # Sets on_follow to +block+
22
+
23
+ def on_follow &block
24
+ @on_follow = block
25
+ end
26
+
27
+ ##
28
+ # Starts a loop that checks for any notifications for the authenticated
29
+ # user, running the appropriate stored proc when needed
30
+
31
+ def run_interact
32
+ @streamer.user do |update|
33
+ if update.kind_of? Mastodon::Notification
34
+
35
+ case update.type
36
+
37
+ when 'mention'
38
+
39
+ # this makes it so .content calls strip instead
40
+ update.status.class.module_eval { alias_method :content, :strip } if @strip_html
41
+ store_mention_data update.status
42
+ @on_reply.call(self, update.status) unless @on_reply.nil?
43
+
44
+ when 'reblog'
45
+ @on_boost.call(self, update) unless @on_boost.nil?
46
+
47
+ when 'favourite'
48
+ @on_fave.call(self, update) unless @on_fave.nil?
49
+
50
+ when 'follow'
51
+ @on_follow.call(self, update) unless @on_follow.nil?
52
+
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+
59
+ alias_method :run, :run_interact
60
+ end
61
+ end
@@ -0,0 +1,93 @@
1
+ module Elephrame
2
+ module Reply
3
+ attr :on_reply, :mention_data
4
+
5
+ ##
6
+ # Sets on_reply equal to +block+
7
+
8
+ def on_reply &block
9
+ @on_reply = block
10
+ end
11
+
12
+ ##
13
+ # Replies to the last mention the bot recieved using the mention's
14
+ # visibility and spoiler with +text+
15
+ #
16
+ # Automatically includes an @ for the account that mentioned the bot.
17
+ # Does not include any other @. See +reply_with_mentions+ if you want
18
+ # to automatically include all mentions
19
+ #
20
+ # @param text [String] text to post as a reply
21
+ # @param options [Hash] a hash of arguments to pass to post, overrides
22
+ # duplicating settings from last mention
23
+
24
+ def reply(text, *options)
25
+ options = Hash[*options]
26
+
27
+ post("@#{@mention_data[:account].acct} #{text}",
28
+ **@mention_data.merge(options).reject { |k|
29
+ k == :mentions or k == :account
30
+ })
31
+ end
32
+
33
+ ##
34
+ # Replies to the last post and tags everyone who was mentioned
35
+ # (this function respects #NoBot)
36
+ #
37
+ # @param text [String] text to post as a reply
38
+ # @param options [Hash] arguments to pass to post, overrides settings from
39
+ # last mention
40
+
41
+ def reply_with_mentions(text, *options)
42
+ # build up a string of all accounts mentioned in the post
43
+ # unless that account is our own, or the tagged account
44
+ # has #NoBot
45
+ mentions = @mention_data[:mentions].collect do |m|
46
+ "@#{m.acct}" unless m.acct == @username or no_bot? m.id
47
+ end.join ' '
48
+
49
+ reply("#{mentions.strip} #{text}", *options)
50
+ end
51
+
52
+ ##
53
+ # Starts a loop that checks for mentions from the authenticated user account
54
+ # running a supplied block or, if a block is not provided, on_reply
55
+
56
+ def run_reply
57
+ @streamer.user do |update|
58
+ next unless update.kind_of? Mastodon::Notification and update.type == 'mention'
59
+
60
+ # this makes it so .content calls strip instead
61
+ update.status.class.module_eval { alias_method :content, :strip } if @strip_html
62
+
63
+ store_mention_data update.status
64
+
65
+ if block_given?
66
+ yield(self, update.status)
67
+ else
68
+ @on_reply.call(self, update.status)
69
+ end
70
+ end
71
+ end
72
+
73
+ alias_method :run, :run_reply
74
+
75
+ private
76
+
77
+ ##
78
+ # Stores select data about a post into a hash for later use
79
+ #
80
+ # @param mention [Mastodon::Status] the most recent mention the bot received
81
+
82
+ def store_mention_data(mention)
83
+ @mention_data = {
84
+ reply_id: mention.id,
85
+ visibility: mention.visibility,
86
+ spoiler: mention.spoiler_text,
87
+ hide_media: mention.sensitive?,
88
+ mentions: mention.mentions,
89
+ account: mention.account
90
+ }
91
+ end
92
+ end
93
+ end
@@ -1,3 +1,7 @@
1
+ require_relative 'reply'
2
+ require_relative 'command'
3
+ require_relative 'interaction'
4
+
1
5
  module Elephrame
2
6
  module Streaming
3
7
  attr :streamer
@@ -11,241 +15,4 @@ module Elephrame
11
15
  end
12
16
 
13
17
  end
14
-
15
-
16
- module Reply
17
- attr :on_reply, :mention_data
18
-
19
- ##
20
- # Sets on_reply equal to +block+
21
-
22
- def on_reply &block
23
- @on_reply = block
24
- end
25
-
26
- ##
27
- # Replies to the last mention the bot recieved using the mention's
28
- # visibility and spoiler with +text+
29
- #
30
- # Automatically includes an @ for the account that mentioned the bot.
31
- # Does not include any other @. See +reply_with_mentions+ if you want
32
- # to automatically include all mentions
33
- #
34
- # @param text [String] text to post as a reply
35
- # @param options [Hash] a hash of arguments to pass to post, overrides
36
- # duplicating settings from last mention
37
-
38
- def reply(text, *options)
39
- options = Hash[*options]
40
-
41
- post("@#{@mention_data[:account].acct} #{text}",
42
- **@mention_data.merge(options).reject { |k|
43
- k == :mentions or k == :account
44
- })
45
- end
46
-
47
- ##
48
- # Replies to the last post and tags everyone who was mentioned
49
- # (this function respects #NoBot)
50
- #
51
- # @param text [String] text to post as a reply
52
- # @param options [Hash] arguments to pass to post, overrides settings from
53
- # last mention
54
-
55
- def reply_with_mentions(text, *options)
56
- # build up a string of all accounts mentioned in the post
57
- # unless that account is our own, or the tagged account
58
- # has #NoBot
59
- mentions = @mention_data[:mentions].collect do |m|
60
- "@#{m.acct}" unless m.acct == @username or no_bot? m.id
61
- end.join ' '
62
-
63
- reply("#{mentions.strip} #{text}", *options)
64
- end
65
-
66
- ##
67
- # Starts a loop that checks for mentions from the authenticated user account
68
- # running a supplied block or, if a block is not provided, on_reply
69
-
70
- def run_reply
71
- @streamer.user do |update|
72
- next unless update.kind_of? Mastodon::Notification and update.type == 'mention'
73
-
74
- # this makes it so .content calls strip instead
75
- update.status.class.module_eval { alias_method :content, :strip } if @strip_html
76
-
77
- store_mention_data update.status
78
-
79
- if block_given?
80
- yield(self, update.status)
81
- else
82
- @on_reply.call(self, update.status)
83
- end
84
- end
85
- end
86
-
87
- alias_method :run, :run_reply
88
-
89
- private
90
-
91
- ##
92
- # Stores select data about a post into a hash for later use
93
- #
94
- # @param mention [Mastodon::Status] the most recent mention the bot received
95
-
96
- def store_mention_data(mention)
97
- @mention_data = {
98
- reply_id: mention.id,
99
- visibility: mention.visibility,
100
- spoiler: mention.spoiler_text,
101
- hide_media: mention.sensitive?,
102
- mentions: mention.mentions,
103
- account: mention.account
104
- }
105
- end
106
- end
107
-
108
-
109
- module AllInteractions
110
- include Elephrame::Reply
111
- attr :on_fave, :on_boost, :on_follow
112
-
113
- ##
114
- # Sets on_fave equal to +block+
115
-
116
- def on_fave &block
117
- @on_fave = block
118
- end
119
-
120
- ##
121
- # Sets on_boost to +block+
122
-
123
- def on_boost &block
124
- @on_boost = block
125
- end
126
-
127
- ##
128
- # Sets on_follow to +block+
129
-
130
- def on_follow &block
131
- @on_follow = block
132
- end
133
-
134
- ##
135
- # Starts a loop that checks for any notifications for the authenticated
136
- # user, running the appropriate stored proc when needed
137
-
138
- def run_interact
139
- @streamer.user do |update|
140
- if update.kind_of? Mastodon::Notification
141
-
142
- case update.type
143
-
144
- when 'mention'
145
-
146
- # this makes it so .content calls strip instead
147
- update.status.class.module_eval { alias_method :content, :strip } if @strip_html
148
- store_mention_data update.status
149
- @on_reply.call(self, update.status) unless @on_reply.nil?
150
-
151
- when 'reblog'
152
- @on_boost.call(self, update) unless @on_boost.nil?
153
-
154
- when 'favourite'
155
- @on_fave.call(self, update) unless @on_fave.nil?
156
-
157
- when 'follow'
158
- @on_follow.call(self, update) unless @on_follow.nil?
159
-
160
- end
161
- end
162
- end
163
- end
164
-
165
-
166
- alias_method :run, :run_interact
167
- end
168
-
169
-
170
- module Command
171
- include Elephrame::Reply
172
-
173
- attr_reader :commands, :prefix
174
- attr :cmd_hash, :cmd_regex, :not_found
175
-
176
- ##
177
- # sets the prefix for commands
178
- #
179
- # @param pf [String] the prefix
180
-
181
- def set_prefix pf
182
- @prefix = pf
183
- end
184
-
185
- ##
186
- # Shortcut method to provide usage docs in response to help command
187
- #
188
- # @param usage [String]
189
-
190
- def set_help usage
191
- add_command 'help' do |bot, content, status|
192
- bot.reply("#{usage}")
193
- end
194
- end
195
-
196
- ##
197
- # Adds the command and block into the bot to process later
198
- # also sets up the command regex
199
- #
200
- # @param cmd [String] a command to add
201
- # @param block [Proc] the code to execute when +cmd+ is recieved
202
-
203
- def add_command cmd, &block
204
- @commands.append cmd unless @commands.include? cmd
205
- @cmd_hash[cmd.to_sym] = block
206
-
207
- # build up our regex (this regex should be fine, i guess :shrug:)
208
- @cmd_regex = /\A#{@prefix}(?<cmd>#{@commands.join('|')})\b(?<data>.*)/m
209
- end
210
-
211
- ##
212
- # What to do if we don't match anything
213
- #
214
- # @param block [Proc] a block to run when we don't match a command
215
-
216
- def if_not_found &block
217
- @not_found = block
218
- end
219
-
220
- ##
221
- # Starts loop to process any mentions, running command procs set up earlier
222
-
223
- def run_commands
224
- @streamer.user do |update|
225
- next unless update.kind_of? Mastodon::Notification and update.type == 'mention'
226
-
227
- # set up the status to strip html, if needed
228
- update.status.class
229
- .module_eval { alias_method :content, :strip } if @strip_html
230
- store_mention_data update.status
231
-
232
- # strip our username out of the status
233
- post = update.status.content.gsub(/@#{@username} /, '')
234
-
235
- # see if the post matches our regex, running the stored proc if it does
236
- matches = @cmd_regex.match(post)
237
-
238
- unless matches.nil?
239
- @cmd_hash[matches[:cmd].to_sym]
240
- .call(self,
241
- matches[:data].strip,
242
- update.status)
243
- else
244
- @not_found.call(self, update.status)
245
- end
246
- end
247
- end
248
-
249
- alias_method :run, :run_commands
250
- end
251
18
  end
@@ -1,3 +1,3 @@
1
1
  module Elephrame
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elephrame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-13 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,6 +110,9 @@ files:
110
110
  - lib/elephrame/rest/bots.rb
111
111
  - lib/elephrame/rest/rest.rb
112
112
  - lib/elephrame/streaming/bots.rb
113
+ - lib/elephrame/streaming/command.rb
114
+ - lib/elephrame/streaming/interaction.rb
115
+ - lib/elephrame/streaming/reply.rb
113
116
  - lib/elephrame/streaming/streaming.rb
114
117
  - lib/elephrame/util/status.rb
115
118
  - lib/elephrame/version.rb
@@ -120,15 +123,16 @@ metadata:
120
123
  allowed_push_host: https://rubygems.org
121
124
  homepage_uri: https://github.com/theZacAttacks/elephrame
122
125
  source_code_uri: https://github.com/theZacAttacks/elephrame
126
+ changelog_uri: https://github.com/theZacAttacks/elephrame/releases
123
127
  post_install_message:
124
128
  rdoc_options: []
125
129
  require_paths:
126
130
  - lib
127
131
  required_ruby_version: !ruby/object:Gem::Requirement
128
132
  requirements:
129
- - - ">="
133
+ - - "~>"
130
134
  - !ruby/object:Gem::Version
131
- version: '0'
135
+ version: '2.0'
132
136
  required_rubygems_version: !ruby/object:Gem::Requirement
133
137
  requirements:
134
138
  - - ">="