Class: Pupil

Inherits:
Object
  • Object
show all
Defined in:
lib/pupil/base.rb,
lib/pupil/users.rb,
lib/pupil/lists.rb,
lib/pupil/blocks.rb,
lib/pupil/account.rb,
lib/pupil/schemes.rb,
lib/pupil/statuses.rb,
lib/pupil/stream/base.rb,
lib/pupil/friendships.rb,
lib/pupil/keygen/base.rb,
lib/pupil/direct_messages.rb

Defined Under Namespace

Classes: DirectMessage, Entities, Hashtag, Keygen, List, NetworkError, Status, Stream, URL, UnsupportedParameter, User

Constant Summary

TWITTER_API_URL =
"http://api.twitter.com"

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Pupil) initialize(key)

A new instance of Pupil

Parameters:

  • pupil_key (Hash)


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

def initialize key
  @screen_name = key[:screen_name]
  @client = nil
  @config = nil
  
  @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]
  )
end

Instance Attribute Details

- (Object) screen_name (readonly)

Returns the value of attribute screen_name



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

def screen_name
  @screen_name
end

Class Method Details

+ (String) param_serializer(parameter)

URL Serialized parameters

Parameters:

  • parameter (Hash)

Returns:

  • (String)

    URL Serialized parameters



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

