pupil 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ class Pupil
2
+ # Verify credentials
3
+ # @return [Pupil::User] User credentials
4
+ def verify_credentials
5
+ response = self.get("/account/verify_credentials.json")
6
+ user = User.new response
7
+ return user
8
+ end
9
+
10
+ # Rate limit statuses
11
+ # @return [Hash] Rate limit statuses
12
+ def rate_limit
13
+ response = self.get("/account/rate_limit_status.json")
14
+ return response
15
+ end
16
+
17
+ # End oauth session
18
+ # @return [Hash] Result
19
+ def end_session
20
+ response = self.post("/account/end_session.json")
21
+ return response
22
+ end
23
+
24
+ # Update profile
25
+ # @return [Pupil::User] Updated profile
26
+ # @param [Hash] param
27
+ # @option param [String] :name
28
+ # @option param [String] :url
29
+ # @option param [String] :location
30
+ # @option param [String] :description
31
+ # @option param [String] :colors #=> :background
32
+ # @option param [String] :colors #=> :link
33
+ # @option param [String] :colors #=> :sidebar_border
34
+ # @option param [String] :colors #=> :sidebar_fill
35
+ # @option param [String] :colors #=> :text
36
+ def update_profile param
37
+ if param.key? :colors
38
+ opt = Hash.new
39
+ opt.update({:profile_background_color => param[:colors][:background]}) if param[:colors][:background]
40
+ opt.update({:profile_link_color => param[:colors][:link]}) if param[:colors][:link]
41
+ opt.update({:profile_sidebar_border => param[:colors][:sidebar_border]}) if param[:colors][:sidebar_border]
42
+ opt.update({:profile_sidebar_fill => param[:colors][:sidebar_fill]}) if param[:colors][:sidebar_fill]
43
+ oot.update({:profile_text_color => param[:colors][:text]}) if param[:colors][:text]
44
+ param.delete :colors
45
+ response = self.post("/account/update_profile_colors.json", opt)
46
+ return User.new response if param.size <= 0
47
+ end
48
+ response2 = self.post("/account/update_profile.json", param)
49
+ return User.new response2
50
+ end
51
+ end
@@ -0,0 +1,83 @@
1
+ class Pupil
2
+ attr_reader :screen_name
3
+ class UnsupportedParameter < StandardError ; end
4
+ class NetworkError < StandardError ; end
5
+
6
+ TWITTER_API_URL = "http://api.twitter.com"
7
+
8
+ # @param [Hash] pupil_key
9
+ def initialize key
10
+ @screen_name = key[:screen_name]
11
+ @client = nil
12
+ @config = nil
13
+
14
+ @consumer = OAuth::Consumer.new(
15
+ key[:consumer_key],
16
+ key[:consumer_secret],
17
+ :site => TWITTER_API_URL
18
+ )
19
+ @access_token = OAuth::AccessToken.new(
20
+ @consumer,
21
+ key[:access_token],
22
+ key[:access_token_secret]
23
+ )
24
+ end
25
+
26
+ # @param [Hash] parameter
27
+ # @return [String] URL Serialized parameters
28
+ def self.param_serializer parameter
29
+ return "" unless parameter.class == Hash
30
+ ant = Hash.new
31
+ parameter.each do |key, value|
32
+ case key.to_sym
33
+ when :include
34
+ if value.class == String || Symbol
35
+ ant[:"include_#{value}"] = :true
36
+ break
37
+ end
38
+
39
+ value.each do |element|
40
+ raise UnsupportedParameter, "include_entities is not supported." if element.to_sym == :entities
41
+ ant[:"include_#{element}"] = :true
42
+ end
43
+ when :exclude
44
+ if value.class == String || Symbol
45
+ ant[:"exclude_#{value}"] = :true
46
+ break
47
+ end
48
+
49
+ value.each do |element|
50
+ ant[:"exclude_#{element}"] = :true
51
+ end
52
+ else
53
+ ant[key.to_sym] = value.to_s
54
+ end
55
+ end
56
+ param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
57
+ return param ? param : ""
58
+ end
59
+
60
+ def param_serializer parameter
61
+ Pupil.param_serializer parameter
62
+ end
63
+
64
+ def get url, param={}
65
+ param_s = param_serializer(param)
66
+ begin
67
+ response = @access_token.get(url+param_s).body
68
+ rescue => vars
69
+ raise NetworkError, vars
70
+ end
71
+ return JSON.parse(response)
72
+ end
73
+
74
+ def post url, param={}
75
+ param_s = param_serializer(param)
76
+ begin
77
+ response = @access_token.post(url+param_s).body
78
+ rescue => vars
79
+ raise NetworkError, vars
80
+ end
81
+ return JSON.parse(response)
82
+ end
83
+ end
@@ -0,0 +1,45 @@
1
+ class Pupil
2
+ # @param [Fixnum] id id
3
+ # @return [Hash] response
4
+ def block param
5
+ case param.keys[0].to_sym
6
+ when :screen_name
7
+ response = self.post("/blocks/create.json", {:screen_name => param.values[0]})
8
+ when :id
9
+ response = self.post("/blocks/create.json", {:user_id => param.values[0]})
10
+ end
11
+
12
+ if response.class == Hash && response["id"]
13
+ return User.new response
14
+ end
15
+ return false
16
+ end
17
+
18
+ # @param [Fixnum] id id
19
+ # @return [Hash] response
20
+ def unblock param
21
+ case param.keys[0].to_sym
22
+ when :screen_name
23
+ response = self.post("/blocks/destroy.json", {:screen_name => param.values[0]})
24
+ when :id
25
+ response = self.post("/blocks/destroy.json", {:user_id => param.values[0]})
26
+ end
27
+
28
+ if response.class == Hash && response["id"]
29
+ return User.new response
30
+ end
31
+ return false
32
+ end
33
+
34
+ # @return [Hash] list of blocking users
35
+ def blocking
36
+ response = self.get("/blocks/blocking.json")
37
+ p response
38
+ users = Array.new
39
+ response["users"].each do |element|
40
+ user = User.new element
41
+ users << user
42
+ end
43
+ return users
44
+ end
45
+ end
@@ -0,0 +1,57 @@
1
+ class Pupil
2
+ # Returning direct messages
3
+ # @param [Hash] param
4
+ # @return [Hash] directmessages
5
+ def dm(param = {})
6
+ param_s = param_serializer(param)
7
+ begin
8
+ response = @access_token.get("http://api.twitter.com/1/direct_messages.xml"+param_s)
9
+ rescue
10
+ return false
11
+ end
12
+ doc = REXML::Document.new(response.body)
13
+ return false if doc.is_error?
14
+ directmessages = Array.new
15
+
16
+ doc.get_elements('/direct-messages/direct_message').each{|element|
17
+ dm = DirectMessage.new(element)
18
+ directmessages << dm
19
+ }
20
+
21
+ return directmessages
22
+ end
23
+
24
+ # Returning direct messages you sent
25
+ # @param [Hash] param
26
+ # @return [Hash] directmessage you sent
27
+ def dm_sent(param = {})
28
+ param_s = param_serializer(param)
29
+ begin
30
+ response = @access_token.get("http://api.twitter.com/1/direct_messages/sent.xml"+param_s)
31
+ rescue
32
+ return false
33
+ end
34
+ doc = REXML::Document.new(response.body)
35
+ return false if doc.is_error?
36
+ directmessages = Array.new
37
+
38
+ doc.get_elements('/direct-messages/direct_message').each{|element|
39
+ dm = DirectMessage.new(element)
40
+ directmessages << dm
41
+ }
42
+
43
+ return directmessages
44
+ end
45
+
46
+ # Delete direct message
47
+ # @param [Fixnum] dm_id message id that you want to delete
48
+ # @return [Hash] response
49
+ def dm_destroy(dm_id)
50
+ begin
51
+ response = @access_token.post("http://api.twitter.com/1/direct_messages/destroy/#{dm_id}.xml")
52
+ rescue
53
+ return false
54
+ end
55
+ return response
56
+ end
57
+ end
@@ -0,0 +1,70 @@
1
+ class Pupil
2
+ def friends_ids name
3
+ response = self.get("/friends/ids/#{name}.json")
4
+ ids = Array.new
5
+ response.each do |element|
6
+ ids << element
7
+ end
8
+ return ids
9
+ end
10
+
11
+ def followers_ids name=@screen_name
12
+ response = self.get("/1/followers/ids/#{name}.json")
13
+ ids = Array.new
14
+ response.each do |element|
15
+ ids << element
16
+ end
17
+ return ids
18
+ end
19
+
20
+ # Check friendships
21
+ # @param [String] src source screen_name
22
+ # @param [String] dst destination screen_name
23
+ # @return [Boolean] return true if paired users have friendship, or ruturn false
24
+ def friendship?(src, dst)
25
+ param = {:source_screen_name => src, :target_screen_name => dst}
26
+ response = self.get("/friendships/show.json", param)
27
+ if response["relationship"]["source"]["following"] == true && response["relationship"]["target"]["following"] == true then
28
+ return true
29
+ else
30
+ return false
31
+ end
32
+ end
33
+
34
+ alias_method "relationship?", "friendship?"
35
+ alias_method "friendships_exists?", "friendship?"
36
+
37
+ # Follow user for screen_name
38
+ # @param [String] name screen_name
39
+ # @return [Hash] response
40
+ def follow param
41
+ case param.keys[0].to_sym
42
+ when :screen_name
43
+ response = self.post("/friendships/create.json", {:screen_name => param.values[0]})
44
+ when :id
45
+ response = self.post("/friendships/create.json", {:user_id => param.values[0]})
46
+ end
47
+
48
+ if response.class == Hash && response["id"]
49
+ return User.new response
50
+ end
51
+ return false
52
+ end
53
+
54
+ # Unfollow user for screen_name
55
+ # @param [String] name screen_name
56
+ # @return [Hash] response
57
+ def unfollow param
58
+ case param.keys[0].to_sym
59
+ when :screen_name
60
+ response = self.post("/friendships/destroy.json", {:screen_name => param.values[0]})
61
+ when :id
62
+ response = self.post("/friendships/destroy.json", {:user_id => param.values[0]})
63
+ end
64
+
65
+ if response.class == Hash && response["id"]
66
+ return User.new response
67
+ end
68
+ return false
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+ class Pupil
2
+ # @param [Fixnum] id list id
3
+ # @param [String] ids id comma separated
4
+ # @return [Hash] response
5
+ def addlist(listid,ids)
6
+ response = @access_token.post("http://api.twitter.com/1/#{@username}/#{listid}/create_all.xml?user_id=#{ids}")
7
+ return response
8
+ end
9
+
10
+ # @return [Hash] lists
11
+ def lists
12
+ response = @access_token.get("http://api.twitter.com/1/#{@username}/lists.xml")
13
+ doc = REXML::Document.new(response.body)
14
+ return false if doc.is_error?
15
+ lists = Array.new
16
+ doc.get_elements('/lists_list/lists/list').each{|element|
17
+ list = List.new(element)
18
+ lists << list
19
+ }
20
+ return lists
21
+ end
22
+
23
+ def lists_member_create(listid,id)
24
+ begin
25
+ response = @access_token.post("http://api.twitter.com/1/#{@username}/#{listid}/members.xml?id=#{id}")
26
+ rescue
27
+ return false
28
+ else
29
+ return response
30
+ end
31
+ end
32
+
33
+
34
+ end
@@ -0,0 +1,220 @@
1
+ class Pupil
2
+ class User
3
+ attr_reader :contributors_enabled
4
+ attr_reader :created_at
5
+ attr_reader :default_profile
6
+ attr_reader :default_profile_image
7
+ attr_reader :description
8
+ attr_reader :favourites_count
9
+ attr_reader :follow_request_sent
10
+ attr_reader :followers_count
11
+ attr_reader :following
12
+ attr_reader :friends_count
13
+ attr_reader :geo_enabled
14
+ attr_reader :id
15
+ attr_reader :id_str
16
+ attr_reader :is_translator
17
+ attr_reader :lang
18
+ attr_reader :listed_count
19
+ attr_reader :location
20
+ attr_reader :name
21
+ attr_reader :notifications
22
+ attr_reader :profile_background_color
23
+ attr_reader :profile_background_image_url
24
+ attr_reader :profile_background_image_url_https
25
+ attr_reader :profile_background_tile
26
+ attr_reader :profile_image_url
27
+ attr_reader :profile_image_url_https
28
+ attr_reader :profile_link_color
29
+ attr_reader :profile_sidebar_border_color
30
+ attr_reader :profile_sidebar_fill_color
31
+ attr_reader :profile_text_color
32
+ attr_reader :profile_use_background_image
33
+ attr_reader :protected
34
+ attr_reader :screen_name
35
+ attr_reader :show_all_inline_media
36
+ attr_reader :status
37
+ attr_reader :statuses_count
38
+ attr_reader :time_zone
39
+ attr_reader :url
40
+ attr_reader :utc_offset
41
+ attr_reader :verified
42
+
43
+ def initialize j
44
+ @contributors_enabled = j["contributors_enabled"] rescue nil
45
+ @created_at = j["created_at"]
46
+ @default_profile = j["default_profile"]
47
+ @default_profile_image = j["default_profile_image"]
48
+ @description = j["description"]
49
+ @favourites_count = j["favourites_count"]
50
+ @follow_request_sent = j["follow_request_sent"]
51
+ @followers_count = j["followers_count"]
52
+ @following = j["following"]
53
+ @friends_count = j["friends_count"]
54
+ @geo_enabled = j["geo_enabled"]
55
+ @id = j["id"]
56
+ @id_str = j["id_str"]
57
+ @is_translator = j["is_translator"]
58
+ @lang = j["lang"]
59
+ @listed_count = j["listed_count"]
60
+ @location = j["location"]
61
+ @name = j["name"]
62
+ @notifications = j["notifications"]
63
+ @profile_background_color = j["profile_background_color"]
64
+ @profile_background_image_url = j["profile_background_image_url"]
65
+ @profile_background_image_url_https = j["profile_background_image_url_https"]
66
+ @profile_background_tile = j["profile_background_tile"]
67
+ @profile_image_url = j["profile_image_url"]
68
+ @profile_image_url_https = j["profile_image_url_https"]
69
+ @profile_link_color = j["profile_link_color"]
70
+ @profile_sidebar_border_color = j["profile_sidebar_border_color"]
71
+ @profile_sidebar_fill_color = j["profile_sidebar_fill_color"]
72
+ @profile_text_color = j["profile_text_color"]
73
+ @profile_use_background_image = j["profile_use_background_image"]
74
+ @protected = j["protected"]
75
+ @screen_name = j["screen_name"]
76
+ @show_all_inline_media = j["show_all_inline_media"]
77
+ @status = Status.new j["status"]
78
+ @statuses_count = j["statuses_count"]
79
+ @time_zone = j["time_zone"]
80
+ @url = j["url"]
81
+ @utc_offset = j["utc_offset"]
82
+ @verified = j["verified"]
83
+ end
84
+ end
85
+
86
+ class URL
87
+ attr_reader :url
88
+ attr_reader :expanded_url
89
+ attr_reader :start
90
+ attr_reader :end
91
+
92
+ def initialize(element)
93
+ @url = element.elements['url'].text()
94
+ @expanded_url = element.elements['expanded_url'].text()
95
+ @start = element.attributes['start']
96
+ @end = element.attributes['end']
97
+ end
98
+ end
99
+
100
+ class Status
101
+ attr_reader :contributors
102
+ attr_reader :coordinates
103
+ attr_reader :created_at
104
+ attr_reader :favorited
105
+ attr_reader :geo
106
+ attr_reader :id
107
+ attr_reader :id_str
108
+ attr_reader :in_reply_to_screen_name
109
+ attr_reader :in_reply_to_status_id
110
+ attr_reader :in_reply_to_status_id_str
111
+ attr_reader :in_reply_to_user_id
112
+ attr_reader :in_reply_to_user_id_str
113
+ attr_reader :place
114
+ attr_reader :retweet_count
115
+ attr_reader :retweeted
116
+ attr_reader :source
117
+ attr_reader :text
118
+ attr_reader :truncated
119
+ attr_reader :user
120
+
121
+ def initialize j
122
+ @contributors = j["contributors"]
123
+ @coordinates = j["coordinates"]
124
+ @created_at = j["created_at"]
125
+ @favorited = j["favorited"]
126
+ @geo = j["geo"]
127
+ @id = j["id"]
128
+ @id_str = j["id_str"]
129
+ @in_reply_to_screen_name = j["in_reply_to_screen_name"]
130
+ @in_reply_to_status_id = j["in_reply_to_status_id"]
131
+ @in_reply_to_status_id_str = j["in_reply_to_status_id_str"]
132
+ @in_reply_to_user_id = j["in_reply_to_user_id"]
133
+ @in_reply_to_user_id_str = j["in_reply_to_user_id_str"]
134
+ @place = j["place"]
135
+ @retweet_count = j["retweet_count"]
136
+ @retweeted = j["retweeted"]
137
+ j["source"] =~ /href=\"(.+?)\".+?>(.+?)</
138
+ @source = {:url => $1, :name => $2}
139
+ @text = j["text"]
140
+ @truncated = j["truncated"]
141
+ @user = User.new j["user"] rescue nil
142
+ end
143
+ end
144
+
145
+ class List
146
+ attr_reader :id
147
+ attr_reader :name
148
+ attr_reader :full_name
149
+ attr_reader :slug
150
+ attr_reader :description
151
+ attr_reader :subscriber_count
152
+ attr_reader :member_count
153
+ attr_reader :uri
154
+ attr_reader :following
155
+ attr_reader :mode
156
+ attr_reader :user
157
+
158
+ def initialize(element)
159
+ @id = element.elements['id'].text()
160
+ @name = element.elements['name'].text()
161
+ @full_name = element.elements['full_name'].text()
162
+ @slug = element.elements['slug'].text()
163
+ @description = element.elements['description'].text()
164
+ @subscriber_count = element.elements['subscriber_count'].text()
165
+ @member_count = element.elements['member_count'].text()
166
+ @uri = element.elements['uri'].text()
167
+ @following = element.elements['following'].text()
168
+ @mode = element.elements['mode'].text()
169
+ @user = User.new(element.elements['user'])
170
+ end
171
+ end
172
+
173
+ class Entities
174
+ attr_reader :user_mentions
175
+ attr_reader :urls
176
+ attr_reader :hashtags
177
+
178
+ def initialize(element)
179
+ @user_mentions = UserMention.new(element.elements['user_mention'])
180
+ @urls = URL.new(element.elements['urls'])
181
+ @hashtags = Hashtag.new(element.elements['hashtags'])
182
+ end
183
+ end
184
+
185
+ class Hashtag
186
+ attr_reader :text
187
+ attr_reader :start
188
+ attr_reader :end
189
+
190
+ def initialize(element)
191
+ @text = element.elements['text'].text()
192
+ @start = element.attributes['start']
193
+ @end = element.attributes['end']
194
+ end
195
+ end
196
+
197
+ class DirectMessage
198
+ attr_reader :id
199
+ attr_reader :sender_id
200
+ attr_reader :text
201
+ attr_reader :recipient_id
202
+ attr_reader :created_at
203
+ attr_reader :sender_screen_name
204
+ attr_reader :recipient_screen_name
205
+ attr_reader :sender
206
+ attr_reader :recipient
207
+
208
+ def initialize(element)
209
+ @id = element.elements['id'].text()
210
+ @sender_id = element.elements['sender_id'].text()
211
+ @text = element.elements['text'].text()
212
+ @recipient_id = element.elements['recipient_id'].text()
213
+ @created_at = element.elements['created_at'].text()
214
+ @sender_screen_name = element.elements['sender_screen_name'].text()
215
+ @recipient_screen_name= element.elements['recipient_screen_name'].text()
216
+ @sender = User.new(element.elements['sender'])
217
+ @recipient = User.new(element.elements['recipient'])
218
+ end
219
+ end
220
+ end