File: friendships.rb

package info (click to toggle)
ruby-twitter-oauth 0.4.94-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 446; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,394 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module TwitterOAuth
  class Client

    # Returns an array of numeric IDs for every user the specified user is following.
    def friends_ids(options={})
      args = options.map{|k,v| "#{k}=#{v}"}.join('&')
      get("/friends/ids.json?#{args}")
    end

    # Returns an array of numeric IDs for every user following the specified user.
    def followers_ids(options={})
      args = options.map{|k,v| "#{k}=#{v}"}.join('&')
      get("/followers/ids.json?#{args}")
    end

    # Allows the authenticating user to follow the specified user. Returns the befriended user when successful.
    def friend(id)
      post("/friendships/create/#{id}.json")
    end

    # Allows the authenticating users to unfollow the specified user. Returns the unfollowed user when successful.
    def unfriend(id)
      post("/friendships/destroy/#{id}.json")
    end

    # Tests for the existence of friendship between two users. Will return true if user_a follows user_b, otherwise will return false.
    # You are better off using get_friendship as it returns more extended information.
    def friends?(a, b)
      oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}")
      oauth_response.body.strip == 'true'
    end

    # Returns detailed information about the relationship between two users.
    def get_friendship(a, b)
      get("/friendships/show.json?source_screen_name=#{a}&target_screen_name=#{b}")
    end

    # Returns a cursored collection of user objects for every user the specified
    # user is following (otherwise known as their "friends")
    def friends(cursor=-1)
      get("/friends/list.json?cursor=#{cursor}")
    end

    # Helper to retrun all friends via multiple requests
    def all_friends(cursor=-1)
      users = []
      while cursor != 0 do
        json = friends(cursor)
        cursor = json["next_cursor"]
        users += json["users"]
      end
      users
    end

    # Returns a cursored collection of user objects for users following the
    # specified user.
    def followers(cursor=-1)
      get("/followers/list.json?cursor=#{cursor}")
    end

    # Helper to retrun all followers via multiple requests
    def all_followers(cursor=-1)
      users = []
      while cursor != 0 do
        json = followers(cursor)
        cursor = json["next_cursor"]
        users += json["users"]
      end
      users
    end

  end
end