simple_twitter 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/pages.yml +51 -0
- data/.github/workflows/test.yml +48 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +114 -0
- data/README.md +47 -4
- data/lib/simple_twitter/client.rb +142 -0
- data/lib/simple_twitter/client_error.rb +5 -0
- data/lib/simple_twitter/error.rb +29 -0
- data/lib/simple_twitter/server_error.rb +5 -0
- data/lib/simple_twitter/version.rb +1 -1
- data/lib/simple_twitter.rb +4 -54
- data/simple_twitter.gemspec +12 -5
- metadata +77 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6658a0b3e20a5da4a1a59d28ee5062bb2f1e816e6298cf49dcc48d0cfda2bcbb
|
4
|
+
data.tar.gz: 7295ee6a0d9ecd2ea5fe264bfcbe4357d8e2cde9b40db0001f0ebb39e7479399
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa8c402510a617b96a6dd6c239c7d4d9b0c9e01fc7202f371ba826b095c01542ba2ee76ef11d953b7aa76e3c8099597cf3d0bd2da7fa264c98214c87ad8b1e96
|
7
|
+
data.tar.gz: 02eead6edb730eb7562dc7869ca15d8908b5424526a902afc68912f69a1aa847c32531d57291802237854dc84af3681a931474362e0704eec31740057c59f55d
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Simple workflow for deploying static content to GitHub Pages
|
2
|
+
name: Deploy static content to Pages
|
3
|
+
|
4
|
+
on:
|
5
|
+
# Runs on pushes targeting the default branch
|
6
|
+
push:
|
7
|
+
branches:
|
8
|
+
- main
|
9
|
+
|
10
|
+
# Allows you to run this workflow manually from the Actions tab
|
11
|
+
workflow_dispatch:
|
12
|
+
|
13
|
+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
|
14
|
+
permissions:
|
15
|
+
contents: read
|
16
|
+
pages: write
|
17
|
+
id-token: write
|
18
|
+
|
19
|
+
# Allow one concurrent deployment
|
20
|
+
concurrency:
|
21
|
+
group: "pages"
|
22
|
+
cancel-in-progress: true
|
23
|
+
|
24
|
+
jobs:
|
25
|
+
# Single deploy job since we're just deploying
|
26
|
+
deploy:
|
27
|
+
environment:
|
28
|
+
name: github-pages
|
29
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
30
|
+
runs-on: ubuntu-latest
|
31
|
+
steps:
|
32
|
+
- name: Checkout
|
33
|
+
uses: actions/checkout@v3
|
34
|
+
|
35
|
+
- uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ruby
|
38
|
+
bundler-cache: true
|
39
|
+
|
40
|
+
- run: bundle exec yard
|
41
|
+
|
42
|
+
- name: Setup Pages
|
43
|
+
uses: actions/configure-pages@v3
|
44
|
+
- name: Upload artifact
|
45
|
+
uses: actions/upload-pages-artifact@v2
|
46
|
+
with:
|
47
|
+
# Upload entire repository
|
48
|
+
path: './doc'
|
49
|
+
- name: Deploy to GitHub Pages
|
50
|
+
id: deployment
|
51
|
+
uses: actions/deploy-pages@main
|
@@ -0,0 +1,48 @@
|
|
1
|
+
name: test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
pull_request:
|
8
|
+
types:
|
9
|
+
- opened
|
10
|
+
- synchronize
|
11
|
+
- reopened
|
12
|
+
schedule:
|
13
|
+
- cron: "0 10 * * 5" # JST 19:00 (Fri)
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
test:
|
17
|
+
runs-on: ubuntu-latest
|
18
|
+
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
|
22
|
+
matrix:
|
23
|
+
ruby:
|
24
|
+
- "2.3"
|
25
|
+
- "2.4"
|
26
|
+
- "2.5"
|
27
|
+
- "2.6"
|
28
|
+
- "2.7"
|
29
|
+
- "3.0"
|
30
|
+
- "3.1"
|
31
|
+
- "3.2"
|
32
|
+
|
33
|
+
steps:
|
34
|
+
- uses: actions/checkout@v3
|
35
|
+
|
36
|
+
- uses: ruby/setup-ruby@v1
|
37
|
+
with:
|
38
|
+
ruby-version: ${{ matrix.ruby }}
|
39
|
+
bundler-cache: true
|
40
|
+
|
41
|
+
- name: bundle update
|
42
|
+
run: |
|
43
|
+
set -xe
|
44
|
+
bundle config path vendor/bundle
|
45
|
+
bundle update --jobs $(nproc) --retry 3
|
46
|
+
|
47
|
+
- run: bundle exec rspec
|
48
|
+
- run: bundle exec yard --fail-on-warning
|
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,117 @@
|
|
1
|
+
## Unreleased
|
2
|
+
https://github.com/yhara/simple_twitter/compare/v2.0.0...main
|
3
|
+
|
4
|
+
## v2.0.0 (2023-07-17)
|
5
|
+
https://github.com/yhara/simple_twitter/compare/v1.0.0...v2.0.0
|
6
|
+
|
7
|
+
### :bomb: [BREAKING CHANGE] positional args to keywords args
|
8
|
+
Since v2.0, the positional arguments of the following methods are now keyword arguments.
|
9
|
+
|
10
|
+
| v1.0 | v2.0+ |
|
11
|
+
| ---------------------------------------------------- | ------------------------------------------------------------- |
|
12
|
+
| `SimpleTwitter::Client#get(url, params = {})` | `SimpleTwitter::Client#get(url, params: {}, json: {})` |
|
13
|
+
| `SimpleTwitter::Client#get_raw(url, params = {})` | `SimpleTwitter::Client#get_raw(url, params: {}, json: {})` |
|
14
|
+
| `SimpleTwitter::Client#post(url, params = {})` | `SimpleTwitter::Client#post(url, params: {}, json: {})` |
|
15
|
+
| `SimpleTwitter::Client#post_raw(url, params = {})` | `SimpleTwitter::Client#post_raw(url, params: {}, json: {})` |
|
16
|
+
| `SimpleTwitter::Client#put(url, params = {})` | `SimpleTwitter::Client#put(url, params: {}, json: {})` |
|
17
|
+
| `SimpleTwitter::Client#put_raw(url, params = {})` | `SimpleTwitter::Client#put_raw(url, params: {}, json: {})` |
|
18
|
+
| `SimpleTwitter::Client#delete(url, params = {})` | `SimpleTwitter::Client#delete(url, params: {}, json: {})` |
|
19
|
+
| `SimpleTwitter::Client#delete_raw(url, params = {})` | `SimpleTwitter::Client#delete_raw(url, params: {}, json: {})` |
|
20
|
+
|
21
|
+
Please modify as follows when you upgrade from v1.0 :pray:
|
22
|
+
|
23
|
+
Before (v1.0)
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
client.get("https://api.twitter.com/2/tweets", ids: "1302127884039909376,1369885448319889409")
|
27
|
+
```
|
28
|
+
|
29
|
+
After (v2.0+)
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
client.get("https://api.twitter.com/2/tweets", params: { ids: "1302127884039909376,1369885448319889409" })
|
33
|
+
```
|
34
|
+
|
35
|
+
If only 1 argument was used (e.g. `client.get("https://api.twitter.com/2/users/me")`), no modification is required.
|
36
|
+
|
37
|
+
See more.
|
38
|
+
|
39
|
+
* https://github.com/yhara/simple_twitter/pull/3
|
40
|
+
* https://github.com/yhara/simple_twitter/pull/19
|
41
|
+
|
42
|
+
### :bomb: [BREAKING CHANGE] raise Error when Twitter API returned error
|
43
|
+
Until v1.0, even if the Twitter API returns an error, Ruby does not throw an error, so it is considered a normal exit.
|
44
|
+
|
45
|
+
This is confusing, so when the API returns an error, Ruby also raises an error.
|
46
|
+
|
47
|
+
Before (v1.0)
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
client = SimpleTwitter::Client.new(bearer_token: "dummy")
|
51
|
+
client.get("https://api.twitter.com/2/users/me")
|
52
|
+
#=>
|
53
|
+
{:title=>"Unsupported Authentication",
|
54
|
+
:detail=>
|
55
|
+
"Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].",
|
56
|
+
:type=>"https://api.twitter.com/2/problems/unsupported-authentication",
|
57
|
+
:status=>403}
|
58
|
+
```
|
59
|
+
|
60
|
+
After (v2.0+)
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
client = SimpleTwitter::Client.new(bearer_token: "dummy")
|
64
|
+
client.get("https://api.twitter.com/2/users/me")
|
65
|
+
/path/to/simple_twitter/lib/simple_twitter.rb:96:in `parse_response': Unsupported Authentication (status 403) (SimpleTwitter::ClientError)
|
66
|
+
from (eval):6:in `get'
|
67
|
+
from (irb):2:in `<main>'
|
68
|
+
from ./bin/console:15:in `<main>'
|
69
|
+
```
|
70
|
+
|
71
|
+
Detailed error details can be obtained as follows
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
begin
|
75
|
+
client = SimpleTwitter::Client.new(bearer_token: "invalid_bearer_token")
|
76
|
+
client.get("https://api.twitter.com/2/users/me")
|
77
|
+
rescue SimpleTwitter::Error => error
|
78
|
+
error.raw_response.class
|
79
|
+
#=> HTTP::Response
|
80
|
+
|
81
|
+
error.raw_response.code
|
82
|
+
#=> 403
|
83
|
+
|
84
|
+
error.body[:title]
|
85
|
+
# => "Unsupported Authentication"
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
See more.
|
90
|
+
|
91
|
+
* https://github.com/yhara/simple_twitter/pull/2
|
92
|
+
* https://github.com/yhara/simple_twitter/pull/15
|
93
|
+
|
94
|
+
### Features
|
95
|
+
- Enabled rubygems_mfa_required
|
96
|
+
- https://github.com/yhara/simple_twitter/pull/9
|
97
|
+
- Add changelog_uri to gemspec
|
98
|
+
- https://github.com/yhara/simple_twitter/pull/10
|
99
|
+
- Write YARD comment and publish to Pages
|
100
|
+
- https://github.com/yhara/simple_twitter/pull/13
|
101
|
+
- Add documentation_uri to gemspec metadata
|
102
|
+
- https://github.com/yhara/simple_twitter/pull/16
|
103
|
+
|
104
|
+
### Others
|
105
|
+
- Add @sue445 as an author
|
106
|
+
- https://github.com/yhara/simple_twitter/pull/6
|
107
|
+
- Add tests and CI
|
108
|
+
- https://github.com/yhara/simple_twitter/pull/7
|
109
|
+
- https://github.com/yhara/simple_twitter/pull/12
|
110
|
+
- Tweak doc
|
111
|
+
- https://github.com/yhara/simple_twitter/pull/17
|
112
|
+
- https://github.com/yhara/simple_twitter/pull/18
|
113
|
+
- https://github.com/yhara/simple_twitter/pull/20
|
114
|
+
|
1
115
|
## v1.0.0 (2021-03-28)
|
2
116
|
|
3
117
|
- initial release
|
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
Dead simple Twitter API client. Supports both v1 and v2
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/simple_twitter.svg)](https://badge.fury.io/rb/simple_twitter)
|
6
|
+
[![test](https://github.com/yhara/simple_twitter/actions/workflows/test.yml/badge.svg)](https://github.com/yhara/simple_twitter/actions/workflows/test.yml)
|
7
|
+
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
@@ -25,7 +28,7 @@ require 'simple_twitter'
|
|
25
28
|
|
26
29
|
client = SimpleTwitter::Client.new(bearer_token: "...")
|
27
30
|
pp client.get("https://api.twitter.com/2/tweets",
|
28
|
-
ids: "1302127884039909376,1369885448319889409")
|
31
|
+
params: { ids: "1302127884039909376,1369885448319889409" })
|
29
32
|
```
|
30
33
|
|
31
34
|
Result:
|
@@ -55,11 +58,25 @@ client = SimpleTwitter::Client.new(
|
|
55
58
|
access_token_secret: config[:access_token_secret],
|
56
59
|
)
|
57
60
|
pp client.post("https://api.twitter.com/1.1/statuses/update.json",
|
58
|
-
status: "Test.")
|
61
|
+
params: { status: "Test." })
|
59
62
|
```
|
60
63
|
|
61
64
|
You can get the access_token and access_token_secret for your own at the Twitter Developer Portal. For other users, you need to get them via OAuth (out of scope of this gem.)
|
62
65
|
|
66
|
+
### Post with JSON body
|
67
|
+
Since Twitter API v2, POST must be sent as JSON body
|
68
|
+
|
69
|
+
e.g.https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets
|
70
|
+
|
71
|
+
Send using the `json` argument.
|
72
|
+
|
73
|
+
e.g.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
client = SimpleTwitter::Client.new(bearer_token: ENV["ACCESS_TOKEN"])
|
77
|
+
client.post("https://api.twitter.com/2/tweets", json: { text: "Hello twitter!" })
|
78
|
+
```
|
79
|
+
|
63
80
|
### Advanced
|
64
81
|
|
65
82
|
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).
|
@@ -77,16 +94,42 @@ end
|
|
77
94
|
Some API parameters has `.` in its name (eg. `tweet.fields`.) Did you know that in Ruby you can include `.` in a hash key if quoted? :-)
|
78
95
|
|
79
96
|
```rb
|
80
|
-
tweets = @client.get("https://api.twitter.com/2/users/#{id}/tweets", {
|
97
|
+
tweets = @client.get("https://api.twitter.com/2/users/#{id}/tweets", params: {
|
81
98
|
expansions: "author_id",
|
82
99
|
max_results: 100,
|
83
100
|
"tweet.fields": "author_id,created_at,referenced_tweets,text",
|
84
101
|
})
|
85
102
|
```
|
86
103
|
|
104
|
+
### Detailed error details
|
105
|
+
If an error is returned from the API, you may need more information.
|
106
|
+
|
107
|
+
In such cases, you can retrieve it as follows.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
begin
|
111
|
+
client = SimpleTwitter::Client.new(bearer_token: "invalid_bearer_token")
|
112
|
+
client.get("https://api.twitter.com/2/users/me")
|
113
|
+
rescue SimpleTwitter::Error => error
|
114
|
+
error.raw_response.class
|
115
|
+
#=> HTTP::Response
|
116
|
+
|
117
|
+
error.raw_response.code
|
118
|
+
#=> 403
|
119
|
+
|
120
|
+
error.body[:title]
|
121
|
+
# => "Unsupported Authentication"
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
See more. [SimpleTwitter::Error](https://yhara.github.io/simple_twitter/SimpleTwitter/Error.html)
|
126
|
+
|
127
|
+
## API Reference
|
128
|
+
See https://yhara.github.io/simple_twitter/
|
129
|
+
|
87
130
|
## Contributing
|
88
131
|
|
89
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yhara/simple_twitter.
|
90
133
|
|
91
134
|
## License
|
92
135
|
|
@@ -0,0 +1,142 @@
|
|
1
|
+
module SimpleTwitter
|
2
|
+
# Twitter API Client
|
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
|
9
|
+
def initialize(bearer_token: nil,
|
10
|
+
api_key: nil,
|
11
|
+
api_secret_key: nil,
|
12
|
+
access_token: nil,
|
13
|
+
access_token_secret: nil)
|
14
|
+
if bearer_token
|
15
|
+
@bearer_token = bearer_token
|
16
|
+
else
|
17
|
+
@oauth_params = {
|
18
|
+
consumer_key: api_key,
|
19
|
+
consumer_secret: api_secret_key,
|
20
|
+
token: access_token,
|
21
|
+
token_secret: access_token_secret,
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# @!method get(url, params: {}, json: {})
|
27
|
+
# Call Twitter API with GET method
|
28
|
+
# @param url [String]
|
29
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
30
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
31
|
+
# @return [Hash] parsed json data
|
32
|
+
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
|
33
|
+
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
|
34
|
+
|
35
|
+
# @!method get_raw(url, params: {}, json: {})
|
36
|
+
# Call Twitter API with GET method
|
37
|
+
# @param url [String]
|
38
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
39
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
40
|
+
# @return [HTTP::Response]
|
41
|
+
|
42
|
+
# @!method post(url, params: {}, json: {})
|
43
|
+
# Call Twitter API with POST method
|
44
|
+
# @param url [String]
|
45
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
46
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
47
|
+
# @return [Hash] parsed json data
|
48
|
+
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
|
49
|
+
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
|
50
|
+
|
51
|
+
# @!method post_raw(url, params: {}, json: {})
|
52
|
+
# Call Twitter API with POST method
|
53
|
+
# @param url [String]
|
54
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
55
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
56
|
+
# @return [HTTP::Response]
|
57
|
+
|
58
|
+
# @!method put(url, params: {}, json: {})
|
59
|
+
# Call Twitter API with PUT method
|
60
|
+
# @param url [String]
|
61
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
62
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
63
|
+
# @return [Hash] parsed json data
|
64
|
+
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
|
65
|
+
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
|
66
|
+
|
67
|
+
# @!method put_raw(url, params: {}, json: {})
|
68
|
+
# Call Twitter API with PUT method
|
69
|
+
# @param url [String]
|
70
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
71
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
72
|
+
# @return [HTTP::Response]
|
73
|
+
|
74
|
+
# @!method delete(url, params: {}, json: {})
|
75
|
+
# Call Twitter API with DELETE method
|
76
|
+
# @param url [String]
|
77
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
78
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
79
|
+
# @return [Hash] parsed json data
|
80
|
+
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
|
81
|
+
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
|
82
|
+
|
83
|
+
# @!method delete_raw(url, params: {}, json: {})
|
84
|
+
# Call Twitter API with DELETE method
|
85
|
+
# @param url [String]
|
86
|
+
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
|
87
|
+
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
|
88
|
+
# @return [HTTP::Response]
|
89
|
+
|
90
|
+
%i[get post put delete].each do |m|
|
91
|
+
class_eval <<~EOD
|
92
|
+
def #{m}(url, params: {}, json: {})
|
93
|
+
res = #{m}_raw(url, params: params, json: json)
|
94
|
+
parse_response(res)
|
95
|
+
end
|
96
|
+
|
97
|
+
def #{m}_raw(url, params: {}, json: {})
|
98
|
+
args = { params: params }
|
99
|
+
args[:json] = json unless json.empty?
|
100
|
+
http(:#{m}, url, params).#{m}(url, args)
|
101
|
+
end
|
102
|
+
EOD
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
# @param method [Symbol]
|
108
|
+
# @param url [String]
|
109
|
+
# @param params [Hash<Symbol, String>]
|
110
|
+
# @return [HTTP::Client]
|
111
|
+
def http(method, url, params)
|
112
|
+
HTTP.auth(auth_header(method, url, params))
|
113
|
+
end
|
114
|
+
|
115
|
+
# @param method [Symbol]
|
116
|
+
# @param url [String]
|
117
|
+
# @param params [Hash<Symbol, String>]
|
118
|
+
# @return [String]
|
119
|
+
def auth_header(method, url, params)
|
120
|
+
if @bearer_token
|
121
|
+
"Bearer #{@bearer_token}"
|
122
|
+
else
|
123
|
+
SimpleOAuth::Header.new(method, url, params, @oauth_params).to_s
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# @param res [HTTP::Response]
|
128
|
+
# @return [Hash] parsed json data
|
129
|
+
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
|
130
|
+
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
|
131
|
+
def parse_response(res)
|
132
|
+
case res.code.to_i / 100
|
133
|
+
when 4
|
134
|
+
raise ClientError, res
|
135
|
+
when 5
|
136
|
+
raise ServerError, res
|
137
|
+
end
|
138
|
+
|
139
|
+
JSON.parse(res.to_s, symbolize_names: true)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module SimpleTwitter
|
2
|
+
# Error base class
|
3
|
+
class Error < StandardError
|
4
|
+
# @!attribute [r] raw_response
|
5
|
+
# @return [HTTP::Response] raw error response
|
6
|
+
attr_reader :raw_response
|
7
|
+
|
8
|
+
# @!attribute [r] body
|
9
|
+
# @return [Hash<Symbol, String>] error response body
|
10
|
+
attr_reader :body
|
11
|
+
|
12
|
+
# @param raw_response [HTTP::Response] raw error response from Twitter API
|
13
|
+
def initialize(raw_response)
|
14
|
+
@raw_response = raw_response
|
15
|
+
|
16
|
+
begin
|
17
|
+
@body = JSON.parse(raw_response.to_s, symbolize_names: true)
|
18
|
+
|
19
|
+
title = @body[:title] || "Unknown error"
|
20
|
+
title << " (status #{raw_response.code})"
|
21
|
+
|
22
|
+
super(title)
|
23
|
+
rescue JSON::ParserError => e
|
24
|
+
# Twitter doesn't returns json
|
25
|
+
super("Unknown error (status #{raw_response.code})")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/simple_twitter.rb
CHANGED
@@ -2,58 +2,8 @@ require 'http'
|
|
2
2
|
require 'simple_oauth'
|
3
3
|
|
4
4
|
module SimpleTwitter
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
access_token: nil,
|
10
|
-
access_token_secret: nil)
|
11
|
-
if bearer_token
|
12
|
-
@bearer_token = bearer_token
|
13
|
-
else
|
14
|
-
@oauth_params = {
|
15
|
-
consumer_key: api_key,
|
16
|
-
consumer_secret: api_secret_key,
|
17
|
-
token: access_token,
|
18
|
-
token_secret: access_token_secret,
|
19
|
-
}
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
%i[get post put delete].each do |m|
|
24
|
-
class_eval <<~EOD
|
25
|
-
# @return [Object] parsed json data
|
26
|
-
def #{m}(url, params={})
|
27
|
-
JSON.parse(#{m}_raw(url, params).to_s, symbolize_names: true)
|
28
|
-
end
|
29
|
-
|
30
|
-
# @return [HTTP::Response]
|
31
|
-
def #{m}_raw(url, params={})
|
32
|
-
http(:#{m}, url, params).#{m}(url, params: params)
|
33
|
-
end
|
34
|
-
EOD
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
# @param method [Symbol]
|
40
|
-
# @param url [String]
|
41
|
-
# @param params [Hash<Symbol, String>]
|
42
|
-
# @return [HTTP::Request]
|
43
|
-
def http(method, url, params)
|
44
|
-
HTTP.auth(auth_header(method, url, params))
|
45
|
-
end
|
46
|
-
|
47
|
-
# @param method [Symbol]
|
48
|
-
# @param url [String]
|
49
|
-
# @param params [Hash<Symbol, String>]
|
50
|
-
# @return [String]
|
51
|
-
def auth_header(method, url, params)
|
52
|
-
if @bearer_token
|
53
|
-
"Bearer #{@bearer_token}"
|
54
|
-
else
|
55
|
-
SimpleOAuth::Header.new(method, url, params, @oauth_params).to_s
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
5
|
+
autoload :Client, "simple_twitter/client"
|
6
|
+
autoload :ClientError, "simple_twitter/client_error"
|
7
|
+
autoload :Error, "simple_twitter/error"
|
8
|
+
autoload :ServerError, "simple_twitter/server_error"
|
59
9
|
end
|
data/simple_twitter.gemspec
CHANGED
@@ -3,8 +3,8 @@ require_relative "lib/simple_twitter/version"
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "simple_twitter"
|
5
5
|
spec.version = SimpleTwitter::VERSION
|
6
|
-
spec.authors = ["Yutaka HARA"]
|
7
|
-
spec.email = ["yutaka.hara+github@gmail.com"]
|
6
|
+
spec.authors = ["Yutaka HARA", "sue445"]
|
7
|
+
spec.email = ["yutaka.hara+github@gmail.com", "sue445fukuoka@gmail.com"]
|
8
8
|
|
9
9
|
spec.summary = "Dead simple client for Twitter API v1/v2"
|
10
10
|
spec.description = "Dead simple client for Twitter API (supports both v1 and v2)"
|
@@ -14,7 +14,9 @@ Gem::Specification.new do |spec|
|
|
14
14
|
|
15
15
|
spec.metadata["homepage_uri"] = spec.homepage
|
16
16
|
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
-
|
17
|
+
spec.metadata["changelog_uri"] = spec.homepage + "/blob/main/CHANGELOG.md"
|
18
|
+
spec.metadata["documentation_uri"] = "https://yhara.github.io/simple_twitter/"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
18
20
|
|
19
21
|
# Specify which files should be added to the gem when it is released.
|
20
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -25,6 +27,11 @@ Gem::Specification.new do |spec|
|
|
25
27
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
26
28
|
spec.require_paths = ["lib"]
|
27
29
|
|
28
|
-
spec.add_dependency "http", "
|
29
|
-
spec.add_dependency "simple_oauth", "
|
30
|
+
spec.add_dependency "http", ">= 4"
|
31
|
+
spec.add_dependency "simple_oauth", ">= 0.3.1"
|
32
|
+
|
33
|
+
spec.add_development_dependency "rspec"
|
34
|
+
spec.add_development_dependency "rspec-its"
|
35
|
+
spec.add_development_dependency "webmock"
|
36
|
+
spec.add_development_dependency "yard"
|
30
37
|
end
|
metadata
CHANGED
@@ -1,51 +1,114 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_twitter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yutaka HARA
|
8
|
+
- sue445
|
8
9
|
autorequire:
|
9
10
|
bindir: exe
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2023-07-17 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: http
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "
|
18
|
+
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
20
|
version: '4'
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
|
-
- - "
|
25
|
+
- - ">="
|
25
26
|
- !ruby/object:Gem::Version
|
26
27
|
version: '4'
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
29
|
name: simple_oauth
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- - "
|
32
|
+
- - ">="
|
32
33
|
- !ruby/object:Gem::Version
|
33
34
|
version: 0.3.1
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
|
-
- - "
|
39
|
+
- - ">="
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: 0.3.1
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
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'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec-its
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: webmock
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: yard
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
41
98
|
description: Dead simple client for Twitter API (supports both v1 and v2)
|
42
99
|
email:
|
43
100
|
- yutaka.hara+github@gmail.com
|
101
|
+
- sue445fukuoka@gmail.com
|
44
102
|
executables: []
|
45
103
|
extensions: []
|
46
104
|
extra_rdoc_files: []
|
47
105
|
files:
|
106
|
+
- ".github/dependabot.yml"
|
107
|
+
- ".github/workflows/pages.yml"
|
108
|
+
- ".github/workflows/test.yml"
|
48
109
|
- ".gitignore"
|
110
|
+
- ".rspec"
|
111
|
+
- ".yardopts"
|
49
112
|
- CHANGELOG.md
|
50
113
|
- Gemfile
|
51
114
|
- LICENSE.txt
|
@@ -54,6 +117,10 @@ files:
|
|
54
117
|
- bin/console
|
55
118
|
- bin/setup
|
56
119
|
- lib/simple_twitter.rb
|
120
|
+
- lib/simple_twitter/client.rb
|
121
|
+
- lib/simple_twitter/client_error.rb
|
122
|
+
- lib/simple_twitter/error.rb
|
123
|
+
- lib/simple_twitter/server_error.rb
|
57
124
|
- lib/simple_twitter/version.rb
|
58
125
|
- simple_twitter.gemspec
|
59
126
|
homepage: https://github.com/yhara/simple_twitter
|
@@ -62,6 +129,9 @@ licenses:
|
|
62
129
|
metadata:
|
63
130
|
homepage_uri: https://github.com/yhara/simple_twitter
|
64
131
|
source_code_uri: https://github.com/yhara/simple_twitter
|
132
|
+
changelog_uri: https://github.com/yhara/simple_twitter/blob/main/CHANGELOG.md
|
133
|
+
documentation_uri: https://yhara.github.io/simple_twitter/
|
134
|
+
rubygems_mfa_required: 'true'
|
65
135
|
post_install_message:
|
66
136
|
rdoc_options: []
|
67
137
|
require_paths:
|
@@ -77,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
147
|
- !ruby/object:Gem::Version
|
78
148
|
version: '0'
|
79
149
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
150
|
+
rubygems_version: 3.4.10
|
81
151
|
signing_key:
|
82
152
|
specification_version: 4
|
83
153
|
summary: Dead simple client for Twitter API v1/v2
|