Class: Pupil

Inherits:
Object
  • Object
show all
Includes:
Essentials
Defined in:
lib/pupil/cli.rb,
lib/pupil/base.rb,
lib/pupil/help.rb,
lib/pupil/lists.rb,
lib/pupil/users.rb,
lib/pupil/blocks.rb,
lib/pupil/search.rb,
lib/pupil/account.rb,
lib/pupil/general.rb,
lib/pupil/schemes.rb,
lib/pupil/version.rb,
lib/pupil/statuses.rb,
lib/pupil/essentials.rb,
lib/pupil/friendships.rb,
lib/pupil/keygen/base.rb,
lib/pupil/stream/base.rb,
lib/pupil/direct_messages.rb

Defined Under Namespace

Modules: Essentials Classes: CLI, DirectMessage, Entities, Hashtag, Keygen, List, NetworkError, Scheme, Status, Stream, URL, User, UserMention

Constant Summary collapse

TWITTER_API_URL =
"https://api.twitter.com"
VERSION =
"0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Essentials

#get, #guess_parameter, #post, #serialize_parameter

Constructor Details

#initialize(key) ⇒ Pupil

Returns a new instance of Pupil.

Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pupil/base.rb', line 14

def initialize key
  @consumer = OAuth::Consumer.new(
    key[:consumer_key],
    key[:consumer_secret],
    :site => TWITTER_API_URL
  )
  @access_token = OAuth::AccessToken.new(
    @consumer,
    key[:access_token],
    key[:access_token_secret]
  )

  @screen_name = key[:screen_name] || nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(action, *args) ⇒ Object



34
35
36
37
38
39
# File 'lib/pupil/general.rb', line 34

def method_missing(action, *args)
  # e.g. pupil.users_search("username", :method => :post)
  url = "/1/#{action.to_s.split('_').join('/')}.json"
  response = self.get(url, args)
  return response
end

Instance Attribute Details

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



8
9
10
# File 'lib/pupil/base.rb', line 8

def screen_name
  @screen_name
end

Instance Method Details

#block(param) ⇒ Pupil::User

Returns response.

Parameters:

  • id (Fixnum)

    id

Returns:



4
5
6
7
8
9
10
11
# File 'lib/pupil/blocks.rb', line 4

def block(param)
  response = self.post("blocks/create.json", {guess_parameter(param) => param})
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

#blockingArray

Returns list of blocking users.

Returns:

  • (Array)

    list of blocking users



25
26
27
28
29
30
31
32
33
34
# File 'lib/pupil/blocks.rb', line 25

def blocking
  response = self.get("blocks/list.json")
  return [] if response["nilclasses"]
  users = Array.new
  response["users"].each do |element|
    user = User.new element
    users << user
  end
  return users
end

#configurationObject



2
3
4
5
# File 'lib/pupil/help.rb', line 2

def configuration()
  response = self.get("help/configuration.json")
  return response
end

#create_list(name, option = {}) ⇒ Object



18
19
20
21
# File 'lib/pupil/lists.rb', line 18

def create_list(name, option={})
  response = self.post("lists/create.json", {:name => name}.update(option))
  return List.new(response, @access_token)
end

#destroy(status_id) ⇒ Object



161
162
163
164
165
# File 'lib/pupil/statuses.rb', line 161

def destroy(status_id)
  response = self.post("statuses/destroy/#{status_id}.json")
  return false unless response
  response
end

#direct_messages(param = {}) ⇒ Hash

Returning direct messages

Parameters:

  • param (Hash) (defaults to: {})

Returns:

  • (Hash)

    directmessages



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pupil/direct_messages.rb', line 5

def direct_messages(param = {})
  response = self.get("direct_messages.json", param)
  return false unless response
  directmessages = Array.new
  response.each do |element|
    dm = DirectMessage.new(element, @access_token)
    directmessages << dm
  end

  return directmessages
end

#end_sessionHash

End oauth session

Returns:



21
22
23
24
# File 'lib/pupil/account.rb', line 21

def end_session
  response = self.post("account/end_session.json")
  return response
end

#follow(param) ⇒ Hash

Follow user for screen_name

Parameters:

  • name (String)

    screen_name

Returns:

  • (Hash)

    response



96
97
98
99
100
# File 'lib/pupil/friendships.rb', line 96

def follow(param)
  response = self.post("/1.1/friendships/create.json", {guess_parameter(param) => param})
  return false unless response
  User.new(response, @access_token)
end

#followers(name = @screen_name) ⇒ Object



35
36
37
38
39
40
# File 'lib/pupil/friendships.rb', line 35

def followers(name=@screen_name)
  name ||= self.profile.screen_name
  ids = self.followers_ids(name)
  users = self.lookup :users => ids
  return users
end

#followers_ids(name = @screen_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/pupil/friendships.rb', line 24

def followers_ids(name=@screen_name)
  name ||= self.profile.screen_name
  response = self.get("followers/ids.json", {guess_parameter(name) => name})
  return false unless response
  ids = []
  response["ids"].each do |element|
    ids << element
  end
  return ids
end

#friends(name = @screen_name) ⇒ Object Also known as: following



15
16
17
18
19
20
# File 'lib/pupil/friendships.rb', line 15

def friends(name=@screen_name)
  name ||= self.profile.screen_name
  ids = self.friends_ids(name)
  users = self.lookup :users => ids
  return users
end

#friends_ids(name = @screen_name) ⇒ Object Also known as: following_ids



2
3
4
5
6
7
8
9
10
11
# File 'lib/pupil/friendships.rb', line 2

def friends_ids(name=@screen_name)
  name ||= self.profile.screen_name
  response = self.get("friends/ids.json", {guess_parameter(name) => name})
  return false unless response
  ids = []
  response["ids"].each do |element|
    ids << element
  end
  return ids
end

#friendship?(src, dst) ⇒ Boolean Also known as: friendships?, relationship?, friendships_exists?

Check friendships

Parameters:

  • src (String)

    source screen_name

  • dst (String)

    destination screen_name

Returns:

  • (Boolean)

    return true if paired users have friendship, or ruturn false



72
73
74
75
76
77
78
79
80
81
# File 'lib/pupil/friendships.rb', line 72

def friendship?(src, dst)
  param = {"source_#{guess_parameter(src)}" => src, "target_#{guess_parameter(dst)}" => dst}
  response = self.get("friendships/show.json", param)
  return nil unless response
  if response["relationship"]["source"]["following"] == true && response["relationship"]["target"]["following"] == true then
    return true
  else
    return false
  end
end

#incoming(param = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/pupil/friendships.rb', line 58

def incoming(param={})
  response = self.get("friendships/incoming.json")
  return false unless response
  ids = []
  response["ids"].each do |element|
    ids << element
  end
  return ids
end

#lists(param = {}) ⇒ Hash

Returns lists.

Returns:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pupil/lists.rb', line 3

def lists(param={})
  if param[:contains]
    response = self.get("lists/memberships.json", param.reject{|p|p==:contains}.update(guess_parameter(param[:contains]) => param[:contains]))
    response = response["lists"]
  else
    response = self.get("lists/all.json", param)
  end
  return [] unless response
  lists = Array.new
  response.each do |list|
    lists << List.new(list, @access_token)
  end
  return lists
end

#lookup(opts, option = {}) ⇒ Object



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
# File 'lib/pupil/general.rb', line 2

def lookup(opts, option={})
  target, source = opts.to_a.first
  case target
  when :users
    users = Array.new
    source.each_slice(100) do |sliced|
      response = self.get("users/lookup.json", {guess_parameter(sliced[0]) => sliced.join(",")}.update(option))
      return false unless response
      response.each do |element|
        user = User.new(element, @access_token)
        users << user
      end
    end
    
    return users
  when :friendships
    fs = Array.new
    source.each_slice(100) do |sliced|
      response = self.get("friendships/lookup.json", {guess_parameter(sliced[0]) => sliced.join(",")}.update(option))
      return false unless response

      response.each do |element|
        fs << element
      end
    end
    
    return fs
  else
    raise ArgumentError, "#{target} is invalid parameter"
  end
end

#mentions(param = {}) ⇒ Array

Returns Mention.

Parameters:

  • param (Hash) (defaults to: {})

Returns:

  • (Array)

    Mention



26
27
28
29
30
31
32
33
34
35
# File 'lib/pupil/statuses.rb', line 26

def mentions(param={})
  response = self.get("statuses/mentions_timeline.json", param)
  return false unless response
  statuses = []
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#no_retweet_idsObject



42
43
44
45
46
# File 'lib/pupil/friendships.rb', line 42

