twitter_labs_api 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b280e642f8468d372684da3c0a04ce2effffd652b244e53a50e1925ade66d3fc
4
+ data.tar.gz: e1f1513a92468bc7fdb3e538bbc70b44ab15be2251f203485258f701b8c6b629
5
+ SHA512:
6
+ metadata.gz: 7166032d4689b43fd696fc5f627dfe00af89eec532b3f36a652d6b22d3c74732c75b60e36be3a18caa9683b68c3b4629abad0d9319985ad56ef01ee19264ad94
7
+ data.tar.gz: 259cb764a4c6f15ed5efb335bc46da7117fa447da3c19fd7754e931e40616bd2464847b547a9a17a7d5094f43195f717f196da6733ec47bfbf7d9c8470aa3e19
@@ -0,0 +1,3 @@
1
+ # 0.1.0 - 5 March 2020
2
+
3
+ Initial gem commit
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'activesupport'
6
+ gem 'httplog'
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twitter_labs_api (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.0.0)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.1, >= 2.1.8)
15
+ concurrent-ruby (1.1.6)
16
+ httplog (1.3.2)
17
+ rack (>= 1.0)
18
+ rainbow (>= 2.0.0)
19
+ i18n (1.6.0)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.14.0)
22
+ rack (2.2.2)
23
+ rainbow (3.0.0)
24
+ rake (10.5.0)
25
+ thread_safe (0.3.6)
26
+ tzinfo (1.2.6)
27
+ thread_safe (~> 0.1)
28
+ zeitwerk (2.1.9)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ activesupport
35
+ bundler (~> 2.0)
36
+ httplog
37
+ rake (~> 10.0)
38
+ twitter_labs_api!
39
+
40
+ BUNDLED WITH
41
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 tomholford
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # twitter-labs-api
2
+
3
+ A basic implementation of a Twitter Labs API client in Ruby. This project uses the v2 endpoints announced [here](https://twittercommunity.com/t/releasing-a-new-version-of-labs-endpoints/134219/3).
4
+
5
+ ## Usage
6
+
7
+ ### 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.
9
+
10
+ One easy way to get a bearer token is to use [this method](https://www.rubydoc.info/gems/twitter/Twitter/REST/Client#bearer_token%3F-instance_method) from https://github.com/sferik/twitter.
11
+
12
+ ### Example
13
+
14
+ ```ruby
15
+ requre `labs_api`
16
+
17
+ api = TwitterLabsAPI.new(bearer_token: 'YOUR-BEARER-TOKEN')
18
+
19
+ api.get_tweet(id: '1234671272602193920')
20
+
21
+ >> {"data"=>{"author_id"=>"44196397", "created_at"=>"2020-03-03T02:45:45.000Z", "id"=>"1234671272602193920", "lang"=>"und", "public_metrics"=>{"retweet_count"=>4534, "reply_count"=>1036, "like_count"=>43489, "quote_count"=>224}, "text"=>"✌️ bro https://t.co/nJ7CUyhr2j"}}
22
+ ```
23
+
24
+ ### Status
25
+ Currently, the following endpoints are implemented:
26
+
27
+ - `TwitterLabsAPI#get_tweet` - Retrieve a single Tweet object with an `id`
28
+ - `TwitterLabsAPI#get_tweets` - Retrieve multiple Tweets with a collection of `ids`
29
+ - `TwitterLabsAPI#get_user`, - Retrieve a single user object with an `id`
30
+ - `TwitterLabsAPI#get_users`, - Retrieve multiple user objects with a collection of `ids`
31
+
32
+ ## Roadmap
33
+
34
+ Since this project is initially driven by my own usage of the API, I will focus on implementing and refinining the Tweets, Users, and Metrics endpoints. If this repo gets enough interest, I might implement the other endpoints and create a proper `.gemspec`. And of course, contributions are welcome :)
35
+
36
+ ## Dependencies
37
+
38
+ This lib uses Ruby's built-in `URI` and `net/http` libs to communicate with Twitter's Labs API.
39
+
40
+ For ease of manipulating responses, this lib depends on `Hash::WithIndifferentAccess` from the Rails `activesupport` project ([docs](https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html)).
41
+
42
+ Thus, one can access the data from a response like so:
43
+ ```ruby
44
+ response = api.get_tweet(id: '1234671272602193920')
45
+
46
+ puts response[:data][:public_metrics][:like_count]
47
+ >> 43489
48
+
49
+ puts response['data']['public_metrics']['like_count']
50
+ >> 43489
51
+ ```
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'twitter_labs_api'
5
+ require 'irb'
6
+
7
+ token = File.open(File.expand_path('~/.secrets/twitter/test.token', __dir__)).read.strip
8
+ api = TwitterLabsAPI.new(bearer_token: token, debug: true)
9
+
10
+ binding.irb
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,92 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ DEFAULT_TWEET_FIELDS = %w[id author_id created_at lang public_metrics].join(',').freeze
7
+ DEFAULT_USER_FIELDS = %w[name username].join(',').freeze
8
+
9
+ class TwitterLabsAPIError < StandardError; end
10
+
11
+ class TwitterLabsAPI
12
+ attr_accessor :bearer_token, :debug
13
+
14
+ def initialize(bearer_token:, debug: false)
15
+ @bearer_token = bearer_token
16
+ @debug = debug
17
+ require 'httplog' if debug
18
+ end
19
+
20
+ def get_tweet(id:, tweet_fields: DEFAULT_TWEET_FIELDS)
21
+ url = "https://api.twitter.com/labs/2/tweets/#{id}"
22
+ params = {
23
+ 'tweet.fields' => tweet_fields
24
+ }.compact
25
+
26
+ make_request(url: url, params: params)
27
+ end
28
+
29
+ def get_tweets(ids:, tweet_fields: DEFAULT_TWEET_FIELDS)
30
+ url = 'https://api.twitter.com/labs/2/tweets'
31
+ params = {
32
+ 'ids' => ids.join(','),
33
+ 'tweet.fields' => tweet_fields
34
+ }.compact
35
+
36
+ make_request(url: url, params: params, is_collection: true)
37
+ end
38
+
39
+ def get_user(id:, user_fields: DEFAULT_USER_FIELDS)
40
+ url = "https://api.twitter.com/labs/2/users/#{id}"
41
+ params = {
42
+ 'user.fields' => user_fields
43
+ }.compact
44
+
45
+ make_request(url: url, params: params)
46
+ end
47
+
48
+ def get_users(ids:, user_fields: DEFAULT_USER_FIELDS)
49
+ url = 'https://api.twitter.com/labs/2/users'
50
+ params = {
51
+ 'ids' => ids.join(','),
52
+ 'user.fields' => user_fields
53
+ }.compact
54
+
55
+ make_request(url: url, params: params, is_collection: true)
56
+ end
57
+
58
+ private
59
+
60
+ def make_request(url:, params: {}, is_collection: false)
61
+ uri = URI.parse(url)
62
+ uri.query = URI.encode_www_form(params)
63
+ request = Net::HTTP::Get.new(uri)
64
+ request['Authorization'] = "Bearer #{bearer_token}"
65
+ req_options = { use_ssl: uri.scheme == 'https' }
66
+
67
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
68
+ http.request(request)
69
+ end
70
+
71
+ is_collection ? handle_collection(response) : handle_single(response)
72
+ end
73
+
74
+ # TODO: handle non-api errs (e.g., timeouts)
75
+ def handle_single(response)
76
+ return JSON.parse(response.body)['data'].with_indifferent_access if response.is_a?(Net::HTTPSuccess)
77
+
78
+ handle_api_error(response)
79
+ end
80
+
81
+ def handle_collection(response)
82
+ return JSON.parse(response.body)['data'].map(&:with_indifferent_access) if response.is_a?(Net::HTTPSuccess)
83
+
84
+ handle_api_error(response)
85
+ end
86
+
87
+ def handle_api_error(response)
88
+ error = JSON.parse(response.body)
89
+
90
+ raise TwitterLabsAPIError, "#{error['title']}: #{error['detail']} #{error['type']}"
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ class TwitterLabsAPI
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'twitter_labs_api/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'twitter_labs_api'
7
+ spec.version = TwitterLabsAPI::VERSION
8
+ spec.authors = ['tomholford']
9
+ spec.email = ['tomholford@users.noreply.github.com']
10
+
11
+ spec.summary = 'A basic implementation of a Twitter Labs API client in Ruby'
12
+ spec.homepage = 'https://github.com/tomholford/twitter-labs-api'
13
+ spec.license = 'MIT'
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/tomholford/twitter-labs-api'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/tomholford/twitter-labs-api/blob/master/CHANGELOG.md'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.0'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_labs_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tomholford
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - tomholford@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/twitter_labs_api.rb
57
+ - lib/twitter_labs_api/version.rb
58
+ - twitter-labs-api.gemspec
59
+ homepage: https://github.com/tomholford/twitter-labs-api
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/tomholford/twitter-labs-api
64
+ source_code_uri: https://github.com/tomholford/twitter-labs-api
65
+ changelog_uri: https://github.com/tomholford/twitter-labs-api/blob/master/CHANGELOG.md
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A basic implementation of a Twitter Labs API client in Ruby
85
+ test_files: []