twitter_labs_api 0.5.0 → 0.7.1
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 +4 -4
- data/CHANGELOG.md +38 -0
- data/Gemfile.lock +2 -2
- data/README.md +13 -8
- data/lib/twitter_labs_api/client.rb +16 -4
- data/lib/twitter_labs_api/resources/tweet.rb +39 -11
- data/lib/twitter_labs_api/resources/user.rb +24 -17
- data/lib/twitter_labs_api/services/params_service.rb +15 -0
- data/lib/twitter_labs_api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5ce6092d6dc4256bc5db207476e6b5d74c822340b2a65287c6642d80ccd011ba
|
|
4
|
+
data.tar.gz: 6182b14e47a3bf227c12031cc674f115c178ae625e8278f192bf67dc5adc781e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef6bf629eae0a91baa59eb43beb7d0770bca8e75243ca04d3884839987f3e466f25b6c763b4046b57d20cc07cb1e5605dd7febb935ddb5333f4ebf74bb2101b7
|
|
7
|
+
data.tar.gz: 84eb57aec3cde7b0b4e8e30f507016e1e97930c810f0777018593841507430b5a871fb3da6ba140ecf2c57462da6101f304deb0a4ab621efa61e4839aefcc2e3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
+
# 0.7.1 - 28 August 2020
|
|
2
|
+
|
|
3
|
+
- Fix: add missing require statement
|
|
4
|
+
|
|
5
|
+
# 0.7.0 - 28 August 2020
|
|
6
|
+
|
|
7
|
+
- Refactor interface for requesting custom response fields
|
|
8
|
+
|
|
9
|
+
## Breaking Change
|
|
10
|
+
|
|
11
|
+
Heads up, the argument to request fields was previously called `tweet_fields`; it is now called `fields`.
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
api.get_tweet(id: '123455683780', fields: requested_fields)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The `fields` parameter now supports multiple different data types (instead of just Tweet attributes). So for example, to request the URL of an embedded media item:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
requested_fields = { tweet: %w[id username], media: %w[url] }
|
|
21
|
+
|
|
22
|
+
api.get_tweet(id: my_id, fields: requested_fields)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
# 0.6.0 - 28 July 2020
|
|
26
|
+
|
|
27
|
+
- Fix: broken dependency
|
|
28
|
+
|
|
29
|
+
# 0.5.2 - 28 July 2020
|
|
30
|
+
|
|
31
|
+
- Add `search`
|
|
32
|
+
- Finish `rdoc` coverage
|
|
33
|
+
|
|
34
|
+
# 0.5.1 - 28 July 2020
|
|
35
|
+
|
|
36
|
+
- Add `User-Agent` request header
|
|
37
|
+
- Add `hide_reply`
|
|
38
|
+
|
|
1
39
|
# 0.5.0 - 27 July 2020
|
|
2
40
|
|
|
3
41
|
- Refactor structure to prepare for adding additional API endpoints
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
twitter_labs_api (0.
|
|
4
|
+
twitter_labs_api (0.7.1)
|
|
5
5
|
activesupport (~> 6.0)
|
|
6
6
|
httplog (~> 1.4)
|
|
7
7
|
|
|
@@ -25,7 +25,7 @@ GEM
|
|
|
25
25
|
cd (1.0.1)
|
|
26
26
|
clipboard (1.0.6)
|
|
27
27
|
coderay (1.1.3)
|
|
28
|
-
concurrent-ruby (1.1.
|
|
28
|
+
concurrent-ruby (1.1.7)
|
|
29
29
|
crack (0.4.3)
|
|
30
30
|
safe_yaml (~> 1.0.0)
|
|
31
31
|
debugging (1.1.1)
|
data/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# twitter-labs-api
|
|
2
2
|
|
|
3
|
+
[](https://developer.twitter.com/en/docs/labs/overview/versioning)
|
|
4
|
+
|
|
3
5
|
A basic implementation of a Twitter Labs API client as a handy Ruby [gem](https://rubygems.org/gems/twitter_labs_api). This project uses the v2 endpoints announced [here](https://twittercommunity.com/t/releasing-a-new-version-of-labs-endpoints/134219/3).
|
|
4
6
|
|
|
5
7
|
## Usage
|
|
6
8
|
|
|
7
9
|
### Prerequisite
|
|
8
|
-
All one needs is a Twitter [bearer token](https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0/bearer-tokens) to get started.
|
|
10
|
+
All one needs is a Twitter [bearer token](https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0/bearer-tokens) to get started. The bearer token is available on the 'Tokens and Keys' page within your app's dashboard on the [Twitter for Developers](https://developer.twitter.com/) site.
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
Alternatively, one can get a bearer token using [this method](https://www.rubydoc.info/gems/twitter/Twitter/REST/Client#bearer_token%3F-instance_method) from https://github.com/sferik/twitter.
|
|
11
13
|
|
|
12
14
|
### Setup
|
|
13
15
|
|
|
@@ -30,14 +32,13 @@ api.get_tweet(id: '1234671272602193920')
|
|
|
30
32
|
|
|
31
33
|
#### Specifying which fields to include in the response
|
|
32
34
|
|
|
33
|
-
By default, the gem requests the 'default' fields for each entity. See the [API Reference](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference) for available fields. One can customize the response payload
|
|
35
|
+
By default, the gem requests the 'default' fields for each entity. See the [API Reference](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference) for available fields. One can customize the response payload, depending on the requested resource.
|
|
34
36
|
|
|
37
|
+
For example, to request the URL of a Tweet's embedded media item:
|
|
35
38
|
```ruby
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
api.get_tweet(id: '1235508591232090112', tweet_fields: my_fields)
|
|
39
|
+
requested_fields = { tweet: %w[id username], media: %w[url] }
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
api.get_tweet(id: my_id, fields: requested_fields)
|
|
41
42
|
```
|
|
42
43
|
|
|
43
44
|
#### API Errors
|
|
@@ -62,6 +63,8 @@ Currently, the following endpoints are implemented:
|
|
|
62
63
|
|
|
63
64
|
- `TwitterLabsAPI#get_tweet` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-tweets-id)) - Retrieve a single Tweet object with an `id`
|
|
64
65
|
- `TwitterLabsAPI#get_tweets` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-tweets)) - Retrieve multiple Tweets with a collection of `ids`
|
|
66
|
+
- `TwitterLabsAPI#hide_reply` ([docs](https://developer.twitter.com/en/docs/labs/hide-replies/api-reference/put-hidden)) - Hide a reply by referencing it's `id`; must be in a conversation belonging to the authenticating user
|
|
67
|
+
- `TwitterLabsAPI#search` ([docs](https://developer.twitter.com/en/docs/labs/recent-search/api-reference/get-recent-search)) - Returns Tweets from the last 7 days that match a search query.
|
|
65
68
|
|
|
66
69
|
#### Users
|
|
67
70
|
|
|
@@ -71,7 +74,9 @@ Currently, the following endpoints are implemented:
|
|
|
71
74
|
|
|
72
75
|
## Roadmap
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
Currently focused on implementing support for all v2 endpoints; if there is enough interest, I will add v1 endpoint support as well.
|
|
78
|
+
|
|
79
|
+
And of course, contributions are welcome :)
|
|
75
80
|
|
|
76
81
|
## Dependencies
|
|
77
82
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
require_relative 'version'
|
|
1
2
|
require_relative 'api_error'
|
|
3
|
+
require_relative 'services/params_service'
|
|
2
4
|
require_relative 'resources/tweet'
|
|
3
5
|
require_relative 'resources/user'
|
|
4
6
|
|
|
@@ -17,13 +19,14 @@ module TwitterLabsAPI
|
|
|
17
19
|
|
|
18
20
|
private
|
|
19
21
|
|
|
20
|
-
def make_request(url:, params: {}, is_collection: false)
|
|
22
|
+
def make_request(url:, params: {}, is_collection: false, method: :get)
|
|
21
23
|
uri = URI.parse(url)
|
|
22
24
|
uri.query = URI.encode_www_form(params)
|
|
23
|
-
request =
|
|
25
|
+
request = http_adapter(method).new(uri)
|
|
24
26
|
request['Authorization'] = "Bearer #{bearer_token}"
|
|
27
|
+
request['User-Agent'] = "twitter_labs_api gem #{TwitterLabsAPI::VERSION}"
|
|
25
28
|
req_options = { use_ssl: uri.scheme == 'https' }
|
|
26
|
-
|
|
29
|
+
|
|
27
30
|
self.api_response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
|
28
31
|
http.request(request)
|
|
29
32
|
end
|
|
@@ -36,7 +39,16 @@ module TwitterLabsAPI
|
|
|
36
39
|
|
|
37
40
|
is_collection ? handle_collection : handle_single
|
|
38
41
|
end
|
|
39
|
-
|
|
42
|
+
|
|
43
|
+
def http_adapter(method)
|
|
44
|
+
case method
|
|
45
|
+
when :put
|
|
46
|
+
Net::HTTP::Put
|
|
47
|
+
else
|
|
48
|
+
Net::HTTP::Get
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
40
52
|
def error_response?
|
|
41
53
|
parsed_response.key?('errors')
|
|
42
54
|
end
|
|
@@ -2,26 +2,54 @@ module TwitterLabsAPI
|
|
|
2
2
|
module Resources
|
|
3
3
|
module Tweet
|
|
4
4
|
DEFAULT_TWEET_FIELDS = %w[id author_id created_at lang public_metrics].freeze
|
|
5
|
+
DEFAULT_FIELDS = { tweet: DEFAULT_TWEET_FIELDS }.freeze
|
|
5
6
|
|
|
7
|
+
# Returns a variety of information about a single Tweet specified by the requested ID.
|
|
6
8
|
# @param [String] :id the ID of the requested Tweet
|
|
7
|
-
# @param [
|
|
8
|
-
|
|
9
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
10
|
+
#
|
|
11
|
+
# e.g.: { tweet: %w[id username], media: %w[url] }
|
|
12
|
+
# @return Hash an object with requested tweet fields
|
|
13
|
+
def get_tweet(id:, fields: DEFAULT_FIELDS)
|
|
9
14
|
url = "https://api.twitter.com/labs/2/tweets/#{id}"
|
|
10
|
-
params =
|
|
11
|
-
'tweet.fields' => tweet_fields.join(',')
|
|
12
|
-
}.compact
|
|
15
|
+
params = ParamsService.from_fields(fields)
|
|
13
16
|
|
|
14
17
|
make_request(url: url, params: params)
|
|
15
18
|
end
|
|
16
19
|
|
|
20
|
+
# Returns a variety of information about the Tweet specified by the requested ID or list of IDs.
|
|
17
21
|
# @param [Array<String>] :ids the collection of requested Tweet IDs
|
|
18
|
-
# @param [
|
|
19
|
-
|
|
22
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
23
|
+
#
|
|
24
|
+
# e.g.: { tweet: %w[id username], media: %w[url] }
|
|
25
|
+
# @return [Array<Hash>] of tweet objects with the requested tweet fields
|
|
26
|
+
def get_tweets(ids:, fields: DEFAULT_FIELDS)
|
|
20
27
|
url = 'https://api.twitter.com/labs/2/tweets'
|
|
21
|
-
params =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
params = ParamsService.from_fields(fields)
|
|
29
|
+
params.merge!({ ids: ids.join(',') })
|
|
30
|
+
|
|
31
|
+
make_request(url: url, params: params, is_collection: true)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Hides or unhides a reply to a Tweet.
|
|
35
|
+
# @param [String] :id the ID of the requested Tweet; must belong to a conversation by the authenticated user
|
|
36
|
+
# @return boolean indicating the hidden status of the requested tweet
|
|
37
|
+
def hide_reply(id:)
|
|
38
|
+
url = "https://api.twitter.com/labs/2/tweets/#{id}/hidden"
|
|
39
|
+
|
|
40
|
+
make_request(url: url, method: :put)[:hidden]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The Labs recent search endpoint returns Tweets from the last 7 days that match a search query.
|
|
44
|
+
# @param [String] :query the search query
|
|
45
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
46
|
+
#
|
|
47
|
+
# e.g.: { tweet: %w[id username], media: %w[url] }
|
|
48
|
+
# @return [Array<Hash>] of tweet objects with the requested tweet fields
|
|
49
|
+
def search(query:, fields: DEFAULT_FIELDS)
|
|
50
|
+
url = 'https://api.twitter.com/labs/2/tweets/search'
|
|
51
|
+
params = ParamsService.from_fields(fields)
|
|
52
|
+
params.merge!({ query: query })
|
|
25
53
|
|
|
26
54
|
make_request(url: url, params: params, is_collection: true)
|
|
27
55
|
end
|
|
@@ -2,38 +2,45 @@ module TwitterLabsAPI
|
|
|
2
2
|
module Resources
|
|
3
3
|
module User
|
|
4
4
|
DEFAULT_USER_FIELDS = %w[id name username].freeze
|
|
5
|
+
DEFAULT_FIELDS = { user: DEFAULT_USER_FIELDS }.freeze
|
|
5
6
|
|
|
7
|
+
# Returns a variety of information about a single more user specified by the requested ID.
|
|
6
8
|
# @param [String] :id the ID of the requested User
|
|
7
|
-
# @param [
|
|
8
|
-
|
|
9
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
10
|
+
#
|
|
11
|
+
# e.g.: { user: %w[id name username] }
|
|
12
|
+
# @return Hash an object with requested user fields
|
|
13
|
+
def get_user(id:, fields: DEFAULT_FIELDS)
|
|
9
14
|
url = "https://api.twitter.com/labs/2/users/#{id}"
|
|
10
|
-
params =
|
|
11
|
-
'user.fields' => user_fields.join(',')
|
|
12
|
-
}.compact
|
|
15
|
+
params = ParamsService.from_fields(fields)
|
|
13
16
|
|
|
14
17
|
make_request(url: url, params: params)
|
|
15
18
|
end
|
|
16
19
|
|
|
20
|
+
# Returns a variety of information about one or more Users specified by the requested IDs.
|
|
17
21
|
# @param [Array<String>] :ids the collection of requested User IDs
|
|
18
|
-
# @param [
|
|
19
|
-
|
|
22
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
23
|
+
#
|
|
24
|
+
# e.g.: { user: %w[id name username] }
|
|
25
|
+
# @return [Array<Hash>] of user objects with the requested user fields
|
|
26
|
+
def get_users(ids:, fields: DEFAULT_FIELDS)
|
|
20
27
|
url = 'https://api.twitter.com/labs/2/users'
|
|
21
|
-
params =
|
|
22
|
-
|
|
23
|
-
'user.fields' => user_fields.join(',')
|
|
24
|
-
}.compact
|
|
28
|
+
params = ParamsService.from_fields(fields)
|
|
29
|
+
params.merge!({ ids: ids.join(',') })
|
|
25
30
|
|
|
26
31
|
make_request(url: url, params: params, is_collection: true)
|
|
27
32
|
end
|
|
28
33
|
|
|
34
|
+
# Returns a variety of information about one or more Users specified by the requested usernames.
|
|
29
35
|
# @param [Array<String>] :usernames the collection of requested Usernames
|
|
30
|
-
# @param [
|
|
31
|
-
|
|
36
|
+
# @param [Hash] :fields a hash for fields to include in the response payload;
|
|
37
|
+
#
|
|
38
|
+
# e.g.: { user: %w[id name username] }
|
|
39
|
+
# @return [Array<Hash>] of user objects with the requested user fields
|
|
40
|
+
def get_users_by_usernames(usernames:, fields: DEFAULT_FIELDS)
|
|
32
41
|
url = 'https://api.twitter.com/labs/2/users/by'
|
|
33
|
-
params =
|
|
34
|
-
|
|
35
|
-
'user.fields' => user_fields.join(',')
|
|
36
|
-
}.compact
|
|
42
|
+
params = ParamsService.from_fields(fields)
|
|
43
|
+
params.merge!({ usernames: usernames.join(',') })
|
|
37
44
|
|
|
38
45
|
make_request(url: url, params: params, is_collection: true)
|
|
39
46
|
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class ParamsService
|
|
2
|
+
# Convert user-passed fields hash to query params
|
|
3
|
+
# @param [Hash] :fields A hash of requested fields; see API Reference for details
|
|
4
|
+
# @return [Hash] A hash of query params for consumption by API client
|
|
5
|
+
# @example
|
|
6
|
+
# input: { tweet: %w[id username], media: ['url'] }
|
|
7
|
+
# output: { 'tweet.fields' => 'id,username', 'media.fields' => 'url }
|
|
8
|
+
def self.from_fields(fields)
|
|
9
|
+
fields.keys.inject({}) do |memo, key|
|
|
10
|
+
memo["#{key}.fields"] = fields[key].join(',')
|
|
11
|
+
|
|
12
|
+
memo
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: twitter_labs_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tomholford
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-
|
|
11
|
+
date: 2020-08-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -90,6 +90,7 @@ files:
|
|
|
90
90
|
- lib/twitter_labs_api/client.rb
|
|
91
91
|
- lib/twitter_labs_api/resources/tweet.rb
|
|
92
92
|
- lib/twitter_labs_api/resources/user.rb
|
|
93
|
+
- lib/twitter_labs_api/services/params_service.rb
|
|
93
94
|
- lib/twitter_labs_api/version.rb
|
|
94
95
|
- twitter-labs-api.gemspec
|
|
95
96
|
homepage: https://github.com/tomholford/twitter-labs-api
|