elephrame 0.3.4 → 0.3.6

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: 3aa8e7f20012b5d9316bc25a617408517f13047b9e3b0bda0e3315bb87f9a548
4
- data.tar.gz: c67ff2d3adc7b4f52da5443c02d0029686165383ffd44b62c2f6a1acefe55b71
3
+ metadata.gz: b5debc85f1a59e0bbb4d562dd2685cbe15bd086c005e16cc0f56054109d62c2b
4
+ data.tar.gz: 3d090af83e6d1200b936aeedc86003fa80fbb63edb2b46aec19f5fee4855b111
5
5
  SHA512:
6
- metadata.gz: 6267827dbfcda7ea3abf38f7f8e94faca3fff3007fec3de355a92d8e8b0621793b2f7f55d8ca9425f83d35bb3126f03757a8129401b8a19c46de693789b62ab8
7
- data.tar.gz: acd997ed3205534a494080426157342374552d099930b9eaf5863570490bb9ec1ef005ab6906b1af87c333ae944d396458438cac106de6038c78d611585a4b25
6
+ metadata.gz: aec1f7cc1644c9ee00dc6ef77ef3db692f72c46ffa378be2314caae97a7e93e06e285e62681dd3d9ccbe0020ed6e9b4de17f953d3e2b917c931e07f19d98b5e0
7
+ data.tar.gz: b0c09dd1c87aad6261d59c04aa77b75430a5b0c0ceb29b29840c02827e83bf5dd245c87dcb4c5ca4de2c819d404fc49e058e7d93709a9ac0c4a4c4e024587db9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elephrame (0.3.4)
4
+ elephrame (0.3.6)
5
5
  moostodon (~> 0.2.0)
6
6
  rufus-scheduler
7
7
 
data/README.md CHANGED
@@ -65,8 +65,10 @@ Exposed methods from bot object:
65
65
  - `client` this is the underlying mastodon rest client we use in the backend. use this to make custom calls to the api for which we don't provide a helper
66
66
  - `username` the name of the bot as fetched by verify_credentials
67
67
  - `strip_html` (defaults to true) if set, the framework will automatically strip all html symbols from the post content
68
+ - `max_retries` (defaults to 5) the maximum amount of times the framework will retry a mastodon request
69
+ - `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
68
70
  - `post(content, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: [])` this provides an easy way to post statuses from inside code blocks
69
- - `reply(content)` a shorthand method to reply to the last mention (Note: doesn't automatically @ the other user/s)
71
+ - `reply(content, *options)` a shorthand method to reply to the last mention (Note: doesn't automatically @ the other user/s)
70
72
  - `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
71
73
  - `no_bot?(account_id)` returns true if user with `account_id` has some form of "#NoBot" in their bio
72
74
 
data/lib/elephrame/bot.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'time'
2
+ require 'net/http'
3
+
1
4
  module Elephrame
2
5
  module Bots
3
6
 
@@ -6,12 +9,12 @@ module Elephrame
6
9
  # holds common functions and variables
7
10
 
8
11
  class BaseBot
9
- attr_reader :client, :username
10
- attr_accessor :strip_html
12
+ attr_reader :client, :username, :failed
13
+ attr_accessor :strip_html, :max_retries
11
14
 
12
15
  ##
13
- # Sets up our REST client, gets and saves our username, sets default
14
- # value for strip_html (true)
16
+ # Sets up our REST +client+, gets and saves our +username+, sets default
17
+ # value for +strip_html+ (true), and +max_retries+ (5), +failed+
15
18
  #
16
19
  # @return [Elephrame::Bots::BaseBot]
17
20
 
@@ -20,6 +23,8 @@ module Elephrame
20
23
  bearer_token: ENV['TOKEN'])
21
24
  @username = @client.verify_credentials().acct
22
25
  @strip_html = true
26
+ @max_retries = 5
27
+ @failed = { media: false, post: false }
23
28
  end
24
29
 
25
30
  ##
@@ -35,9 +40,12 @@ module Elephrame
35
40
  def post(text, visibility: 'unlisted', spoiler: '',
36
41
  reply_id: '', hide_media: false, media: [])
