twitter_tweet_bot 1.0.0 → 1.2.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 +4 -4
- data/README.md +18 -10
- 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/params/boolean_param.rb +13 -0
- data/lib/twitter_tweet_bot/api/params/comma_separated_param.rb +21 -0
- data/lib/twitter_tweet_bot/api/params/hash_param.rb +13 -0
- data/lib/twitter_tweet_bot/api/params/string_param.rb +13 -0
- data/lib/twitter_tweet_bot/api/params/tweet_params.rb +53 -0
- data/lib/twitter_tweet_bot/api/params/users_me_params.rb +43 -0
- data/lib/twitter_tweet_bot/api/refresh_token.rb +1 -4
- data/lib/twitter_tweet_bot/api/tweet.rb +14 -4
- data/lib/twitter_tweet_bot/api/users_me.rb +10 -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 +3 -2
- 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/cache.rb +1 -1
- data/lib/twitter_tweet_bot/client/api.rb +6 -6
- data/lib/twitter_tweet_bot/client/entity.rb +2 -2
- data/lib/twitter_tweet_bot/{configration.rb → configuration.rb} +2 -3
- data/lib/twitter_tweet_bot/entity/base.rb +14 -10
- data/lib/twitter_tweet_bot/entity/tweet.rb +4 -2
- data/lib/twitter_tweet_bot/entity/user.rb +4 -3
- data/lib/twitter_tweet_bot/version.rb +5 -1
- data/lib/twitter_tweet_bot.rb +5 -5
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0075abce451f1cc89c2cfaf56d04c9a18c3a1ae4e9bed984c8ec1a582689a944
|
4
|
+
data.tar.gz: 8d37895dec281683bf51db107ac3b4f02894d53380d797a7ff98aab0aa6d78a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0969b67fe82425486264df739eec73b98945eb339e39588d2117831949ca5667c8a2da7a9098dd25fc16a78fdb5318a416ddce331c939c0e34cd4b8dd553ada4'
|
7
|
+
data.tar.gz: ef888edd3e082f7371148c2471ce0ea0fa82c89da230506511681d7880df388ed72c349a8ee879debe1f7ffe2d3e9b65bf8fa82a6228d040026e844dd08eab00
|
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'
|
@@ -44,14 +44,14 @@ TwitterTweetBot.configure do |config|
|
|
44
44
|
# Redirect URL After Authorization
|
45
45
|
config.redirect_uri = 'https://example.com/twitter/callback'
|
46
46
|
# Twitter's App Scopes with OAuth 2.0
|
47
|
-
config.scopes = [
|
47
|
+
config.scopes = %w[tweet.read tweet.write users.read offline.access]
|
48
48
|
end
|
49
49
|
```
|
50
50
|
|
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,15 @@ TwitterTweetBot.post_tweet(token.access_token, 'Yeah!')
|
|
94
94
|
# @text="Yeah!">
|
95
95
|
```
|
96
96
|
|
97
|
-
|
97
|
+
##### With [some params](https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets)
|
98
|
+
|
99
|
+
```rb
|
100
|
+
TwitterTweetBot.post_tweet(token.access_token, 'Yeah! Yeah!') do |params|
|
101
|
+
params.reply = { in_reply_to_tweet_id: '*******************' }
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
#### Ex. Refresh an access token (required `'offline.access'` in scopes)
|
98
106
|
|
99
107
|
```rb
|
100
108
|
TwitterTweetBot.refresh_token(token.refresh_token)
|
@@ -122,10 +130,10 @@ TwitterTweetBot.post_tweet('Yeah!')
|
|
122
130
|
|
123
131
|
<summary>Details</summary>
|
124
132
|
|
125
|
-
#### Step1.
|
133
|
+
#### Step1. Configuration
|
126
134
|
|
127
135
|
```rb
|
128
|
-
require 'twitter_tweet_bot'
|
136
|
+
require 'twitter_tweet_bot/cache'
|
129
137
|
|
130
138
|
TwitterTweetBot.configure do |config|
|
131
139
|
# ...
|
@@ -139,7 +147,7 @@ end
|
|
139
147
|
|
140
148
|
```rb
|
141
149
|
# `code_verifier` and `state` will be cached.
|
142
|
-
TwitterTweetBot.
|
150
|
+
TwitterTweetBot.authorize
|
143
151
|
```
|
144
152
|
|
145
153
|
#### 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
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TwitterTweetBot
|
2
|
+
module API
|
3
|
+
module Params
|
4
|
+
module CommaSeparatedParam
|
5
|
+
DELIMITER = ','.freeze
|
6
|
+
|
7
|
+
def self.build(key, value)
|
8
|
+
{ key => formed_value(value) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.formed_value(value)
|
12
|
+
return value unless value.is_a?(Array)
|
13
|
+
|
14
|
+
value.join(DELIMITER)
|
15
|
+
end
|
16
|
+
|
17
|
+
private_constant :DELIMITER
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'twitter_tweet_bot/api/params/boolean_param'
|
2
|
+
require 'twitter_tweet_bot/api/params/string_param'
|
3
|
+
require 'twitter_tweet_bot/api/params/hash_param'
|
4
|
+
|
5
|
+
module TwitterTweetBot
|
6
|
+
module API
|
7
|
+
module Params
|
8
|
+
class TweetParams
|
9
|
+
private_class_method :new
|
10
|
+
|
11
|
+
# @yield [params]
|
12
|
+
# @yieldparam params [TwitterTweetBot::API::Params::TweetParams]
|
13
|
+
def self.build(&block)
|
14
|
+
new(&block).build
|
15
|
+
end
|
16
|
+
|
17
|
+
# @see https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets
|
18
|
+
AVAILABLE_PARAMS = {
|
19
|
+
for_super_followers_only: BooleanParam,
|
20
|
+
text: StringParam,
|
21
|
+
direct_message_deep_link: StringParam,
|
22
|
+
quote_tweet_id: StringParam,
|
23
|
+
reply_settings: StringParam,
|
24
|
+
geo: HashParam,
|
25
|
+
media: HashParam,
|
26
|
+
poll: HashParam,
|
27
|
+
reply: HashParam
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
# @yield [params]
|
31
|
+
# @yieldparam params [TwitterTweetBot::API::Params::TweetParams]
|
32
|
+
def initialize(&block)
|
33
|
+
block&.call(self)
|
34
|
+
end
|
35
|
+
|
36
|
+
def params
|
37
|
+
@params ||= {}
|
38
|
+
end
|
39
|
+
alias build params
|
40
|
+
|
41
|
+
private :params
|
42
|
+
|
43
|
+
AVAILABLE_PARAMS.each do |name, param_klass|
|
44
|
+
define_method("#{name}=") do |value|
|
45
|
+
params.merge!(param_klass.build(name, value))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private_constant :AVAILABLE_PARAMS
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'twitter_tweet_bot/api/params/comma_separated_param'
|
2
|
+
require 'twitter_tweet_bot/api/params/string_param'
|
3
|
+
|
4
|
+
module TwitterTweetBot
|
5
|
+
module API
|
6
|
+
module Params
|
7
|
+
class UsersMeParams
|
8
|
+
private_class_method :new
|
9
|
+
|
10
|
+
# @yield [params]
|
11
|
+
# @yieldparam params [TwitterTweetBot::API::Params::UsersMeParams]
|
12
|
+
def self.build(&block)
|
13
|
+
new(&block).build
|
14
|
+
end
|
15
|
+
|
16
|
+
# @yield [params]
|
17
|
+
# @yieldparam params [TwitterTweetBot::API::Params::UsersMeParams]
|
18
|
+
def initialize(&block)
|
19
|
+
block&.call(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
def params
|
23
|
+
@params ||= {}
|
24
|
+
end
|
25
|
+
alias build params
|
26
|
+
|
27
|
+
def expansions=(value)
|
28
|
+
params.merge!(StringParam.build('expansions', value))
|
29
|
+
end
|
30
|
+
|
31
|
+
def tweet_fields=(value)
|
32
|
+
params.merge!(CommaSeparatedParam.build('tweet.fields', value))
|
33
|
+
end
|
34
|
+
|
35
|
+
def user_fields=(value)
|
36
|
+
params.merge!(CommaSeparatedParam.build('user.fields', value))
|
37
|
+
end
|
38
|
+
|
39
|
+
private :params
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
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,
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'twitter_tweet_bot/api/http'
|
2
|
+
require 'twitter_tweet_bot/api/params/tweet_params'
|
2
3
|
|
3
4
|
module TwitterTweetBot
|
4
5
|
module API
|
@@ -9,19 +10,28 @@ module TwitterTweetBot
|
|
9
10
|
|
10
11
|
API_ENDPOTNT = 'https://api.twitter.com/2/tweets'.freeze
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
# @param [String] access_token
|
14
|
+
# @param [String] text
|
15
|
+
# @yield [params]
|
16
|
+
# @yieldparam params [TwitterTweetBot::API::Params::TweetParams]
|
17
|
+
def self.post(access_token:, text:, **, &block)
|
18
|
+
new(access_token).post(
|
19
|
+
Params::TweetParams.build do |params|
|
20
|
+
params.text = text
|
21
|
+
block&.call(params)
|
22
|
+
end
|
23
|
+
)
|
14
24
|
end
|
15
25
|
|
16
26
|
def initialize(access_token)
|
17
27
|
@access_token = access_token
|
18
28
|
end
|
19
29
|
|
20
|
-
def post(
|
30
|
+
def post(params)
|
21
31
|
request(
|
22
32
|
:post_json,
|
23
33
|
API_ENDPOTNT,
|
24
|
-
|
34
|
+
params,
|
25
35
|
bearer_authorization_header(access_token)
|
26
36
|
)
|
27
37
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'twitter_tweet_bot/api/http'
|
2
|
+
require 'twitter_tweet_bot/api/params/users_me_params'
|
2
3
|
|
3
4
|
module TwitterTweetBot
|
4
5
|
module API
|
@@ -9,19 +10,24 @@ module TwitterTweetBot
|
|
9
10
|
|
10
11
|
API_ENDPOTNT = 'https://api.twitter.com/2/users/me'.freeze
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
# @param [String] access_token
|
14
|
+
# @yield [params]
|
15
|
+
# @yieldparam params [TwitterTweetBot::API::Params::UsersMeParams]
|
16
|
+
def self.fetch(access_token:, **, &block)
|
17
|
+
new(access_token).fetch(
|
18
|
+
Params::UsersMeParams.build(&block)
|
19
|
+
)
|
14
20
|
end
|
15
21
|
|
16
22
|
def initialize(access_token)
|
17
23
|
@access_token = access_token
|
18
24
|
end
|
19
25
|
|
20
|
-
def fetch(
|
26
|
+
def fetch(params)
|
21
27
|
request(
|
22
28
|
:get,
|
23
29
|
API_ENDPOTNT,
|
24
|
-
|
30
|
+
params,
|
25
31
|
bearer_authorization_header(access_token)
|
26
32
|
)
|
27
33
|
end
|
@@ -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
|
@@ -2,8 +2,9 @@ require 'twitter_tweet_bot/cache/store'
|
|
2
2
|
|
3
3
|
module TwitterTweetBot
|
4
4
|
module Cache
|
5
|
-
module
|
6
|
-
attr_accessor :cache_provider
|
5
|
+
module ConfigurationExt
|
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
|
@@ -6,7 +6,7 @@ require 'twitter_tweet_bot/cache/entity_ext'
|
|
6
6
|
|
7
7
|
module TwitterTweetBot
|
8
8
|
Client.prepend(Cache::ClientExt)
|
9
|
-
|
9
|
+
Configuration.prepend(Cache::ConfigurationExt)
|
10
10
|
|
11
11
|
Entity::Authorization.include(Cache::EntityExt::Authorization)
|
12
12
|
Entity::Token.include(Cache::EntityExt::Token)
|
@@ -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,
|
@@ -28,15 +28,15 @@ module TwitterTweetBot
|
|
28
28
|
)
|
29
29
|
end
|
30
30
|
|
31
|
-
def post_tweet(access_token, text)
|
31
|
+
def post_tweet(access_token, text, &block)
|
32
32
|
TwitterTweetBot::API::Tweet.post(
|
33
|
-
**params_with_config(access_token: access_token, text: text)
|
33
|
+
**params_with_config(access_token: access_token, text: text), &block
|
34
34
|
)
|
35
35
|
end
|
36
36
|
|
37
|
-
def users_me(access_token,
|
37
|
+
def users_me(access_token, &block)
|
38
38
|
TwitterTweetBot::API::UsersMe.fetch(
|
39
|
-
**params_with_config(access_token: access_token,
|
39
|
+
**params_with_config(access_token: access_token), &block
|
40
40
|
)
|
41
41
|
end
|
42
42
|
|
@@ -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)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module TwitterTweetBot
|
2
|
-
class
|
2
|
+
class Configuration
|
3
3
|
attr_accessor :name,
|
4
4
|
:client_id,
|
5
5
|
:client_secret,
|
@@ -23,9 +23,8 @@ module TwitterTweetBot
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def to_hash
|
26
|
-
instance_variables.
|
26
|
+
instance_variables.each_with_object({}) do |key, hash|
|
27
27
|
hash[key[1..].to_sym] = instance_variable_get(key)
|
28
|
-
hash
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
@@ -7,24 +7,28 @@ module TwitterTweetBot
|
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
9
|
class_methods do
|
10
|
+
# @param [Array] fields
|
10
11
|
def act_as_entity(*fields)
|
11
12
|
class_attribute :fields,
|
12
13
|
instance_writer: false,
|
13
14
|
default: fields
|
14
15
|
|
15
|
-
attr_reader
|
16
|
-
end
|
17
|
-
end
|
16
|
+
attr_reader :raw
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
fields.each do |field|
|
19
|
+
define_method(field) { target_fields[field] }
|
20
|
+
end
|
22
21
|
|
23
|
-
|
22
|
+
# @param [Hash] hash
|
23
|
+
define_method(:initialize) { |hash| @raw = Hash(hash) }
|
24
|
+
|
25
|
+
define_method(:target_fields) { raw }
|
26
|
+
private :target_fields
|
27
|
+
end
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
|
29
|
+
# @param [Hash] hash
|
30
|
+
def build(hash)
|
31
|
+
new(hash)
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
require 'twitter_tweet_bot/entity/base'
|
3
2
|
|
4
3
|
module TwitterTweetBot
|
@@ -12,8 +11,10 @@ module TwitterTweetBot
|
|
12
11
|
:username
|
13
12
|
)
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
private
|
15
|
+
|
16
|
+
def target_fields
|
17
|
+
Hash(raw[:data])
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
data/lib/twitter_tweet_bot.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
require 'twitter_tweet_bot/client'
|
2
|
-
require 'twitter_tweet_bot/
|
2
|
+
require 'twitter_tweet_bot/configuration'
|
3
3
|
require 'twitter_tweet_bot/version'
|
4
4
|
|
5
5
|
require 'active_support/core_ext/module/attribute_accessors'
|
6
6
|
require 'active_support/core_ext/module/delegation'
|
7
7
|
|
8
8
|
module TwitterTweetBot
|
9
|
-
|
9
|
+
NoConfigurationError = Class.new(StandardError).freeze
|
10
10
|
|
11
11
|
mattr_accessor :default_config
|
12
12
|
|
13
13
|
class << self
|
14
14
|
def configure(&block)
|
15
|
-
self.default_config =
|
15
|
+
self.default_config = Configuration.new(&block)
|
16
16
|
end
|
17
17
|
|
18
18
|
def client(config = nil)
|
19
19
|
Client.new(config || default_config!)
|
20
20
|
end
|
21
21
|
|
22
|
-
delegate :
|
22
|
+
delegate :authorize,
|
23
23
|
:fetch_token,
|
24
24
|
:refresh_token,
|
25
25
|
:post_tweet,
|
@@ -29,7 +29,7 @@ module TwitterTweetBot
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def default_config!
|
32
|
-
default_config or raise
|
32
|
+
default_config or raise NoConfigurationError
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
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.2.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-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -42,6 +42,12 @@ files:
|
|
42
42
|
- lib/twitter_tweet_bot/api/http/get.rb
|
43
43
|
- lib/twitter_tweet_bot/api/http/headers.rb
|
44
44
|
- lib/twitter_tweet_bot/api/http/post.rb
|
45
|
+
- lib/twitter_tweet_bot/api/params/boolean_param.rb
|
46
|
+
- lib/twitter_tweet_bot/api/params/comma_separated_param.rb
|
47
|
+
- lib/twitter_tweet_bot/api/params/hash_param.rb
|
48
|
+
- lib/twitter_tweet_bot/api/params/string_param.rb
|
49
|
+
- lib/twitter_tweet_bot/api/params/tweet_params.rb
|
50
|
+
- lib/twitter_tweet_bot/api/params/users_me_params.rb
|
45
51
|
- lib/twitter_tweet_bot/api/refresh_token.rb
|
46
52
|
- lib/twitter_tweet_bot/api/tweet.rb
|
47
53
|
- lib/twitter_tweet_bot/api/users_me.rb
|
@@ -57,7 +63,7 @@ files:
|
|
57
63
|
- lib/twitter_tweet_bot/client.rb
|
58
64
|
- lib/twitter_tweet_bot/client/api.rb
|
59
65
|
- lib/twitter_tweet_bot/client/entity.rb
|
60
|
-
- lib/twitter_tweet_bot/
|
66
|
+
- lib/twitter_tweet_bot/configuration.rb
|
61
67
|
- lib/twitter_tweet_bot/entity.rb
|
62
68
|
- lib/twitter_tweet_bot/entity/authorization.rb
|
63
69
|
- lib/twitter_tweet_bot/entity/base.rb
|