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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a2b63d602240d7b56c182f40ec07306600818005a0b5d35b9cf6f8a93e405cf
4
- data.tar.gz: 9c2b1c30135546c64319ddaf38bf75ee15184d9d3a7b0d42404c96380b036ba0
3
+ metadata.gz: 5b94a383f3116ec1bba1e7a9b4ba71bb33a2254ac07e988160887318bd46d0ed
4
+ data.tar.gz: 50f312230ac41ebc3976a5fe72aee63075a9f5d324c5ddd75c76e56e8ed9bcda
5
5
  SHA512:
6
- metadata.gz: 453ac53d4a7240829771bee7d8a94e5b1b1a5bfebdb26352fbe63579bdad9df20ac2da805da551f8a8d5f9a20ab9c3e6361dbcc5c8c96e8a14f42e3201d0d7c9
7
- data.tar.gz: 6ae79d1b5ac9de5a26830eb28ba6ae8a62e0df6e85beda2ae9975b3d625bfab7495b5b5d6a704dfd66e5b6327b7e04406b86dfd3d8587d0140058889ce73922a
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 Twitter's V2 API / OAuth 2.0 with PCKE)
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. Congiuration
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. Congiuration
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.authorization
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. Rerirect (or Go) to `authorization.url`
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. Restore an access token (required `'offline.access'` in scopes)
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. Congiuration
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.authorization
142
+ TwitterTweetBot.authorize
143
143
  ```
144
144
 
145
145
  #### Step3. Fetch an access token
@@ -52,9 +52,7 @@ module TwitterTweetBot
52
52
  end
53
53
 
54
54
  def headers
55
- basic_authorization_header(
56
- Base64.strict_encode64("#{client_id}:#{client_secret}")
57
- )
55
+ basic_authorization_header(client_id, client_secret)
58
56
  end
59
57
 
60
58
  private_constant :API_ENDPOTNT,
@@ -26,7 +26,7 @@ module TwitterTweetBot
26
26
  Base64.urlsafe_encode64(digest_by_sha256(verifier), padding: false)
27
27
  )
28
28
  else # 'plain'
29
- verifier
29
+ encode(verifier)
30
30
  end
31
31
  end
32
32
 
@@ -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.authorization(
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
- .authorization(code_verifier, code_challenge_method, state)
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 authorization(code_verifier, code_challenge_method, state)
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
- return as_hash(uri.to_s, secure_code)
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
- @code = code
10
- @body = body
7
+ super(format_message(code, body))
11
8
  end
12
9
 
13
- def inspect
14
- "#<#{self.class} #{code} #{body}>"
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(credentials)
12
- { authorization: format(BASIC_AUTHORIZATION, credentials: credentials) }
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
- :URLENCODED_CONTENT_TYPE
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 = yield(current_cache)
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
@@ -5,7 +5,7 @@ module TwitterTweetBot
5
5
  module ClientExt
6
6
  include Caching
7
7
 
8
- def authorization(*)
8
+ def authorize(*)
9
9
  with_cache { super }
10
10
  end
11
11
 
@@ -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, :cache
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.reduce({}) do |hash, cache_field|
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
@@ -32,6 +32,7 @@ module TwitterTweetBot
32
32
 
33
33
  def deserialize_value(value)
34
34
  return {} if value.nil?
35
+
35
36
  JSON.parse(value, symbolize_names: true)
36
37
  end
37
38
 
@@ -3,8 +3,8 @@ require 'twitter_tweet_bot/api'
3
3
  module TwitterTweetBot
4
4
  class Client
5
5
  module API
6
- def authorization(code_verifier = nil, code_challenge_method = nil, state = nil)
7
- TwitterTweetBot::API::Authorization.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 authorization(*)
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.new(yield)
30
+ entity_klass.build(block.call)
31
31
  end
32
32
 
33
33
  def parse_json(body)
@@ -23,9 +23,8 @@ module TwitterTweetBot
23
23
  end
24
24
 
25
25
  def to_hash
26
- instance_variables.reduce({}) do |hash, key|
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
@@ -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
- set_fields!(Hash(json))
24
+ initialize_fields!(Hash(json))
21
25
  end
22
26
 
23
27
  private
24
28
 
25
- def set_fields!(hash)
29
+ def initialize_fields!(hash)
26
30
  fields.each do |field|
27
31
  instance_variable_set(:"@#{field}", hash[field])
28
32
  end
@@ -11,8 +11,8 @@ module TwitterTweetBot
11
11
  :edit_history_tweet_ids
12
12
  )
13
13
 
14
- def initialize(data)
15
- super Hash(data)[:data]
14
+ def self.build(json)
15
+ super(Hash(json)[:data])
16
16
  end
17
17
  end
18
18
  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 initialize(data)
16
- super Hash(data)[:data]
14
+ def self.build(json)
15
+ super(Hash(json)[:data])
17
16
  end
18
17
  end
19
18
  end
@@ -1,3 +1,7 @@
1
1
  module TwitterTweetBot
2
- VERSION = '1.0.0'.freeze
2
+ module Version
3
+ def self.current
4
+ Gem::Version.new('1.1.0')
5
+ end
6
+ end
3
7
  end
@@ -19,7 +19,7 @@ module TwitterTweetBot
19
19
  Client.new(config || default_config!)
20
20
  end
21
21
 
22
- delegate :authorization,
22
+ delegate :authorize,
23
23
  :fetch_token,
24
24
  :refresh_token,
25
25
  :post_tweet,
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.0.0
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-05-14 00:00:00.000000000 Z
11
+ date: 2023-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport