simple_twitter 2.0.0 → 2.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: 6658a0b3e20a5da4a1a59d28ee5062bb2f1e816e6298cf49dcc48d0cfda2bcbb
4
- data.tar.gz: 7295ee6a0d9ecd2ea5fe264bfcbe4357d8e2cde9b40db0001f0ebb39e7479399
3
+ metadata.gz: 9efa656f4b989f1702d73b9fc4dcd1526edeb944fd9de450993cea3a359ac980
4
+ data.tar.gz: eb2027e3aaf434de6014f8ad60775af313c6a3bc88dfca3ac350dd5ad4798c91
5
5
  SHA512:
6
- metadata.gz: aa8c402510a617b96a6dd6c239c7d4d9b0c9e01fc7202f371ba826b095c01542ba2ee76ef11d953b7aa76e3c8099597cf3d0bd2da7fa264c98214c87ad8b1e96
7
- data.tar.gz: 02eead6edb730eb7562dc7869ca15d8908b5424526a902afc68912f69a1aa847c32531d57291802237854dc84af3681a931474362e0704eec31740057c59f55d
6
+ metadata.gz: d7f681333e86714dbf3aba86319761118c002b914f0d45579b9c35bcb0d0937a4a276ee04137c7cc76cdf66ddd25ab057e431420bd3b95b0fa91e411a96925d0
7
+ data.tar.gz: 7c0159dbc2da88fed921c25887488b5c81406ebcb4793ff10bf76de8d447cb5d8d61f7954c0c44a0b766e3ee1828d68f6ef9c9fa2812eaf709ab5f705d678579
data/.env.example ADDED
@@ -0,0 +1,5 @@
1
+ # TWITTER_OAUTH2_BEARER_TOKEN=
2
+ # TWITTER_OAUTH1_API_KEY=
3
+ # TWITTER_OAUTH1_API_SECRET_KEY=
4
+ # TWITTER_OAUTH1_ACCESS_TOKEN=
5
+ # TWITTER_OAUTH1_ACCESS_TOKEN_SECRET=
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  Gemfile.lock
10
+ .env
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Unreleased
2
- https://github.com/yhara/simple_twitter/compare/v2.0.0...main
2
+ https://github.com/yhara/simple_twitter/compare/v2.1.0...main
3
+
4
+ ## v2.1.0 (2023-07-22)
5
+ https://github.com/yhara/simple_twitter/compare/v2.0.0...v2.1.0
6
+
7
+ * Support media uploading
8
+ * https://github.com/yhara/simple_twitter/pull/26
9
+ * Add User-Agent
10
+ * https://github.com/yhara/simple_twitter/pull/27
3
11
 
4
12
  ## v2.0.0 (2023-07-17)
5
13
  https://github.com/yhara/simple_twitter/compare/v1.0.0...v2.0.0
data/README.md CHANGED
@@ -77,6 +77,48 @@ client = SimpleTwitter::Client.new(bearer_token: ENV["ACCESS_TOKEN"])
77
77
  client.post("https://api.twitter.com/2/tweets", json: { text: "Hello twitter!" })
78
78
  ```
79
79
 
80
+ ### Upload media
81
+ If you want to tweet with an image, you need to do the following steps
82
+
83
+ 1. Upload image as media
84
+ * c.f. https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload
85
+ 2. Tweet with media
86
+ * c.f. https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets
87
+
88
+ e.g.
89
+
90
+ ```ruby
91
+ config = (load from yaml or something)
92
+ client = SimpleTwitter::Client.new(
93
+ api_key: config[:api_key],
94
+ api_secret_key: config[:api_secret_key],
95
+ access_token: config[:access_token],
96
+ access_token_secret: config[:access_token_secret],
97
+ )
98
+
99
+ # Upload image as media
100
+ media = client.post(
101
+ "https://upload.twitter.com/1.1/media/upload.json",
102
+ form: {
103
+ media: HTTP::FormData::File.new("/path/to/image.png")
104
+ }
105
+ )
106
+ # =>
107
+ # {:media_id=>12345678901234567890,
108
+ # :media_id_string=>"12345678901234567890",
109
+ # :size=>60628,
110
+ # :expires_after_secs=>86400,
111
+ # :image=>{:image_type=>"image/png", :w=>400, :h=>400}}
112
+
113
+ # Tweet with media
114
+ client.post("https://api.twitter.com/2/tweets",
115
+ json: {
116
+ text: "Test tweet with image",
117
+ media: { media_ids: [media[:media_id_string]] },
118
+ }
119
+ )
120
+ ```
121
+
80
122
  ### Advanced
81
123
 
82
124
  If you want the raw json string or use streaming API, use `get_raw`, `post_raw`, etc. which returns `HTTP::Response` of the [http gem](https://github.com/httprb/http).
data/bin/console CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "bundler/setup"
5
5
  require "simple_twitter"
6
+ require "dotenv/load"
6
7
 
7
8
  # You can add fixtures and/or initialization code here to make experimenting
8
9
  # with your gem easier. You can also use a different console, if you like.
@@ -11,5 +12,13 @@ require "simple_twitter"
11
12
  # require "pry"
12
13
  # Pry.start
13
14
 
15
+ @client = SimpleTwitter::Client.new(
16
+ bearer_token: ENV["TWITTER_OAUTH2_BEARER_TOKEN"],
17
+ api_key: ENV["TWITTER_OAUTH1_API_KEY"],
18
+ api_secret_key: ENV["TWITTER_OAUTH1_API_SECRET_KEY"],
19
+ access_token: ENV["TWITTER_OAUTH1_ACCESS_TOKEN"],
20
+ access_token_secret: ENV["TWITTER_OAUTH1_ACCESS_TOKEN_SECRET"],
21
+ )
22
+
14
23
  require "irb"
15
24
  IRB.start(__FILE__)
@@ -1,11 +1,11 @@
1
1
  module SimpleTwitter
2
2
  # Twitter API Client
3
3
  class Client
4
- # @param bearer_token [String] This requires for API v2
5
- # @param api_key [String] This requires for API v1.1
6
- # @param api_secret_key [String] This requires for API v1.1
7
- # @param access_token [String] This requires for API v1.1
8
- # @param access_token_secret [String] This requires for API v1.1
4
+ # @param bearer_token [String] This requires for OAuth 2
5
+ # @param api_key [String] This requires for OAuth 1.0a
6
+ # @param api_secret_key [String] This requires for OAuth 1.0a
7
+ # @param access_token [String] This requires for OAuth 1.0a
8
+ # @param access_token_secret [String] This requires for OAuth 1.0a
9
9
  def initialize(bearer_token: nil,
10
10
  api_key: nil,
11
11
  api_secret_key: nil,
@@ -23,80 +23,87 @@ module SimpleTwitter
23
23
  end
24
24
  end
25
25
 
26
- # @!method get(url, params: {}, json: {})
26
+ # @!method get(url, params: {}, json: {}, form: {})
27
27
  # Call Twitter API with GET method
28
28
  # @param url [String]
29
29
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
30
30
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
31
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
31
32
  # @return [Hash] parsed json data
32
33
  # @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
33
34
  # @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
34
35
 
35
- # @!method get_raw(url, params: {}, json: {})
36
+ # @!method get_raw(url, params: {}, json: {}, form: {})
36
37
  # Call Twitter API with GET method
37
38
  # @param url [String]
38
39
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
39
40
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
41
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
40
42
  # @return [HTTP::Response]
41
43
 
42
- # @!method post(url, params: {}, json: {})
44
+ # @!method post(url, params: {}, json: {}, form: {})
43
45
  # Call Twitter API with POST method
44
46
  # @param url [String]
45
47
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
46
48
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
49
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
47
50
  # @return [Hash] parsed json data
48
51
  # @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
49
52
  # @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
50
53
 
51
- # @!method post_raw(url, params: {}, json: {})
54
+ # @!method post_raw(url, params: {}, json: {}, form: {})
52
55
  # Call Twitter API with POST method
53
56
  # @param url [String]
54
57
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
55
58
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
59
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
56
60
  # @return [HTTP::Response]
57
61
 
58
- # @!method put(url, params: {}, json: {})
62
+ # @!method put(url, params: {}, json: {}, form: {})
59
63
  # Call Twitter API with PUT method
60
64
  # @param url [String]
61
65
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
62
66
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
67
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
63
68
  # @return [Hash] parsed json data
64
69
  # @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
65
70
  # @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
66
71
 
67
- # @!method put_raw(url, params: {}, json: {})
72
+ # @!method put_raw(url, params: {}, json: {}, form: {})
68
73
  # Call Twitter API with PUT method
69
74
  # @param url [String]
70
75
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
71
76
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
77
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
72
78
  # @return [HTTP::Response]
73
79
 
74
- # @!method delete(url, params: {}, json: {})
80
+ # @!method delete(url, params: {}, json: {}, form: {})
75
81
  # Call Twitter API with DELETE method
76
82
  # @param url [String]
77
83
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
78
84
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
85
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
79
86
  # @return [Hash] parsed json data
80
87
  # @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
81
88
  # @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
82
89
 
83
- # @!method delete_raw(url, params: {}, json: {})
90
+ # @!method delete_raw(url, params: {}, json: {}, form: {})
84
91
  # Call Twitter API with DELETE method
85
92
  # @param url [String]
86
93
  # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
87
94
  # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
95
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
88
96
  # @return [HTTP::Response]
89
97
 
90
98
  %i[get post put delete].each do |m|
91
99
  class_eval <<~EOD
92
- def #{m}(url, params: {}, json: {})
93
- res = #{m}_raw(url, params: params, json: json)
100
+ def #{m}(url, params: {}, json: {}, form: {})
101
+ res = #{m}_raw(url, params: params, json: json, form: form)
94
102
  parse_response(res)
95
103
  end
96
104
 
97
- def #{m}_raw(url, params: {}, json: {})
98
- args = { params: params }
99
- args[:json] = json unless json.empty?
105
+ def #{m}_raw(url, params: {}, json: {}, form: {})
106
+ args = create_http_args(params: params, json: json, form: form)
100
107
  http(:#{m}, url, params).#{m}(url, args)
101
108
  end
102
109
  EOD
@@ -104,6 +111,22 @@ module SimpleTwitter
104
111
 
105
112
  private
106
113
 
114
+ # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
115
+ # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
116
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
117
+ # @return [Hash<Symbol, Object>]
118
+ def create_http_args(params:, json:, form:)
119
+ args = {
120
+ params: params,
121
+ headers: {
122
+ "User-Agent" => "simple_twitter v#{SimpleTwitter::VERSION} (https://github.com/yhara/simple_twitter)",
123
+ },
124
+ }
125
+ args[:json] = json unless json.empty?
126
+ args[:form] = form unless form.empty?
127
+ args
128
+ end
129
+
107
130
  # @param method [Symbol]
108
131
  # @param url [String]
109
132
  # @param params [Hash<Symbol, String>]
@@ -1,3 +1,3 @@
1
1
  module SimpleTwitter
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require 'http'
2
2
  require 'simple_oauth'
3
3
 
4
+ require_relative "simple_twitter/version"
5
+
4
6
  module SimpleTwitter
5
7
  autoload :Client, "simple_twitter/client"
6
8
  autoload :ClientError, "simple_twitter/client_error"
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_dependency "http", ">= 4"
31
31
  spec.add_dependency "simple_oauth", ">= 0.3.1"
32
32
 
33
+ spec.add_development_dependency "dotenv"
33
34
  spec.add_development_dependency "rspec"
34
35
  spec.add_development_dependency "rspec-its"
35
36
  spec.add_development_dependency "webmock"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_twitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yutaka HARA
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-07-17 00:00:00.000000000 Z
12
+ date: 2023-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: http
@@ -39,6 +39,20 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: 0.3.1
42
+ - !ruby/object:Gem::Dependency
43
+ name: dotenv
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: rspec
44
58
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +117,7 @@ executables: []
103
117
  extensions: []
104
118
  extra_rdoc_files: []
105
119
  files:
120
+ - ".env.example"
106
121
  - ".github/dependabot.yml"
107
122
  - ".github/workflows/pages.yml"
108
123
  - ".github/workflows/test.yml"