def no_retweet_ids()
  response = self.get("friendships/no_retweet_ids.json")
  return false unless response
  return response
end

#oembed(param = {}) ⇒ Object



137
138
139
140
# File 'lib/pupil/statuses.rb', line 137

def oembed(param={})
  response = self.get("statuses/oembed.json", param)
  return response
end

#outgoing(param = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/pupil/friendships.rb', line 48

def outgoing(param={})
  response = self.get("friendships/outgoing.json")
  return false unless response
  ids = []
  response["ids"].each do |element|
    ids << element
  end
  return ids
end

#public_timeline(param = {}) ⇒ Array

Returning public timeline

Parameters:

  • param (Hash) (defaults to: {})

Returns:

  • (Array)

    Timeline



71
72
73
74
75
76
77
78
79
80
# File 'lib/pupil/statuses.rb', line 71

def public_timeline(param={})
  response = self.get("statuses/public_timeline.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#rate_limitHash

Rate limit statuses

Returns:

  • (Hash)

    Rate limit statuses



14
15
16
17
# File 'lib/pupil/account.rb', line 14

def rate_limit
  response = self.get("account/rate_limit_status.json")
  return response
end

#retweeted_by_me(param = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/pupil/statuses.rb', line 82

def retweeted_by_me(param={})
  response = self.get("statuses/retweeted_by_me.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#retweeted_by_user(param = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/pupil/statuses.rb', line 126

def retweeted_by_user(param={})
  response = self.get("statuses/retweeted_by_user.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#retweeted_to_me(param = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/pupil/statuses.rb', line 93

def retweeted_to_me(param={})
  response = self.get("statuses/retweeted_to_me.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#retweeted_to_user(param = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/pupil/statuses.rb', line 115

def retweeted_to_user(param={})
  response = self.get("statuses/retweeted_to_user.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#retweets_of_me(param = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/pupil/statuses.rb', line 104

def retweets_of_me(param={})
  response = self.get("statuses/retweets_of_me.json", param)
  return false unless response
  statuses = Array.new
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#search(keyword, option = {}) ⇒ Array

Returns Tweets.

Parameters:

  • keyword (String)

    Search word

  • option (Hash) (defaults to: {})

Returns:

  • (Array)

    Tweets



5
6
7
8
9
10
11
12
13
# File 'lib/pupil/search.rb', line 5

def search(keyword, option={})
  response = self.get("search/tweets.json", {:q => keyword}.update(option))
  statuses = Array.new
  response["statuses"].each do |element|
    status = Status.new element
    statuses << status
  end
  return statuses
end

#search_user(keyword, option = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/pupil/users.rb', line 9

def search_user(keyword, option={})
  response = self.get("users/search.json", {:q => keyword}.update(option))
  users = Array.new
  response.each do |element|
    user = User.new(element, @access_token)
    users << user
  end
  return users
end

#send_direct_message(sentence, opts) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/pupil/direct_messages.rb', line 32

def send_direct_message(sentence, opts)
  raise ArgumentError, ":to parameter not given" unless opts[:to]
  response = self.post("direct_messages/new.json", {:text => sentence, guess_parameter(opts[:to]) => opts[:to]})
  return false unless response
  response
end

#sent_direct_messages(param = {}) ⇒ Hash

Returning direct messages you sent

Parameters:

  • param (Hash) (defaults to: {})

Returns:

  • (Hash)

    directmessage you sent



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pupil/direct_messages.rb', line 20

def sent_direct_messages(param = {})
  response = self.get("direct_messages/sent.json", param)
  return false unless response
  directmessages = Array.new
  response.each do |element|
    dm = DirectMessage.new(element, @access_token)
    directmessages << dm
  end

  return directmessages
end

#status(status_id, param = {}) ⇒ Object



142
143
144
145
146
147
# File 'lib/pupil/statuses.rb', line 142

def status(status_id, param={})
  response = self.get("statuses/show/#{status_id}.json", param)
  return false unless response
  status = Status.new(response, @access_token)
  return status
end

#timeline(param = {}) ⇒ Array Also known as: home_timeline

Returns Timeline.

Parameters:

  • param (Hash) (defaults to: {})

Options Hash (param):

  • :count (Fixnum)

    Number of tweets

  • :since_id (Fixnum)
  • :max_id (Fixnum)
  • :page (Fixnum)
  • :trim_user (Symbol)
  • :include (Symbol)

    #=> [:rts]

  • :exclude (Symbol)

    #=> [:replies]

  • :contributor_details (Symbol)

Returns:

  • (Array)

    Timeline



12
13
14
15
16
17
18
19
20
21
# File 'lib/pupil/statuses.rb', line 12

def timeline(param={})
  response = self.get("statuses/home_timeline.json", param)
  return false unless response
  statuses = []
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#unblock(param) ⇒ Pupil::User

Returns response.

Parameters:

  • id (Fixnum)

    id

Returns:



15
16
17
18
19
20
21
22
# File 'lib/pupil/blocks.rb', line 15

def unblock(param)
  response = self.post("blocks/destroy.json", {guess_parameter(param) => param})
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

#unfollow(param) ⇒ Hash

Unfollow user for screen_name

Parameters:

  • name (String)

    screen_name

Returns:

  • (Hash)

    response



105
106
107
108
109
# File 'lib/pupil/friendships.rb', line 105

def unfollow(param)
  response = self.post("/1.1/friendships/destroy.json", {guess_parameter(param) => param})
  return false unless response
  User.new response
end

#update(status, irt = '') ⇒ Object Also known as: tweet



149
150
151
152
153
154
155
156
157
# File 'lib/pupil/statuses.rb', line 149

def update(status, irt='')
  response = self.post(
    "statuses/update.json",
    "status"=> status,
    "in_reply_to_status_id" => irt
  )
  return false unless response
  response
end

#update_friendships(target, param) ⇒ Object



87
88
89
90
91
# File 'lib/pupil/friendships.rb', line 87

def update_friendships(target, param)
  response = self.post("friendships/update.json", param)
  return false unless response
  return response
end

#update_profile(param) ⇒ Pupil::User

Update profile

Parameters:

Options Hash (param):

  • :name (String)
  • :url (String)
  • :location (String)
  • :description (String)
  • :colors (String)

    #=> :background

  • :colors (String)

    #=> :link

  • :colors (String)

    #=> :sidebar_border

  • :colors (String)

    #=> :sidebar_fill

  • :colors (String)

    #=> :text

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pupil/account.rb', line 38

def update_profile(param)
  if param.key? :colors
    opt = Hash.new
    opt.update({:profile_background_color => param[:colors][:background]}) if param[:colors][:background]
    opt.update({:profile_link_color => param[:colors][:link]}) if param[:colors][:link]
    opt.update({:profile_sidebar_border => param[:colors][:sidebar_border]}) if param[:colors][:sidebar_border]
    opt.update({:profile_sidebar_fill => param[:colors][:sidebar_fill]}) if param[:colors][:sidebar_fill]
    oot.update({:profile_text_color => param[:colors][:text]}) if param[:colors][:text]
    param.delete :colors
    response = self.post("account/update_profile_colors.json", opt)
    return User.new response if param.size <= 0
  end
  response2 = self.post("account/update_profile.json", param)
  return User.new response2
end

#user(target, option = {}) ⇒ Object



2
3
4
5
6
7
# File 'lib/pupil/users.rb', line 2

def user(target, option={})
  response = self.get("users/show.json", {guess_parameter(target) => target}.update(option))
  return false unless response
  user = User.new(response, @access_token)
  return user
end

#user_timeline(target, option = {}) ⇒ Array Also known as: timeline_for

Returning user timeline

Examples:

twitter = Pupil.new PUPIL_KEY
twitter.user_timeline(:screen_name => 'o_ame', :exclude => :replies).each do |status|
  puts "#{status.user.screen_name}: #{status.text}"
end

Parameters:

Returns:

  • (Array)

    timeline



55
56
57
58
59
60
61
62
63
64
# File 'lib/pupil/statuses.rb', line 55

def user_timeline(target, option={})
  response = self.get("statuses/user_timeline.json", {guess_parameter(target) => target}.update(option))
  return false unless response
  statuses = []
  response.each do |element|
    status = Status.new(element, @access_token)
    statuses << status
  end
  return statuses
end

#verify_credentialsPupil::User Also known as: profile

Verify credentials

Returns:



4
5
6
7
8
# File 'lib/pupil/account.rb', line 4

def verify_credentials
  response = self.get("account/verify_credentials.json")
  user = User.new response
  return user
end