Skip to content

Commit

Permalink
Bump version v0.0.7
Browse files Browse the repository at this point in the history
User#followers
  • Loading branch information
robertodecurnex committed Jul 5, 2022
1 parent 1d38e23 commit 75c4edf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ client.user_by_username("username") #=> #<Twttr::Model::User>
#Twttr::Client#users(:user_ids)
client.users(["user_id_1", "user_id_2"]) #=> [#<Twttr::Model::User>]

# Twttr::Client#Followers
client.followers("user_id") == client.user("user_id").followers

# Twttr::Client#Following
client.following("user_id") == client.user("user_id").following
```
Expand Down
14 changes: 14 additions & 0 deletions lib/twttr/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ def initialize(data, client = nil)
@client = client
end

# GET /2/users/:id/followers
# https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-followers
#
# Returns paginated list of users following user_id.
#
# @param max_results [Integer] Max number of results per peage.
# @param pagination_token [String] Initial page pagination token.
# @yield [Array<Twttr::Model::User>] User followers page.
# @yield [String,NilClass] Pagination token.
# @return [Array<Twttr::Model::User>] Follower Users.
def followers(max_results: nil, pagination_token: nil, &block)
client.followers(id, max_results: max_results, pagination_token: pagination_token, &block)
end

# Forwards to Follows#following setting user_id to current user's id.
#
# GET /2/users/:id/following
Expand Down
31 changes: 30 additions & 1 deletion test/lib/twttr/model/test_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Twttr
class Client
class TestUser < Minitest::Test
class TestUser < Minitest::Test # rubocop:disable Metrics/ClassLength
def setup
@client = Twttr::Client.new do |config|
config.consumer_key = 'consumer_key'
Expand All @@ -30,6 +30,8 @@ def setup
"meta": {"result_count": 2}
}'
@mock_oauth_responses = {
'https://api.twitter.com/2/users/user_id/followers' => OpenStruct.new(body: first_mock_body),
'https://api.twitter.com/2/users/user_id/followers?pagination_token=next-token' => OpenStruct.new(body: second_mock_body),
'https://api.twitter.com/2/users/user_id/following' => OpenStruct.new(body: first_mock_body),
'https://api.twitter.com/2/users/user_id/following?pagination_token=next-token' => OpenStruct.new(body: second_mock_body)
}
Expand Down Expand Up @@ -64,6 +66,33 @@ def test_initialize
end
end

def test_followers
mock = lambda do |uri, _config|
@mock_oauth_responses[uri.to_s]
end
Twttr::Client::OAuthRequest.stub :get, mock do
users = @user.followers
assert_equal(4, users.length)
users.each { |user| assert_instance_of(Twttr::Model::User, user) }
end
end

def test_followers_with_block # rubocop:disable Metrics/AbcSize
mock = lambda do |uri, _config|
puts uri.to_s
@mock_oauth_responses[uri.to_s]
end
Twttr::Client::OAuthRequest.stub :get, mock do
all_users = @user.followers do |users, token|
assert_equal(2, users.length)
users.each { |user| assert_instance_of(Twttr::Model::User, user) }
assert_nil(nil, token)
end
assert_equal(4, all_users.length)
all_users.each { |user| assert_instance_of(Twttr::Model::User, user) }
end
end

def test_following
mock = lambda do |uri, _config|
@mock_oauth_responses[uri.to_s]
Expand Down
2 changes: 1 addition & 1 deletion twttr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'twttr'
s.version = '0.0.6'
s.version = '0.0.7'
s.summary = 'Twitter API v2 Interface'
s.description = 'Modular Twitter API interface, initially targeting Twitter API v2'
s.authors = ['Roberto Decurnex']
Expand Down

0 comments on commit 75c4edf

Please sign in to comment.