Class: Pupil
- Inherits:
-
Object
- Object
- Pupil
- 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)
-
- (Object) screen_name
readonly
Returns the value of attribute screen_name.
Class Method Summary (collapse)
-
+ (String) param_serializer(parameter)
URL Serialized parameters.
Instance Method Summary (collapse)
-
- (Hash) addlist(listid, ids)
Response.
-
- (Pupil::User) block(param)
Response.
-
- (Array) blocking
List of blocking users.
- - (Object) destroy(status_id)
-
- (Hash) dm(param = {})
Returning direct messages.
-
- (Hash) dm_destroy(dm_id)
Delete direct message.
-
- (Hash) dm_sent(param = {})
Returning direct messages you sent.
-
- (Hash) end_session
End oauth session.
-
- (Hash) follow(param)
Follow user for screen_name.
- - (Object) followers_ids(name = @screen_name)
- - (Object) friends_ids(name)
-
- (Boolean) friendship?(src, dst)
(also: #relationship?, #friendships_exists?)
Check friendships.
- - (Object) get(url, param = {})
-
- (Array) home_timeline(param = {})
Timeline.
-
- (Pupil) initialize(key)
constructor
A new instance of Pupil.
-
- (Hash) lists
Lists.
- - (Object) lists_member_create(listid, id)
- - (Object) lookup(param = {})
-
- (Hash) mentions(param = {})
Mention.
- - (Object) param_serializer(parameter)
- - (Object) post(url, param = {})
-
- (Hash) rate_limit
Rate limit statuses.
- - (Object) show_status(status_id)
-
- (Pupil::User) unblock(param)
Response.
-
- (Hash) unfollow(param)
Unfollow user for screen_name.
- - (Object) update(status, irt = '') (also: #tweet)
-
- (Pupil::User) update_profile(param)
Update profile.
-
- (Hash) user_timeline(param = {})
Returning user timeline.
-
- (Pupil::User) verify_credentials
Verify credentials.
Constructor Details
- (Pupil) initialize(key)
A new instance of Pupil
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
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
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
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
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
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? = Array.new doc.get_elements('/direct-messages/direct_message').each{|element| dm = DirectMessage.new(element) << dm } return end |
- (Hash) dm_destroy(dm_id)
Delete direct message
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
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? = Array.new doc.get_elements('/direct-messages/direct_message').each{|element| dm = DirectMessage.new(element) << dm } return end |
- (Hash) end_session
End oauth session
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
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pupil/friendships.rb', line 40 def follow 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
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
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
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
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
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
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
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
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
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
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 |