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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aca1612fd5c6ca9f4f7aa4c0bd7fadc3d0b105c7f22f9cfbebf02f9534c660d
4
- data.tar.gz: 3905910316783c900a90f5db2dc8349a0c878eee2a3d3bb18e72a06c3cbb3c7f
3
+ metadata.gz: 92e750042df55480f9181738fc2f74f331146f90243d5cb7534bf61ed7630b9c
4
+ data.tar.gz: a1321fb9515f8013d6526a58b5919424b1f05b56716635dfc3df90a690093e54
5
5
  SHA512:
6
- metadata.gz: 9a54bf01707af52ed15f3d03920309c7366ff7967e054aa31e39520d1cf271c56fd7a3ff414655cb7c3f7d4c97475a06f80e6ddb22d02970551791ab2398d4cf
7
- data.tar.gz: 454ec863e48e73c742c1a35c13ef000a2ff2ebaea5252e461885d1f64bbe2c1e87812e8a21d6df0b070e5247245aa5c09df5bea1b139e46470fbcb949afdcf77
6
+ metadata.gz: b265ea6a5efecf64b46587093b28aa3e34c5e943aa32950f5f124aa177268d25cc2608d87b0c5dda4f4d859afd9a4b6b63656adf2ebe2fea4ac144747de02225
7
+ data.tar.gz: a0bdfa34e1f16986d04eae7be4bef87b6ede08c8610da96a49fdd224117232a5ca4249c5465b6bdf167c20b4eca422284467c2382fdaa8163c8f927971a59240
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2024-04-28
3
+ ## [0.1.1] - 2024-04-29
4
+
5
+ - Change oauth2_client initialize
6
+
7
+
8
+ ## [0.1.0] - 2024-04-29
4
9
 
5
10
  - Initial release
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
- require 'x'
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 = RetriableX::Client::make_client(@args)
23
+ @client = make_client(@args)
23
24
  end
24
25
 
25
- def me()
26
- execute do |count|
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 |count|
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
- if e.is_a?(X::Unauthorized)
52
- # try once refresh
53
- if is_refresh?() && !refreshed_flag
54
- refreshed_flag = true
55
- refresh()
56
- continue
57
- else
58
- raise e
59
- end
60
- end
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 is_refresh?
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::new(@args[:client_key], @args[:client_secret], '')
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 self.make_client(args)
83
+ def make_client(args)
87
84
  if !args[:api_key].nil?
88
- X::Client.new(
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 'twitter_oauth2'
3
+ require "twitter_oauth2"
4
4
 
5
5
  module RetriableX
6
+ # OAuth2 Client
6
7
  class Oauth2Client
7
- def initialize(client_id, client_secret, redirect_uri)
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
@@ -2,29 +2,30 @@
2
2
 
3
3
  module RetriableX
4
4
  module Scope
5
- TweetRead = :"tweet.read"
6
- TweetWrite = :"tweet.write"
7
- TweetModerateWrite = :"tweet.moderate.write"
8
- UsersRead = :"users.read"
9
- FollowsRead = :"follows.read"
10
- FollowsWrite = :"follows.write"
11
- OfflineAccess = :"offline.access"
12
- SpaceRead = :"space.read"
13
- MuteRead = :"mute.read"
14
- MuteWrite = :"mute.write"
15
- LikeRead = :"like.read"
16
- LikeWrite = :"like.write"
17
- ListRead = :"list.read"
18
- ListWrite = :"list.write"
19
- BlockRead = :"block.read"
20
- BlockWrite = :"block.write"
21
- BookmarkRead = :"bookmark.read"
22
- BookmarkWrite = :"bookmark.write"
23
- DmRead = :"dm.read"
24
- DmWrite = :"dm.write"
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
- FollowCheck = [RetriableX::Scope::TweetRead, RetriableX::Scope::UsersRead, RetriableX::Scope::OfflineAccess]
28
+ FOLLOW_CHECK = [RetriableX::Scope::TWEET_READ, RetriableX::Scope::USERS_READ,
29
+ RetriableX::Scope::OFFLINE_ACCESS].freeze
29
30
  end
30
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RetriableX
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/retriable_x.rb CHANGED
@@ -5,7 +5,6 @@ require_relative "retriable_x/oauth2_client"
5
5
  require_relative "retriable_x/scope"
6
6
  require_relative "retriable_x/version"
7
7
 
8
-
9
8
  module RetriableX
10
9
  class Error < StandardError; end
11
10
  # Your code goes here...
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.0
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.2.0
76
+ version: 3.0.0
77
77
  required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="