twitter_tweet_bot 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -9
- data/lib/twitter_tweet_bot/api/access_token.rb +1 -3
- data/lib/twitter_tweet_bot/api/authorization/secure_code.rb +1 -1
- data/lib/twitter_tweet_bot/api/authorization.rb +4 -4
- data/lib/twitter_tweet_bot/api/http/error.rb +5 -6
- data/lib/twitter_tweet_bot/api/http/headers.rb +14 -2
- data/lib/twitter_tweet_bot/api/http/post.rb +2 -2
- data/lib/twitter_tweet_bot/api/refresh_token.rb +1 -4
- data/lib/twitter_tweet_bot/cache/caching.rb +2 -1
- data/lib/twitter_tweet_bot/cache/client_ext.rb +1 -1
- data/lib/twitter_tweet_bot/cache/configuration_ext.rb +2 -1
- data/lib/twitter_tweet_bot/cache/entity_ext/base.rb +2 -3
- data/lib/twitter_tweet_bot/cache/store.rb +1 -0
- data/lib/twitter_tweet_bot/client/api.rb +2 -2
- data/lib/twitter_tweet_bot/client/entity.rb +2 -2
- data/lib/twitter_tweet_bot/configration.rb +1 -2
- data/lib/twitter_tweet_bot/entity/base.rb +6 -2
- data/lib/twitter_tweet_bot/entity/tweet.rb +2 -2
- data/lib/twitter_tweet_bot/entity/user.rb +2 -3
- data/lib/twitter_tweet_bot/version.rb +5 -1
- data/lib/twitter_tweet_bot.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b94a383f3116ec1bba1e7a9b4ba71bb33a2254ac07e988160887318bd46d0ed
|
4
|
+
data.tar.gz: 50f312230ac41ebc3976a5fe72aee63075a9f5d324c5ddd75c76e56e8ed9bcda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4be6f7dfa8be6655a9cab92678b0b0b520c4f6a81a7a941077d46ada12230d3973774c92c8036d8082f6f4d439513f0211208c1497ebd855ec60c6e600b51347
|
7
|
+
data.tar.gz: 77bf8955e70e25140bb3cda5232dc310a4436ccf0c53b05482202b0c4b5f90344580a6626ee171c428fb128c7783429bede1af59b12a2b3b544013c334cf5b6b
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# twitter_tweet_bot
|
2
2
|
Tweet Bot with Twitter's V2 API.<br/>
|
3
|
-
(by
|
3
|
+
(by OAuth 2.0 with PCKE)
|
4
4
|
|
5
5
|
## Getting Started
|
6
6
|
|
@@ -19,7 +19,7 @@ require 'twitter_tweet_bot'
|
|
19
19
|
TwitterTweetBot.post_tweet(<ACEESS_TOKEN>, 'Yeah!')
|
20
20
|
```
|
21
21
|
|
22
|
-
1.
|
22
|
+
1. Configuration
|
23
23
|
2. Issue Authorization URL
|
24
24
|
3. Go to Authorization URL
|
25
25
|
4. Fetch Access Token
|
@@ -29,7 +29,7 @@ TwitterTweetBot.post_tweet(<ACEESS_TOKEN>, 'Yeah!')
|
|
29
29
|
|
30
30
|
<summary>Details</summary>
|
31
31
|
|
32
|
-
#### Step1.
|
32
|
+
#### Step1. Configuration
|
33
33
|
|
34
34
|
```rb
|
35
35
|
require 'twitter_tweet_bot'
|
@@ -51,7 +51,7 @@ end
|
|
51
51
|
#### Step2. Issue an authorization url
|
52
52
|
|
53
53
|
```rb
|
54
|
-
authorization = TwitterTweetBot.
|
54
|
+
authorization = TwitterTweetBot.authorize
|
55
55
|
# =>
|
56
56
|
# #<TwitterTweetBot::Entity::Authorization
|
57
57
|
# @code_verifier="*****",
|
@@ -59,7 +59,7 @@ authorization = TwitterTweetBot.authorization
|
|
59
59
|
# @url="https://twitter.com/i/oauth2/authorize?response_type=code&redirect_uri=<YOUR_REDIRECT_URI>&client_id=<YOUR_CLIENT_ID>&scope=<SCOPES>&code_challenge=*****&code_challenge_method=S256&state=***">
|
60
60
|
```
|
61
61
|
|
62
|
-
#### Step3.
|
62
|
+
#### Step3. Redirect (or Go) to `authorization.url`
|
63
63
|
|
64
64
|
And smash `"Authorize app"`.
|
65
65
|
|
@@ -94,7 +94,7 @@ TwitterTweetBot.post_tweet(token.access_token, 'Yeah!')
|
|
94
94
|
# @text="Yeah!">
|
95
95
|
```
|
96
96
|
|
97
|
-
#### Ex.
|
97
|
+
#### Ex. Refresh an access token (required `'offline.access'` in scopes)
|
98
98
|
|
99
99
|
```rb
|
100
100
|
TwitterTweetBot.refresh_token(token.refresh_token)
|
@@ -122,10 +122,10 @@ TwitterTweetBot.post_tweet('Yeah!')
|
|
122
122
|
|
123
123
|
<summary>Details</summary>
|
124
124
|
|
125
|
-
#### Step1.
|
125
|
+
#### Step1. Configuration
|
126
126
|
|
127
127
|
```rb
|
128
|
-
require 'twitter_tweet_bot'
|
128
|
+
require 'twitter_tweet_bot/cache'
|
129
129
|
|
130
130
|
TwitterTweetBot.configure do |config|
|
131
131
|
# ...
|
@@ -139,7 +139,7 @@ end
|
|
139
139
|
|
140
140
|
```rb
|
141
141
|
# `code_verifier` and `state` will be cached.
|
142
|
-
TwitterTweetBot.
|
142
|
+
TwitterTweetBot.authorize
|
143
143
|
```
|
144
144
|
|
145
145
|
#### Step3. Fetch an access token
|
@@ -11,7 +11,7 @@ module TwitterTweetBot
|
|
11
11
|
AUTH_URL = 'https://twitter.com/i/oauth2/authorize'.freeze
|
12
12
|
RESPONSE_TYPE = 'code'.freeze
|
13
13
|
|
14
|
-
def self.
|
14
|
+
def self.authorize(
|
15
15
|
client_id:,
|
16
16
|
redirect_uri:,
|
17
17
|
scopes:,
|
@@ -21,7 +21,7 @@ module TwitterTweetBot
|
|
21
21
|
**
|
22
22
|
)
|
23
23
|
new(client_id, redirect_uri, scopes)
|
24
|
-
.
|
24
|
+
.authorize(code_verifier, code_challenge_method, state)
|
25
25
|
end
|
26
26
|
|
27
27
|
def initialize(client_id, redirect_uri, scopes)
|
@@ -30,14 +30,14 @@ module TwitterTweetBot
|
|
30
30
|
@scopes = scopes
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
33
|
+
def authorize(code_verifier, code_challenge_method, state)
|
34
34
|
secure_code = Authorization::SecureCode.new(
|
35
35
|
code_verifier: code_verifier,
|
36
36
|
code_challenge_method: code_challenge_method,
|
37
37
|
state: state
|
38
38
|
)
|
39
39
|
uri = build_uri(AUTH_URL, build_body(secure_code))
|
40
|
-
|
40
|
+
as_hash(uri.to_s, secure_code)
|
41
41
|
end
|
42
42
|
|
43
43
|
private
|
@@ -3,15 +3,14 @@ module TwitterTweetBot
|
|
3
3
|
module HTTP
|
4
4
|
module Error
|
5
5
|
class RequestFaild < StandardError
|
6
|
-
attr_reader :code, :body
|
7
|
-
|
8
6
|
def initialize(code, body)
|
9
|
-
|
10
|
-
@body = body
|
7
|
+
super(format_message(code, body))
|
11
8
|
end
|
12
9
|
|
13
|
-
|
14
|
-
|
10
|
+
private
|
11
|
+
|
12
|
+
def format_message(code, body)
|
13
|
+
[code, body].join("\n")
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'base64'
|
1
2
|
require 'uri'
|
2
3
|
require 'net/http'
|
3
4
|
|
@@ -8,13 +9,24 @@ module TwitterTweetBot
|
|
8
9
|
BASIC_AUTHORIZATION = 'Basic %<credentials>s'.freeze
|
9
10
|
BEARER_AUTHORIZATION = 'Bearer %<credentials>s'.freeze
|
10
11
|
|
11
|
-
def basic_authorization_header(
|
12
|
-
{
|
12
|
+
def basic_authorization_header(user, password)
|
13
|
+
{
|
14
|
+
authorization: format(
|
15
|
+
BASIC_AUTHORIZATION,
|
16
|
+
credentials: authorization_credentials(user, password)
|
17
|
+
)
|
18
|
+
}
|
13
19
|
end
|
14
20
|
|
15
21
|
def bearer_authorization_header(credentials)
|
16
22
|
{ authorization: format(BEARER_AUTHORIZATION, credentials: credentials) }
|
17
23
|
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def authorization_credentials(user, password)
|
28
|
+
Base64.strict_encode64("#{user}:#{password}")
|
29
|
+
end
|
18
30
|
end
|
19
31
|
end
|
20
32
|
end
|
@@ -9,8 +9,8 @@ module TwitterTweetBot
|
|
9
9
|
module Post
|
10
10
|
include Base
|
11
11
|
|
12
|
-
JSON_CONTENT_TYPE = 'application/json'.freeze
|
13
12
|
URLENCODED_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=UTF-8'.freeze
|
13
|
+
JSON_CONTENT_TYPE = 'application/json'.freeze
|
14
14
|
|
15
15
|
def request_post_form(url, body, headers = {})
|
16
16
|
request_post(
|
@@ -39,7 +39,7 @@ module TwitterTweetBot
|
|
39
39
|
end
|
40
40
|
|
41
41
|
private_constant :JSON_CONTENT_TYPE,
|
42
|
-
|
42
|
+
:URLENCODED_CONTENT_TYPE
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'base64'
|
2
1
|
require 'twitter_tweet_bot/api/http'
|
3
2
|
|
4
3
|
module TwitterTweetBot
|
@@ -43,9 +42,7 @@ module TwitterTweetBot
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def headers
|
46
|
-
basic_authorization_header(
|
47
|
-
Base64.strict_encode64("#{client_id}:#{client_secret}")
|
48
|
-
)
|
45
|
+
basic_authorization_header(client_id, client_secret)
|
49
46
|
end
|
50
47
|
|
51
48
|
private_constant :API_ENDPOTNT,
|
@@ -7,7 +7,7 @@ module TwitterTweetBot
|
|
7
7
|
|
8
8
|
def with_cache(&block)
|
9
9
|
current_cache = read_cache
|
10
|
-
result =
|
10
|
+
result = block.call(current_cache)
|
11
11
|
write_cache(current_cache, result)
|
12
12
|
|
13
13
|
result
|
@@ -25,6 +25,7 @@ module TwitterTweetBot
|
|
25
25
|
|
26
26
|
def to_cache_object(object)
|
27
27
|
return {} unless object.respond_to?(:to_cache)
|
28
|
+
|
28
29
|
object.to_cache
|
29
30
|
end
|
30
31
|
end
|
@@ -3,7 +3,8 @@ require 'twitter_tweet_bot/cache/store'
|
|
3
3
|
module TwitterTweetBot
|
4
4
|
module Cache
|
5
5
|
module ConfigrationExt
|
6
|
-
attr_accessor :cache_provider
|
6
|
+
attr_accessor :cache_provider
|
7
|
+
attr_writer :cache
|
7
8
|
|
8
9
|
def initialize(cache_provider: nil, **kwargs)
|
9
10
|
@cache_provider = cache_provider
|
@@ -15,14 +15,13 @@ module TwitterTweetBot
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def to_cache
|
18
|
-
cache_fields.
|
18
|
+
cache_fields.each_with_object({}) do |cache_field, hash|
|
19
19
|
next hash unless respond_to?(cache_field)
|
20
20
|
|
21
21
|
value = send(cache_field)
|
22
22
|
next hash if value.nil?
|
23
|
-
hash[cache_field] = value
|
24
23
|
|
25
|
-
hash
|
24
|
+
hash[cache_field] = value
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
@@ -3,8 +3,8 @@ require 'twitter_tweet_bot/api'
|
|
3
3
|
module TwitterTweetBot
|
4
4
|
class Client
|
5
5
|
module API
|
6
|
-
def
|
7
|
-
TwitterTweetBot::API::Authorization.
|
6
|
+
def authorize(code_verifier = nil, code_challenge_method = nil, state = nil)
|
7
|
+
TwitterTweetBot::API::Authorization.authorize(
|
8
8
|
**params_with_config(
|
9
9
|
code_verifier: code_verifier,
|
10
10
|
code_challenge_method: code_challenge_method,
|
@@ -4,7 +4,7 @@ require 'twitter_tweet_bot/entity'
|
|
4
4
|
module TwitterTweetBot
|
5
5
|
class Client
|
6
6
|
module Entity
|
7
|
-
def
|
7
|
+
def authorize(*)
|
8
8
|
with_entity(TwitterTweetBot::Entity::Authorization) { super }
|
9
9
|
end
|
10
10
|
|
@@ -27,7 +27,7 @@ module TwitterTweetBot
|
|
27
27
|
private
|
28
28
|
|
29
29
|
def with_entity(entity_klass, &block)
|
30
|
-
entity_klass.
|
30
|
+
entity_klass.build(block.call)
|
31
31
|
end
|
32
32
|
|
33
33
|
def parse_json(body)
|
@@ -14,15 +14,19 @@ module TwitterTweetBot
|
|
14
14
|
|
15
15
|
attr_reader(*fields)
|
16
16
|
end
|
17
|
+
|
18
|
+
def build(json)
|
19
|
+
new(json)
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
def initialize(json)
|
20
|
-
|
24
|
+
initialize_fields!(Hash(json))
|
21
25
|
end
|
22
26
|
|
23
27
|
private
|
24
28
|
|
25
|
-
def
|
29
|
+
def initialize_fields!(hash)
|
26
30
|
fields.each do |field|
|
27
31
|
instance_variable_set(:"@#{field}", hash[field])
|
28
32
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
require 'twitter_tweet_bot/entity/base'
|
3
2
|
|
4
3
|
module TwitterTweetBot
|
@@ -12,8 +11,8 @@ module TwitterTweetBot
|
|
12
11
|
:username
|
13
12
|
)
|
14
13
|
|
15
|
-
def
|
16
|
-
super
|
14
|
+
def self.build(json)
|
15
|
+
super(Hash(json)[:data])
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
data/lib/twitter_tweet_bot.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitter_tweet_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SongCastle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|