def self.param_serializer parameter
  return "" unless parameter.class == Hash
  ant = Hash.new
  parameter.each do |key, value|
    case key.to_sym
    when :include
      if value.class == String || Symbol
        ant[:include_#{value}"] = :true
        break
      end

      value.each do |element|
        raise UnsupportedParameter, "include_entities is not supported." if element.to_sym == :entities
        ant[:include_#{element}"] = :true
      end
    when :exclude
      if value.class == String || Symbol
        ant[:exclude_#{value}"] = :true
        break
      end

      value.each do |element|
        ant[:exclude_#{element}"] = :true
      end
    else
      ant[key.to_sym] = value.to_s
    end
  end
  param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
  return param ? param : ""
end

Instance Method Details

- (Hash) addlist(listid, ids)

Response

Parameters:

  • id (Fixnum)

    list id

  • ids (String)

    id comma separated

Returns:

  • (Hash)

    response



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

def addlist(listid,ids)
  response = @access_token.post("http://api.twitter.com/1/#{@username}/#{listid}/create_all.xml?user_id=#{ids}")
  return response
end

- (Pupil::User) block(param)

Response

Parameters:

  • id (Fixnum)

    id

Returns:



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

def block param
  case param.keys[0].to_sym
  when :screen_name
    response = self.post("/blocks/create.json", {:screen_name => param.values[0]})
  when :id
    response = self.post("/blocks/create.json", {:user_id => param.values[0]})
  end
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

- (Array) blocking

List of blocking users

Returns:

  • (Array)

    list of blocking users



35
36
37
38
39
40
41
42
43
# File 'lib/pupil/blocks.rb', line 35

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

- (Object) destroy(status_id)



80
81
82
83
# File 'lib/pupil/statuses.rb', line 80

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

- (Hash) dm(param = {})

Returning direct messages

Parameters:

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

Returns:

  • (Hash)

    directmessages



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pupil/direct_messages.rb', line 5

def dm(param = {})
  param_s = param_serializer(param)
  begin
    response = @access_token.get("http://api.twitter.com/1/direct_messages.xml"+param_s)
  rescue
    return false
  end
  doc = REXML::Document.new(response.body)
  return false if doc.is_error?
  directmessages = Array.new

  doc.get_elements('/direct-messages/direct_message').each{|element|
    dm = DirectMessage.new(element)
    directmessages << dm
  }

  return directmessages
end

- (Hash) dm_destroy(dm_id)

Delete direct message

Parameters:

  • dm_id (Fixnum)

    message id that you want to delete

Returns:

  • (Hash)

    response



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

def dm_destroy(dm_id)
  begin
    response = @access_token.post("http://api.twitter.com/1/direct_messages/destroy/#{dm_id}.xml")
  rescue
    return false
  end
  return response
end

- (Hash) dm_sent(param = {})

Returning direct messages you sent

Parameters:

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

Returns:

  • (Hash)

    directmessage you sent



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pupil/direct_messages.rb', line 27

def dm_sent(param = {})
  param_s = param_serializer(param)
  begin
    response = @access_token.get("http://api.twitter.com/1/direct_messages/sent.xml"+param_s)
  rescue
    return false
  end
  doc = REXML::Document.new(response.body)
  return false if doc.is_error?
  directmessages = Array.new

  doc.get_elements('/direct-messages/direct_message').each{|element|
    dm = DirectMessage.new(element)
    directmessages << dm
  }

  return directmessages
end

- (Hash) end_session

End oauth session

Returns:

  • (Hash)

    Result



19
20
21
22
# File 'lib/pupil/account.rb', line 19

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

- (Hash) follow(param)

Follow user for screen_name

Parameters:

  • name (String)

    screen_name

Returns:

  • (Hash)

    response



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

def  param
  case param.keys[0].to_sym
  when :screen_name
    response = self.post("/friendships/create.json", {:screen_name => param.values[0]})
  when :id
    response = self.post("/friendships/create.json", {:user_id => param.values[0]})
  end
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

- (Object) followers_ids(name = @screen_name)



11
12
13
14
15
16
17
18
# File 'lib/pupil/friendships.rb', line 11

def followers_ids name=@screen_name
  response = self.get("/1/followers/ids/#{name}.json")
  ids = Array.new
  response.each do |element|
    ids << element
  end
  return ids
end

- (Object) friends_ids(name)



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

def friends_ids name
  response = self.get("/friends/ids/#{name}.json")
  ids = Array.new
  response.each do |element|
    ids << element
  end
  return ids
end

- (Boolean) friendship?(src, dst) Also known as: 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



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

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

- (Object) get(url, param = {})



70
71
72
73
74
75
76
77
78
# File 'lib/pupil/base.rb', line 70

def get url, param={}
  param_s = param_serializer(param)
  begin
    response = @access_token.get(url+param_s).body
  rescue => vars
    raise NetworkError, vars
  end
  return JSON.parse(response)
end

- (Array) home_timeline(param = {})

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

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

- (Hash) lists

Lists

Returns:

  • (Hash)

    lists



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

def lists
  response = @access_token.get("http://api.twitter.com/1/#{@username}/lists.xml")
  doc = REXML::Document.new(response.body)
  return false if doc.is_error?
  lists = Array.new
  doc.get_elements('/lists_list/lists/list').each{|element|
    list = List.new(element)
    lists << list
  }
  return lists
end

- (Object) lists_member_create(listid, id)



23
24
25
26
27
28
29
30
31
# File 'lib/pupil/lists.rb', line 23

def lists_member_create(listid,id)
  begin
    response = @access_token.post("http://api.twitter.com/1/#{@username}/#{listid}/members.xml?id=#{id}")
  rescue
    return false
  else
    return response
  end
end

- (Object) lookup(param = {})



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

def lookup param={}
  response = self.get("/users/lookup.json", param)
  users = Array.new
  response.each do |element|
    user = User.new element
    users << user
  end
  return users
end

- (Hash) mentions(param = {})

Mention

Parameters:

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

Returns:

  • (Hash)

    mention



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

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

- (Object) param_serializer(parameter)



66
67
68
# File 'lib/pupil/base.rb', line 66

def param_serializer parameter
  Pupil.param_serializer parameter
end

- (Object) post(url, param = {})



80
81
82
83
84
85
86
87
88
# File 'lib/pupil/base.rb', line 80

def post url, param={}
  param_s = param_serializer(param)
  begin
    response = @access_token.post(url+param_s).body
  rescue => vars
    raise NetworkError, vars
  end
  return JSON.parse(response)
end

- (Hash) rate_limit

Rate limit statuses

Returns:

  • (Hash)

    Rate limit statuses



12
13
14
15
# File 'lib/pupil/account.rb', line 12

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

- (Object) show_status(status_id)



62
63
64
65
66
67
# File 'lib/pupil/statuses.rb', line 62

def show_status status_id
  response = @access_token.get("/statuses/show/#{status_id}.json").body
  return response
  status = Status.new response
  return status
end

- (Pupil::User) unblock(param)

Response

Parameters:

  • id (Fixnum)

    id

Returns:



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

def unblock param
  case param.keys[0].to_sym
  when :screen_name
    response = self.post("/blocks/destroy.json", {:screen_name => param.values[0]})
  when :id
    response = self.post("/blocks/destroy.json", {:user_id => param.values[0]})
  end
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

- (Hash) unfollow(param)

Unfollow user for screen_name

Parameters:

  • name (String)

    screen_name

Returns:

  • (Hash)

    response



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

def unfollow param
  case param.keys[0].to_sym
  when :screen_name
    response = self.post("/friendships/destroy.json", {:screen_name => param.values[0]})
  when :id
    response = self.post("/friendships/destroy.json", {:user_id => param.values[0]})
  end
  
  if response.class == Hash && response["id"]
    return User.new response
  end
  return false
end

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



69
70
71
72
73
74
75
76
# File 'lib/pupil/statuses.rb', line 69

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

- (Pupil::User) update_profile(param)

Update profile

Parameters:

  • param (Hash)

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:



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

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

- (Hash) user_timeline(param = {})

Returning user timeline

Examples:

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

Parameters:

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

Options Hash (param):

  • :user_id (Fixnum)

    The ID of user

  • :screen_name (String)

    The Screen name of user

  • :since_id (Fixnum)
  • :max_id (Fixnum)
  • :count (Fixnum)
  • :page (Fixnum)

    Specifies

  • :trim_user (Symbol)
  • :include (Symbol)

    #=> [:rts]

  • :exclude (Symbol)

    #=> [:replies]

  • :contributor_details (Symbol)

Returns:

  • (Hash)

    timeline



52
53
54
55
56
57
58
59
60
# File 'lib/pupil/statuses.rb', line 52

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

- (Pupil::User) verify_credentials

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