twitter_labs_api 0.6.0 → 0.7.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: 1d4b2bac3fd2da4e6171169403a9d0e98a59e6b85470f4858d38277a57516a17
4
- data.tar.gz: 72b988ee36c5bfd84e7d09aedacb7f2efc4f894bdd775ec4f5d9a1b37910207e
3
+ metadata.gz: 308afe5c20914abca1ed575f09b1e5924206034eae405886944d495fb6e4d3da
4
+ data.tar.gz: 5b931944828a477b39d0360d492c2fb6215b13ec12f8b953ac01533bcf822b44
5
5
  SHA512:
6
- metadata.gz: bbd9ec6ea62e02dc8f177889a0823b01534bf537284fb48a0b792f95a347ba08f34fe6951cea1e4bda277867432d0e344b541a70f79ad32db95dc89f61c0cec9
7
- data.tar.gz: 4fe777c3670f8c437febcf7ace17f072a15fd533fd9debbe457bc2aaff2bb36b067af746615a885541c98d468e34cb13b3b3ecc637badd521229bfc37369459c
6
+ metadata.gz: 12be24ffd5dfbc7b81b5bba6665cd264c39a8d9cbe828977b569285093b37e00036950db0a1b94c50fb287847897a6d8ba356bcb8b950c33197c4bda888a2a5a
7
+ data.tar.gz: b44d15ee5acc7a147ea3c2208c242cd45b6d5966cfbac0a2c2946b85594f73263853c4b0b45d9125567d20ffb295e33c45ae6491a2d4a0a31535a94a00b77ef7
@@ -1,3 +1,23 @@
1
+ # 0.7.0 - 28 August 2020
2
+
3
+ - Refactor interface for requesting custom response fields
4
+
5
+ ## Breaking Change
6
+
7
+ Heads up, the argument to request fields was previously called `tweet_fields`; it is now called `fields`.
8
+
9
+ ```ruby
10
+ api.get_tweet(id: '123455683780', fields: requested_fields)
11
+ ```
12
+
13
+ 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:
14
+
15
+ ```ruby
16
+ requested_fields = { tweet: %w[id username], media: %w[url] }
17
+
18
+ api.get_tweet(id: my_id, fields: requested_fields)
19
+ ```
20
+
1
21
  # 0.6.0 - 28 July 2020
2
22
 
3
23
  - Fix: broken dependency
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twitter_labs_api (0.6.0)
4
+ twitter_labs_api (0.7.0)
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.6)
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
@@ -32,14 +32,13 @@ api.get_tweet(id: '1234671272602193920')
32
32
 
33
33
  #### Specifying which fields to include in the response
34
34
 
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 like so:
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.
36
36
 
37
+ For example, to request the URL of a Tweet's embedded media item:
37
38
  ```ruby
38
- my_fields = %w[id author_id created_at text]
39
+ requested_fields = { tweet: %w[id username], media: %w[url] }
39
40
 
40
- api.get_tweet(id: '1235508591232090112', tweet_fields: my_fields)
41
-
42
- >> {"author_id"=>"229708614", "created_at"=>"2020-03-05T10:12:57.000Z", "id"=>"1235508591232090112", "text"=>"Hot take: coronavirus will not boost remote work in the long run because spur-of-the-moment work-from-home for in-person companies is likely to be a shitshow."}
41
+ api.get_tweet(id: my_id, fields: requested_fields)
43
42
  ```
44
43
 
45
44
  #### API Errors
@@ -2,30 +2,31 @@ 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
 
6
7
  # Returns a variety of information about a single Tweet specified by the requested ID.
7
8
  # @param [String] :id the ID of the requested Tweet
8
- # @param [Array<String>] :tweet_fields (["id", "author_id", "created_at", "lang", "public_metrics"]) the list of fields to retrieve for the given tweet
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] }
9
12
  # @return Hash an object with requested tweet fields
10
- def get_tweet(id:, tweet_fields: DEFAULT_TWEET_FIELDS)
13
+ def get_tweet(id:, fields: DEFAULT_FIELDS)
11
14
  url = "https://api.twitter.com/labs/2/tweets/#{id}"
12
- params = {
13
- 'tweet.fields' => tweet_fields.join(',')
14
- }.compact
15
+ params = ParamsService.from_fields(fields)
15
16
 
16
17
  make_request(url: url, params: params)
17
18
  end
18
19
 
19
20
  # Returns a variety of information about the Tweet specified by the requested ID or list of IDs.
20
21
  # @param [Array<String>] :ids the collection of requested Tweet IDs
21
- # @param [Array<String>] :tweet_fields (["id", "author_id", "created_at", "lang", "public_metrics"]) the list of fields to retrieve for the given tweet
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] }
22
25
  # @return [Array<Hash>] of tweet objects with the requested tweet fields
23
- def get_tweets(ids:, tweet_fields: DEFAULT_TWEET_FIELDS)
26
+ def get_tweets(ids:, fields: DEFAULT_FIELDS)
24
27
  url = 'https://api.twitter.com/labs/2/tweets'
25
- params = {
26
- 'ids' => ids.join(','),
27
- 'tweet.fields' => tweet_fields.join(',')
28
- }.compact
28
+ params = ParamsService.from_fields(fields)
29
+ params.merge!({ ids: ids.join(',') })
29
30
 
30
31
  make_request(url: url, params: params, is_collection: true)
31
32
  end
@@ -41,14 +42,14 @@ module TwitterLabsAPI
41
42
 
42
43
  # The Labs recent search endpoint returns Tweets from the last 7 days that match a search query.
43
44
  # @param [String] :query the search query
44
- # @param [Array<String>] :tweet_fields (["id", "author_id", "created_at", "lang", "public_metrics"]) the list of fields to retrieve for the given tweet
45
- # @param [Array<String>] :tweet_fields (["id", "author_id", "created_at", "lang", "public_metrics"]) the list of fields to retrieve for the given tweet
46
- def search(query:, tweet_fields: DEFAULT_TWEET_FIELDS)
47
- url = "https://api.twitter.com/labs/2/tweets/search"
48
- params = {
49
- 'query' => query,
50
- 'tweet.fields' => tweet_fields.join(',')
51
- }.compact
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 })
52
53
 
53
54
  make_request(url: url, params: params, is_collection: true)
54
55
  end
@@ -2,44 +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
 
6
7
  # Returns a variety of information about a single more user specified by the requested ID.
7
8
  # @param [String] :id the ID of the requested User
8
- # @param [Array<String>] :user_fields (["name", "username"]) the list of fields to retrieve for the given User
9
+ # @param [Hash] :fields a hash for fields to include in the response payload;
10
+ #
11
+ # e.g.: { user: %w[id name username] }
9
12
  # @return Hash an object with requested user fields
10
- def get_user(id:, user_fields: DEFAULT_USER_FIELDS)
13
+ def get_user(id:, fields: DEFAULT_FIELDS)
11
14
  url = "https://api.twitter.com/labs/2/users/#{id}"
12
- params = {
13
- 'user.fields' => user_fields.join(',')
14
- }.compact
15
+ params = ParamsService.from_fields(fields)
15
16
 
16
17
  make_request(url: url, params: params)
17
18
  end
18
19
 
19
20
  # Returns a variety of information about one or more Users specified by the requested IDs.
20
21
  # @param [Array<String>] :ids the collection of requested User IDs
21
- # @param [Array<String>] :user_fields (["name", "username"]) the list of fields to retrieve for the given User
22
+ # @param [Hash] :fields a hash for fields to include in the response payload;
23
+ #
24
+ # e.g.: { user: %w[id name username] }
22
25
  # @return [Array<Hash>] of user objects with the requested user fields
23
- def get_users(ids:, user_fields: DEFAULT_USER_FIELDS)
26
+ def get_users(ids:, fields: DEFAULT_FIELDS)
24
27
  url = 'https://api.twitter.com/labs/2/users'
25
- params = {
26
- 'ids' => ids.join(','),
27
- 'user.fields' => user_fields.join(',')
28
- }.compact
28
+ params = ParamsService.from_fields(fields)
29
+ params.merge!({ ids: ids.join(',') })
29
30
 
30
31
  make_request(url: url, params: params, is_collection: true)
31
32
  end
32
33
 
33
34
  # Returns a variety of information about one or more Users specified by the requested usernames.
34
35
  # @param [Array<String>] :usernames the collection of requested Usernames
35
- # @param [Array<String>] :user_fields (["name", "username"]) the list of fields to retrieve for the given User
36
+ # @param [Hash] :fields a hash for fields to include in the response payload;
37
+ #
38
+ # e.g.: { user: %w[id name username] }
36
39
  # @return [Array<Hash>] of user objects with the requested user fields
37
- def get_users_by_usernames(usernames:, user_fields: DEFAULT_USER_FIELDS)
40
+ def get_users_by_usernames(usernames:, fields: DEFAULT_FIELDS)
38
41
  url = 'https://api.twitter.com/labs/2/users/by'
39
- params = {
40
- 'usernames' => usernames.join(','),
41
- 'user.fields' => user_fields.join(',')
42
- }.compact
42
+ params = ParamsService.from_fields(fields)
43
+ params.merge!({ usernames: usernames.join(',') })
43
44
 
44
45
  make_request(url: url, params: params, is_collection: true)
45
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
@@ -1,3 +1,3 @@
1
1
  module TwitterLabsAPI
2
- VERSION = '0.6.0'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomholford
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-28 00:00:00.000000000 Z
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