Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TwitterFavorites and TwitterUserAgent create events in chronological order #3249

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/agents/twitter_favorites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def check
tweets = twitter.favorites(interpolated[:username], opts)
memory[:last_seen] ||= []

tweets.each do |tweet|
tweets.sort_by(&:id).each do |tweet|
next if memory[:last_seen].include?(tweet.id) || tweet.created_at < starting_at

memory[:last_seen].push(tweet.id)
Expand Down
2 changes: 1 addition & 1 deletion app/models/agents/twitter_user_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def check
twitter.user_timeline(interpolated[:username], opts)
end

tweets.each do |tweet|
tweets.sort_by(&:id).each do |tweet|
next unless tweet.created_at >= starting_at

memory[:since_id] = [tweet.id, *memory[:since_id]].max
Expand Down
16 changes: 10 additions & 6 deletions spec/models/agents/twitter_favorites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

describe Agents::TwitterFavorites do
before do
stub_request(:any, /tectonic.*[?&]tweet_mode=extended/).
to_return(body: File.read(Rails.root.join("spec/data_fixtures/user_fav_tweets.json")),
headers: { 'Content-Type': 'application/json;charset=utf-8' },
status: 200)
stub_request(:any, /tectonic.*[?&]tweet_mode=extended/)
.to_return(body: File.read(Rails.root.join("spec/data_fixtures/user_fav_tweets.json")),
headers: { 'Content-Type': 'application/json;charset=utf-8' },
status: 200)
end

before do
@opts = {:username => "tectonic", :number => "10", :history => "100", :expected_update_period_in_days => "2", :starting_at => "Sat Feb 20 01:32:08 +0000 2016"}
@opts = { username: "tectonic", number: "10", history: "100", expected_update_period_in_days: "2",
starting_at: "Sat Feb 20 01:32:08 +0000 2016" }

@agent = Agents::TwitterFavorites.new(name: "tectonic", options: @opts)
@agent.service = services(:generic)
Expand All @@ -31,7 +32,10 @@

describe "making sure check method works" do
it "expect change method to change event" do
expect { @agent.check }.to change {Event.count}.by(3)
expect { @agent.check }.to change { Event.count }.by(3)
Event.last(3).each_cons(2) do |t1, t2|
expect(t1.payload[:id]).to be < t2.payload[:id]
end
end
end

Expand Down
44 changes: 24 additions & 20 deletions spec/models/agents/twitter_user_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
describe Agents::TwitterUserAgent do
before do
# intercept the twitter API request for @tectonic's user profile
stub_request(:any, "https://api.twitter.com/1.1/statuses/user_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&screen_name=tectonic&tweet_mode=extended").
to_return(body: File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")),
headers: { 'Content-Type': 'application/json;charset=utf-8' },
status: 200)
stub_request(:any, "https://api.twitter.com/1.1/statuses/user_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&screen_name=tectonic&tweet_mode=extended")
.to_return(body: File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")),
headers: { 'Content-Type': 'application/json;charset=utf-8' },
status: 200)

@opts = {
:username => "tectonic",
:include_retweets => "true",
:exclude_replies => "false",
:expected_update_period_in_days => "2",
:starting_at => "Jan 01 00:00:01 +0000 2000",
:consumer_key => "---",
:consumer_secret => "---",
:oauth_token => "---",
:oauth_token_secret => "---"
username: "tectonic",
include_retweets: "true",
exclude_replies: "false",
expected_update_period_in_days: "2",
starting_at: "Jan 01 00:00:01 +0000 2000",
consumer_key: "---",
consumer_secret: "---",
oauth_token: "---",
oauth_token_secret: "---"
}

@checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => @opts)
@checker = Agents::TwitterUserAgent.new(name: "tectonic", options: @opts)
@checker.service = services(:generic)
@checker.user = users(:bob)
@checker.save!
Expand All @@ -29,14 +29,17 @@
describe "#check" do
it "should check for changes" do
expect { @checker.check }.to change { Event.count }.by(5)
Event.last(5).each_cons(2) do |t1, t2|
expect(t1.payload[:id]).to be < t2.payload[:id]
end
end
end

describe "#check with starting_at=future date" do
it "should check for changes starting_at a future date, thus not find any" do
opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })
opts = @opts.merge({ starting_at: "Jan 01 00:00:01 +0000 2999", })

checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
checker = Agents::TwitterUserAgent.new(name: "tectonic", options: opts)
checker.service = services(:generic)
checker.user = users(:bob)
checker.save!
Expand All @@ -47,7 +50,9 @@

describe "#check that if choose time line is false then username is required" do
before do
stub_request(:any, "https://api.twitter.com/1.1/statuses/home_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&tweet_mode=extended").to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
stub_request(:any, "https://api.twitter.com/1.1/statuses/home_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&tweet_mode=extended").to_return(
body: File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), status: 200
)
end

it 'requires username unless choose_home_time_line is true' do
Expand All @@ -69,10 +74,9 @@
end

it "error messaged added if choose_home_time_line is false and username does not exist" do
opts = @opts.tap { |o| o.delete(:username) }.merge!({ choose_home_time_line: "false" })

opts = @opts.tap { |o| o.delete(:username) }.merge!({:choose_home_time_line => "false" })

checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
checker = Agents::TwitterUserAgent.new(name: "tectonic", options: opts)
checker.service = services(:generic)
checker.user = users(:bob)
expect(checker.save).to eq false
Expand Down