retriable_x 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/lib/retriable_x/client.rb +35 -35
- data/lib/retriable_x/oauth2_client.rb +12 -7
- data/lib/retriable_x/scope.rb +22 -21
- data/lib/retriable_x/version.rb +1 -1
- data/lib/retriable_x.rb +0 -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: 92e750042df55480f9181738fc2f74f331146f90243d5cb7534bf61ed7630b9c
|
4
|
+
data.tar.gz: a1321fb9515f8013d6526a58b5919424b1f05b56716635dfc3df90a690093e54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b265ea6a5efecf64b46587093b28aa3e34c5e943aa32950f5f124aa177268d25cc2608d87b0c5dda4f4d859afd9a4b6b63656adf2ebe2fea4ac144747de02225
|
7
|
+
data.tar.gz: a0bdfa34e1f16986d04eae7be4bef87b6ede08c8610da96a49fdd224117232a5ca4249c5465b6bdf167c20b4eca422284467c2382fdaa8163c8f927971a59240
|
data/CHANGELOG.md
CHANGED
data/lib/retriable_x/client.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "json"
|
4
|
+
require "x"
|
5
5
|
|
6
6
|
module RetriableX
|
7
|
+
# X Client
|
7
8
|
class Client
|
8
9
|
# @param access_token [String] OAuth2 access token
|
9
10
|
# @param refresh_token [String] OAuth2 refresh token
|
@@ -19,27 +20,22 @@ module RetriableX
|
|
19
20
|
@args = args
|
20
21
|
@try_count = @args[:try_count] || 1
|
21
22
|
@retry_delay = @args[:retry_delay] || 0
|
22
|
-
@client =
|
23
|
+
@client = make_client(@args)
|
23
24
|
end
|
24
25
|
|
25
|
-
def me
|
26
|
-
execute do |
|
26
|
+
def me
|
27
|
+
execute do |_count|
|
27
28
|
@client.get("users/me")
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
def follow_check_screenname(screenname)
|
32
|
-
res = execute do |
|
33
|
+
res = execute do |_count|
|
33
34
|
@client.get("users/by/username/#{screenname}?user.fields=connection_status")
|
34
35
|
end
|
35
36
|
follow?(res)
|
36
37
|
end
|
37
38
|
|
38
|
-
def refresh(refresh_token)
|
39
|
-
@client.refresh_token = refresh_token
|
40
|
-
@client.access_token!
|
41
|
-
end
|
42
|
-
|
43
39
|
private
|
44
40
|
|
45
41
|
def execute
|
@@ -47,31 +43,32 @@ module RetriableX
|
|
47
43
|
refreshed_flag = false
|
48
44
|
loop do
|
49
45
|
return yield(count)
|
50
|
-
rescue => e
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
raise e if @try_count <= count
|
62
|
-
count += 1
|
63
|
-
sleep @retry_delay if @retry_delay > 0
|
46
|
+
rescue StandardError => e
|
47
|
+
count, refreshed_flag = check_err(e, count, refreshed_flag)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def check_err(err, count, refreshed_flag)
|
52
|
+
if err.is_a?(X::Unauthorized)
|
53
|
+
raise err unless refresh? && !refreshed_flag
|
54
|
+
|
55
|
+
refresh
|
56
|
+
return [count, true]
|
64
57
|
end
|
58
|
+
raise err if @try_count <= count
|
59
|
+
|
60
|
+
sleep @retry_delay if @retry_delay.positive?
|
61
|
+
[count + 1, refreshed_flag]
|
65
62
|
end
|
66
63
|
|
67
|
-
def
|
64
|
+
def refresh?
|
68
65
|
!@args[:refresh_token].nil? &&
|
69
66
|
!@args[:client_key].nil? &&
|
70
67
|
!@args[:client_secret].nil?
|
71
68
|
end
|
72
69
|
|
73
70
|
def refresh
|
74
|
-
client = RetriableX::Oauth2Client
|
71
|
+
client = RetriableX::Oauth2Client.new(@args)
|
75
72
|
token = client.refresh(@args[:refresh_token])
|
76
73
|
@args[:refresh_token] = token.refresh_token
|
77
74
|
@args[:access_token] = token.access_token
|
@@ -83,20 +80,23 @@ module RetriableX
|
|
83
80
|
connection_status.include?("following")
|
84
81
|
end
|
85
82
|
|
86
|
-
def
|
83
|
+
def make_client(args)
|
87
84
|
if !args[:api_key].nil?
|
88
|
-
|
89
|
-
api_key: args[:consumer_key],
|
90
|
-
api_key_secret: args[:consumer_secret],
|
91
|
-
access_token: args[:access_key],
|
92
|
-
access_token_secret: args[:access_secret] )
|
85
|
+
make_client_oauth1(args)
|
93
86
|
elsif !args[:access_token].nil?
|
94
87
|
X::Client.new(bearer_token: args[:access_token])
|
95
88
|
else
|
96
89
|
raise "OAuth key not found"
|
97
90
|
end
|
98
91
|
end
|
92
|
+
|
93
|
+
def make_client_oauth1(args)
|
94
|
+
X::Client.new(
|
95
|
+
api_key: args[:consumer_key],
|
96
|
+
api_key_secret: args[:consumer_secret],
|
97
|
+
access_token: args[:access_key],
|
98
|
+
access_token_secret: args[:access_secret]
|
99
|
+
)
|
100
|
+
end
|
99
101
|
end
|
100
102
|
end
|
101
|
-
|
102
|
-
|
@@ -1,14 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "twitter_oauth2"
|
4
4
|
|
5
5
|
module RetriableX
|
6
|
+
# OAuth2 Client
|
6
7
|
class Oauth2Client
|
7
|
-
|
8
|
+
# @param client_key [String] OAuth2 client_key
|
9
|
+
# @param client_secret [String] OAuth2 client_secret
|
10
|
+
# @param redirect_uri [String] OAuth2 redirect_uri
|
11
|
+
def initialize(**args)
|
12
|
+
@args = args
|
8
13
|
@client = TwitterOAuth2::Client.new(
|
9
|
-
identifier: client_id,
|
10
|
-
secret: client_secret,
|
11
|
-
redirect_uri: redirect_uri
|
14
|
+
identifier: @args[:client_id],
|
15
|
+
secret: @args[:client_secret],
|
16
|
+
redirect_uri: @args[:redirect_uri] || ""
|
12
17
|
)
|
13
18
|
end
|
14
19
|
|
@@ -17,12 +22,12 @@ module RetriableX
|
|
17
22
|
[authorization_uri, @client.code_verifier, @client.state]
|
18
23
|
end
|
19
24
|
|
20
|
-
def access_token(code, code_verifier)
|
25
|
+
def access_token!(code, code_verifier)
|
21
26
|
@client.authorization_code = code
|
22
27
|
@client.access_token! code_verifier
|
23
28
|
end
|
24
29
|
|
25
|
-
def refresh(refresh_token)
|
30
|
+
def refresh!(refresh_token)
|
26
31
|
@client.refresh_token = refresh_token
|
27
32
|
@client.access_token!
|
28
33
|
end
|
data/lib/retriable_x/scope.rb
CHANGED
@@ -2,29 +2,30 @@
|
|
2
2
|
|
3
3
|
module RetriableX
|
4
4
|
module Scope
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
5
|
+
TWEET_READ = :"tweet.read"
|
6
|
+
TWEET_WRITE = :"tweet.write"
|
7
|
+
TWEET_MODERATE_WRITE = :"tweet.moderate.write"
|
8
|
+
USERS_READ = :"users.read"
|
9
|
+
FOLLOWS_READ = :"follows.read"
|
10
|
+
FOLLOWS_WRITE = :"follows.write"
|
11
|
+
OFFLINE_ACCESS = :"offline.access"
|
12
|
+
SPACE_READ = :"space.read"
|
13
|
+
MUTE_READ = :"mute.read"
|
14
|
+
MUTE_WRITE = :"mute.write"
|
15
|
+
LIKE_READ = :"like.read"
|
16
|
+
LIKE_WRITE = :"like.write"
|
17
|
+
LIST_READ = :"list.read"
|
18
|
+
LIST_WRITE = :"list.write"
|
19
|
+
BLOCK_READ = :"block.read"
|
20
|
+
BLOCK_WRITE = :"block.write"
|
21
|
+
BOOKMARK_READ = :"bookmark.read"
|
22
|
+
BOOKMARK_WRITE = :"bookmark.write"
|
23
|
+
DM_READ = :"dm.read"
|
24
|
+
DM_WRITE = :"dm.write"
|
25
25
|
end
|
26
26
|
|
27
27
|
module Scopes
|
28
|
-
|
28
|
+
FOLLOW_CHECK = [RetriableX::Scope::TWEET_READ, RetriableX::Scope::USERS_READ,
|
29
|
+
RetriableX::Scope::OFFLINE_ACCESS].freeze
|
29
30
|
end
|
30
31
|
end
|
data/lib/retriable_x/version.rb
CHANGED
data/lib/retriable_x.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retriable_x
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aoyagikouhei
|
@@ -73,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements:
|
74
74
|
- - ">="
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: 3.
|
76
|
+
version: 3.0.0
|
77
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - ">="
|