37
42
 
43
+ uploaded_ids = []
38
44
  unless media.size.zero?
39
- media.collect! {|m|
40
- @client.upload_media(m).id
45
+ @failed[:media] = retry_if_needed {
46
+ uploaded_ids = media.collect {|m|
47
+ @client.upload_media(m).id
48
+ }
41
49
  }
42
50
  end
43
51
 
@@ -45,11 +53,13 @@ module Elephrame
45
53
  visibility: visibility,
46
54
  spoiler_text: spoiler,
47
55
  in_reply_to_id: reply_id,
48
- media_ids: media,
56
+ media_ids: @failed[:media] ? [] : uploaded_ids,
49
57
  sensitive: hide_media,
50
58
  }
51
-
52
- @client.create_status text, options
59
+
60
+ @failed[:post] = retry_if_needed {
61
+ @client.create_status text, options
62
+ }
53
63
  end
54
64
 
55
65
 
@@ -86,6 +96,28 @@ module Elephrame
86
96
  def no_bot? account_id
87
97
  @client.account(account_id).note =~ /#?NoBot/i
88
98
  end
99
+
100
+ private
101
+
102
+ ##
103
+ # An internal function that ensures our HTTP requests go through
104
+ #
105
+ # @param block [Proc] accepts a block, ensures all code inside
106
+ # that block gets executed even if there was an HTTP error.
107
+ #
108
+ # @return [Bool] true on hitting the retry limit, false on success
109
+
110
+ def retry_if_needed &block
111
+ @max_retries.times do |i|
112
+ begin
113
+ block.call
114
+ return false
115
+ rescue HTTP::TimeoutError
116
+ puts "caught HTTP Timeout error at #{Time.now} retrying #{@max_retries-i} more times"
117
+ end
118
+ end
119
+ return true
120
+ end
89
121
 
90
122
  end
91
123
  end
@@ -30,27 +30,14 @@ module Elephrame
30
30
  # *DOES NOT AUTOMATICALLY INCLUDE @'S*
31
31
  #
32
32
  # @param text [String] text to post as a reply
33
+ # @param options [Hash] a hash of arguments to pass to post, overrides
34
+ # duplicating settings from last mention
33
35
 
34
- def reply(text)
35
-
36
+ def reply(text, *options)
37
+ options = Hash[*options]
38
+
36
39
  # maybe also @ everyone from the mention? idk that seems like a bad idea tbh
37
- post(text, @mention_data[:vis], @mention_data[:spoiler],
38
- @mention_data[:id], @mention_data[:sensitive])
39
- end
40
-
41
- ##
42
- # Stores select data about a post into a hash for later use
43
- #
44
- # @param mention [Mastodon::Status] the most recent mention the bot received
45
-
46
- def store_mention_data(mention)
47
- @mention_data = {
48
- id: mention.id,
49
- vis: mention.visibility,
50
- spoiler: mention.spoiler_text,
51
- mentions: mention.mentions,
52
- sensitive: mention.sensitive?
53
- }
40
+ post(text, **@mention_data.merge(options).reject { |k| k == :mentions })
54
41
  end
55
42
 
56
43
  ##
@@ -75,6 +62,23 @@ module Elephrame
75
62
  end
76
63
 
77
64
  alias_method :run, :run_reply
65
+
66
+ private
67
+
68
+ ##
69
+ # Stores select data about a post into a hash for later use
70
+ #
71
+ # @param mention [Mastodon::Status] the most recent mention the bot received
72
+
73
+ def store_mention_data(mention)
74
+ @mention_data = {
75
+ reply_id: mention.id,
76
+ visibility: mention.visibility,
77
+ spoiler: mention.spoiler_text,
78
+ mentions: mention.mentions,
79
+ hide_media: mention.sensitive?
80
+ }
81
+ end
78
82
  end
79
83
 
80
84
 
@@ -1,3 +1,3 @@
1
1
  module Elephrame
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.6"
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.3.4
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-30 00:00:00.000000000 Z
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler