Bump Twitter to 7.0 and refactor TwitterBot (#6246) [deploy]
This commit is contained in:
parent
7744973dd9
commit
168f5c597c
9 changed files with 34 additions and 33 deletions
2
Gemfile
2
Gemfile
|
|
@ -95,7 +95,7 @@ gem "stripe", "~> 5.15" # Ruby library for the Stripe API
|
|||
gem "timber", "~> 3.0" # Great Ruby logging made easy
|
||||
gem "timber-rails", "~> 1.0" # Timber integration for Rails
|
||||
gem "twilio-ruby", "~> 5.31" # The official library for communicating with the Twilio REST API
|
||||
gem "twitter", "~> 6.2" # A Ruby interface to the Twitter API
|
||||
gem "twitter", "~> 7.0" # A Ruby interface to the Twitter API
|
||||
gem "typhoeus", "~> 1.3.1" # Used with Elasticsearch to support http keep-alive connections
|
||||
gem "uglifier", "~> 4.2" # Uglifier minifies JavaScript files
|
||||
gem "ulid", "~> 1.2" # Universally Unique Lexicographically Sortable Identifier implementation for Ruby
|
||||
|
|
|
|||
17
Gemfile.lock
17
Gemfile.lock
|
|
@ -311,6 +311,9 @@ GEM
|
|||
loofah (>= 2.3.1)
|
||||
sax-machine (>= 1.0)
|
||||
ffi (1.12.2)
|
||||
ffi-compiler (1.0.1)
|
||||
ffi (>= 1.0.0)
|
||||
rake
|
||||
figaro (1.1.1)
|
||||
thor (~> 0.14)
|
||||
fix-db-schema-conflicts (3.0.3)
|
||||
|
|
@ -385,15 +388,17 @@ GEM
|
|||
html_truncator (0.4.2)
|
||||
nokogiri (~> 1.5)
|
||||
htmlentities (4.3.4)
|
||||
http (3.3.0)
|
||||
http (4.3.0)
|
||||
addressable (~> 2.3)
|
||||
http-cookie (~> 1.0)
|
||||
http-form_data (~> 2.0)
|
||||
http_parser.rb (~> 0.6.0)
|
||||
http-form_data (~> 2.2)
|
||||
http-parser (~> 1.2.0)
|
||||
http-accept (1.7.0)
|
||||
http-cookie (1.0.3)
|
||||
domain_name (~> 0.5)
|
||||
http-form_data (2.2.0)
|
||||
http-parser (1.2.1)
|
||||
ffi-compiler (>= 1.0, < 2.0)
|
||||
http_parser.rb (0.6.0)
|
||||
httparty (0.17.3)
|
||||
mime-types (~> 3.0)
|
||||
|
|
@ -772,11 +777,11 @@ GEM
|
|||
faraday (~> 0.9)
|
||||
jwt (>= 1.5, <= 2.5)
|
||||
nokogiri (>= 1.6, < 2.0)
|
||||
twitter (6.2.0)
|
||||
twitter (7.0.0)
|
||||
addressable (~> 2.3)
|
||||
buftok (~> 0.2.0)
|
||||
equalizer (~> 0.0.11)
|
||||
http (~> 3.0)
|
||||
http (~> 4.0)
|
||||
http-form_data (~> 2.0)
|
||||
http_parser.rb (~> 0.6.0)
|
||||
memoizable (~> 0.4.0)
|
||||
|
|
@ -970,7 +975,7 @@ DEPENDENCIES
|
|||
timber-rails (~> 1.0)
|
||||
timecop (~> 0.9)
|
||||
twilio-ruby (~> 5.31)
|
||||
twitter (~> 6.2)
|
||||
twitter (~> 7.0)
|
||||
typhoeus (~> 1.3.1)
|
||||
uglifier (~> 4.2)
|
||||
ulid (~> 1.2)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,10 @@
|
|||
class TwitterBot
|
||||
attr_reader :token, :secret
|
||||
|
||||
def initialize(token:, secret:)
|
||||
@token = token
|
||||
@secret = secret
|
||||
end
|
||||
|
||||
def client
|
||||
def self.client(token:, secret:)
|
||||
Twitter::REST::Client.new do |config|
|
||||
config.consumer_key = ApplicationConfig["TWITTER_KEY"]
|
||||
config.consumer_secret = ApplicationConfig["TWITTER_SECRET"]
|
||||
config.access_token = @token
|
||||
config.access_token_secret = @secret
|
||||
config.access_token = token
|
||||
config.access_token_secret = secret
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ class Tweet < ApplicationRecord
|
|||
|
||||
class << self
|
||||
def try_to_get_tweet(twitter_id_code)
|
||||
client = TwitterBot.new(random_identity).client
|
||||
client = TwitterBot.client(random_identity)
|
||||
tweet = client.status(twitter_id_code, tweet_mode: "extended")
|
||||
make_tweet_from_api_object(tweet)
|
||||
end
|
||||
|
|
@ -64,13 +64,14 @@ class Tweet < ApplicationRecord
|
|||
private
|
||||
|
||||
def make_tweet_from_api_object(tweeted)
|
||||
twitter_bot = TwitterBot.new(random_identity)
|
||||
tweeted = if tweeted.attrs[:retweeted_status]
|
||||
twitter_bot.client.status(tweeted.attrs[:retweeted_status][:id_str])
|
||||
TwitterBot.client(random_identity).status(tweeted.attrs[:retweeted_status][:id_str])
|
||||
else
|
||||
tweeted
|
||||
end
|
||||
|
||||
tweet = Tweet.where(twitter_id_code: tweeted.attrs[:id_str]).first_or_initialize
|
||||
|
||||
tweet.twitter_uid = tweeted.user.id.to_s
|
||||
tweet.twitter_username = tweeted.user.screen_name.downcase
|
||||
tweet.user_id = User.find_by(twitter_username: tweeted.user.screen_name)&.id
|
||||
|
|
@ -97,7 +98,9 @@ class Tweet < ApplicationRecord
|
|||
tweet.last_fetched_at = Time.current
|
||||
tweet.user_is_verified = tweeted.user.verified?
|
||||
tweet.is_quote_status = tweeted.attrs[:is_quote_status]
|
||||
|
||||
tweet.save!
|
||||
|
||||
tweet
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ RSpec.describe TweetTag, type: :liquid_tag do
|
|||
end
|
||||
|
||||
it "accepts valid tweet id", :vcr do
|
||||
VCR.use_cassette("twitter_gem") do
|
||||
VCR.use_cassette("twitter_fetch_status") do
|
||||
liquid = generate_tweet_liquid_tag(twitter_id)
|
||||
expect(liquid.render).to include(handle)
|
||||
expect(liquid.render).to include(name)
|
||||
|
|
@ -22,7 +22,7 @@ RSpec.describe TweetTag, type: :liquid_tag do
|
|||
end
|
||||
|
||||
it "render properly", :vcr do
|
||||
VCR.use_cassette("twitter_gem") do
|
||||
VCR.use_cassette("twitter_fetch_status") do
|
||||
Time.use_zone("Asia/Tokyo") do
|
||||
rendered = generate_tweet_liquid_tag(twitter_id).render
|
||||
Approvals.verify(rendered, name: "liquid_tweet_tag_spec", format: :html)
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ RSpec.describe Article, type: :model do
|
|||
end
|
||||
|
||||
it "is valid with valid liquid tags", :vcr do
|
||||
VCR.use_cassette("twitter_gem") do
|
||||
VCR.use_cassette("twitter_fetch_status") do
|
||||
article = build_and_validate_article(with_tweet_tag: true)
|
||||
expect(article).to be_valid
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
require "rails_helper"
|
||||
|
||||
vcr_option = {
|
||||
cassette_name: "twitter_gem",
|
||||
allow_playback_repeats: "true"
|
||||
}
|
||||
VCR_OPTIONS = {
|
||||
cassette_name: "twitter_fetch_status",
|
||||
allow_playback_repeats: true
|
||||
}.freeze
|
||||
|
||||
RSpec.describe Tweet, type: :model, vcr: vcr_option do
|
||||
RSpec.describe Tweet, type: :model, vcr: VCR_OPTIONS do
|
||||
let(:tweet_id) { "1018911886862057472" }
|
||||
|
||||
it "fetches a tweet" do
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
require "rails_helper"
|
||||
|
||||
vcr_option = {
|
||||
cassette_name: "twitter_gem",
|
||||
allow_playback_repeats: "true"
|
||||
}
|
||||
VCR_OPTIONS = {
|
||||
cassette_name: "twitter_fetch_status",
|
||||
allow_playback_repeats: true
|
||||
}.freeze
|
||||
|
||||
RSpec.describe "LiquidEmbeds", type: :request, vcr: vcr_option do
|
||||
RSpec.describe "LiquidEmbeds", type: :request, vcr: VCR_OPTIONS do
|
||||
describe "get /embeds" do
|
||||
it "renders proper tweet" do
|
||||
get "/embed/tweet?args=1018911886862057472"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue