twitter_tweet_bot 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b94a383f3116ec1bba1e7a9b4ba71bb33a2254ac07e988160887318bd46d0ed
4
- data.tar.gz: 50f312230ac41ebc3976a5fe72aee63075a9f5d324c5ddd75c76e56e8ed9bcda
3
+ metadata.gz: 0075abce451f1cc89c2cfaf56d04c9a18c3a1ae4e9bed984c8ec1a582689a944
4
+ data.tar.gz: 8d37895dec281683bf51db107ac3b4f02894d53380d797a7ff98aab0aa6d78a0
5
5
  SHA512:
6
- metadata.gz: 4be6f7dfa8be6655a9cab92678b0b0b520c4f6a81a7a941077d46ada12230d3973774c92c8036d8082f6f4d439513f0211208c1497ebd855ec60c6e600b51347
7
- data.tar.gz: 77bf8955e70e25140bb3cda5232dc310a4436ccf0c53b05482202b0c4b5f90344580a6626ee171c428fb128c7783429bede1af59b12a2b3b544013c334cf5b6b
6
+ metadata.gz: '0969b67fe82425486264df739eec73b98945eb339e39588d2117831949ca5667c8a2da7a9098dd25fc16a78fdb5318a416ddce331c939c0e34cd4b8dd553ada4'
7
+ data.tar.gz: ef888edd3e082f7371148c2471ce0ea0fa82c89da230506511681d7880df388ed72c349a8ee879debe1f7ffe2d3e9b65bf8fa82a6228d040026e844dd08eab00
data/README.md CHANGED
@@ -44,7 +44,7 @@ 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 = ['tweet.read', 'tweet.write', 'users.read', 'offline.access']
47
+ config.scopes = %w[tweet.read tweet.write users.read offline.access]
48
48
  end
49
49
  ```
50
50
 
@@ -94,6 +94,14 @@ TwitterTweetBot.post_tweet(token.access_token, 'Yeah!')
94
94
  # @text="Yeah!">
95
95
  ```
96
96
 
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
+
97
105
  #### Ex. Refresh an access token (required `'offline.access'` in scopes)
98
106
 
99
107
  ```rb
@@ -0,0 +1,13 @@
1
+ module TwitterTweetBot
2
+ module API
3
+ module Params
4
+ module BooleanParam
5
+ def self.build(key, value)
6
+ return {} unless value.is_a?(TrueClass) || value.is_a?(FalseClass)
7
+
8
+ { key => value }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ 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,13 @@
1
+ module TwitterTweetBot
2
+ module API
3
+ module Params
4
+ module HashParam
5
+ def self.build(key, value)
6
+ return {} unless value.is_a?(Hash)
7
+
8
+ { key => value }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module TwitterTweetBot
2
+ module API
3
+ module Params
4
+ module StringParam
5
+ def self.build(key, value)
6
+ return {} unless value.is_a?(String)
7
+
8
+ { key => value }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ 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,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
- def self.post(access_token:, text:, **)
13
- new(access_token).post(text)
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(text)
30
+ def post(params)
21
31
  request(
22
32
  :post_json,
23
33
  API_ENDPOTNT,
24
- { text: text },
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
- def self.fetch(access_token:, fields:, **)
13
- new(access_token).fetch(fields)
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(fields)
26
+ def fetch(params)
21
27
  request(
22
28
  :get,
23
29
  API_ENDPOTNT,
24
- fields,
30
+ params,
25
31
  bearer_authorization_header(access_token)
26
32
  )
27
33
  end
@@ -2,7 +2,7 @@ require 'twitter_tweet_bot/cache/store'
2
2
 
3
3
  module TwitterTweetBot
4
4
  module Cache
5
- module ConfigrationExt
5
+ module ConfigurationExt
6
6
  attr_accessor :cache_provider
7
7
  attr_writer :cache
8
8
 
@@ -6,7 +6,7 @@ require 'twitter_tweet_bot/cache/entity_ext'
6
6
 
7
7
  module TwitterTweetBot
8
8
  Client.prepend(Cache::ClientExt)
9
- Configration.prepend(Cache::ConfigrationExt)
9
+ Configuration.prepend(Cache::ConfigurationExt)
10
10
 
11
11
  Entity::Authorization.include(Cache::EntityExt::Authorization)
12
12
  Entity::Token.include(Cache::EntityExt::Token)
@@ -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, fields = {})
37
+ def users_me(access_token, &block)
38
38
  TwitterTweetBot::API::UsersMe.fetch(
39
- **params_with_config(access_token: access_token, fields: fields)
39
+ **params_with_config(access_token: access_token), &block
40
40
  )
41
41
  end
42
42
 
@@ -1,5 +1,5 @@
1
1
  module TwitterTweetBot
2
- class Configration
2
+ class Configuration
3
3
  attr_accessor :name,
4
4
  :client_id,
5
5
  :client_secret,
@@ -7,28 +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(*fields)
16
- end
16
+ attr_reader :raw
17
17
 
18
- def build(json)
19
- new(json)
20
- end
21
- end
18
+ fields.each do |field|
19
+ define_method(field) { target_fields[field] }
20
+ end
22
21
 
23
- def initialize(json)
24
- initialize_fields!(Hash(json))
25
- end
22
+ # @param [Hash] hash
23
+ define_method(:initialize) { |hash| @raw = Hash(hash) }
26
24
 
27
- private
25
+ define_method(:target_fields) { raw }
26
+ private :target_fields
27
+ end
28
28
 
29
- def initialize_fields!(hash)
30
- fields.each do |field|
31
- instance_variable_set(:"@#{field}", hash[field])
29
+ # @param [Hash] hash
30
+ def build(hash)
31
+ new(hash)
32
32
  end
33
33
  end
34
34
  end
@@ -11,8 +11,10 @@ module TwitterTweetBot
11
11
  :edit_history_tweet_ids
12
12
  )
13
13
 
14
- def self.build(json)
15
- super(Hash(json)[:data])
14
+ private
15
+
16
+ def target_fields
17
+ Hash(raw[:data])
16
18
  end
17
19
  end
18
20
  end
@@ -11,8 +11,10 @@ module TwitterTweetBot
11
11
  :username
12
12
  )
13
13
 
14
- def self.build(json)
15
- super(Hash(json)[:data])
14
+ private
15
+
16
+ def target_fields
17
+ Hash(raw[:data])
16
18
  end
17
19
  end
18
20
  end
@@ -1,7 +1,7 @@
1
1
  module TwitterTweetBot
2
2
  module Version
3
3
  def self.current
4
- Gem::Version.new('1.1.0')
4
+ Gem::Version.new('1.2.0')
5
5
  end
6
6
  end
7
7
  end
@@ -1,18 +1,18 @@
1
1
  require 'twitter_tweet_bot/client'
2
- require 'twitter_tweet_bot/configration'
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
- NoConfigrationError = Class.new(StandardError).freeze
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 = Configration.new(&block)
15
+ self.default_config = Configuration.new(&block)
16
16
  end
17
17
 
18
18
  def client(config = nil)
@@ -29,7 +29,7 @@ module TwitterTweetBot
29
29
  private
30
30
 
31
31
  def default_config!
32
- default_config or raise NoConfigrationError
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.1.0
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-06-03 00:00:00.000000000 Z
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/configration.rb
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