pupil 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,4 +1,20 @@
1
- 0.1.5 / Unreleased
1
+ 0.2.0
2
+ ----------------
3
+
4
+ * Fixed bug what :include(and :exclude) parameter had not worked.
5
+ * New form are supported.
6
+ Classic form:
7
+ pupil = Pupil.new(...)
8
+ pupil.destroy(status_id) #=> Delete tweet
9
+ New one:
10
+ status = timeline.first
11
+ status.destroy
12
+ * Mutable scheme are supported. When Twitter REST API is changed, Schemes correspond to it automatically.
13
+ * New ways to distinguish screen_name from user_id.
14
+ If methods catch parameter of number, recognize it as screen_name.
15
+ * Almost Lists API are supported.
16
+
17
+ 0.1.5
2
18
  ----------------
3
19
 
4
20
  * Pupil::Stream Error handling(Pupil::Stream::StreamError) are rejected.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
data/lib/pupil.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  $LOAD_PATH << File.dirname(File.expand_path(__FILE__)) if RUBY_VERSION >= "1.9.0"
4
+ require "pupil/essentials"
4
5
  require "pupil/base"
5
6
  require "pupil/schemes"
6
7
  require "pupil/account"
data/lib/pupil/base.rb CHANGED
@@ -6,7 +6,6 @@ require "json"
6
6
 
7
7
  class Pupil
8
8
  attr_reader :screen_name
9
- class UnsupportedParameter < StandardError ; end
10
9
  class NetworkError < StandardError ; end
11
10
 
12
11
  TWITTER_API_URL = "http://api.twitter.com"
@@ -14,74 +13,18 @@ class Pupil
14
13
  # @param [Hash] pupil_key
15
14
  def initialize key
16
15
  @screen_name = key[:screen_name]
17
-
16
+
18
17
  @consumer = OAuth::Consumer.new(
19
- key[:consumer_key],
20
- key[:consumer_secret],
21
- :site => TWITTER_API_URL
18
+ key[:consumer_key],
19
+ key[:consumer_secret],
20
+ :site => TWITTER_API_URL
22
21
  )
23
22
  @access_token = OAuth::AccessToken.new(
24
- @consumer,
25
- key[:access_token],
26
- key[:access_token_secret]
23
+ @consumer,
24
+ key[:access_token],
25
+ key[:access_token_secret]
27
26
  )
28
27
  end
29
-
30
- # @param [Hash] parameter
31
- # @return [String] URL Serialized parameters
32
- def self.param_serializer parameter
33
- return "" unless parameter.class == Hash
34
- ant = Hash.new
35
- parameter.each do |key, value|
36
- case key.to_sym
37
- when :include
38
- if value.class == String || Symbol
39
- ant[:"include_#{value}"] = :true
40
- break
41
- end
42
-
43
- value.each do |element|
44
- raise UnsupportedParameter, "include_entities is not supported." if element.to_sym == :entities
45
- ant[:"include_#{element}"] = :true
46
- end
47
- when :exclude
48
- if value.class == String || Symbol
49
- ant[:"exclude_#{value}"] = :true
50
- break
51
- end
52
-
53
- value.each do |element|
54
- ant[:"exclude_#{element}"] = :true
55
- end
56
- else
57
- ant[key.to_sym] = value.to_s
58
- end
59
- end
60
- param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
61
- return param ? param : ""
62
- end
63
28
 
64
- def param_serializer parameter
65
- Pupil.param_serializer parameter
66
- end
67
-
68
- def get url, param={}
69
- param_s = param_serializer(param)
70
- begin
71
- response = @access_token.get(url+param_s).body
72
- rescue => vars
73
- raise NetworkError, vars
74
- end
75
- return JSON.parse(response)
76
- end
77
-
78
- def post url, param={}
79
- param_s = param_serializer(param)
80
- begin
81
- response = @access_token.post(url+param_s).body
82
- rescue => vars
83
- raise NetworkError, vars
84
- end
85
- return JSON.parse(response)
86
- end
29
+ include Essentials
87
30
  end
data/lib/pupil/blocks.rb CHANGED
@@ -1,13 +1,8 @@
1
1
  class Pupil
2
2
  # @param [Fixnum] id id
3
3
  # @return [Pupil::User] 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
4
+ def block(param)
5
+ response = self.post("/blocks/create.json", {guess_parameter(param) => param})
11
6
 
12
7
  if response.class == Hash && response["id"]
13
8
  return User.new response
@@ -17,13 +12,8 @@ class Pupil
17
12
 
18
13
  # @param [Fixnum] id id
19
14
  # @return [Pupil::User] 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
15
+ def unblock(param)
16
+ response = self.post("/blocks/destroy.json", {guess_parameter(param) => param})
27
17
 
28
18
  if response.class == Hash && response["id"]
29
19
  return User.new response
@@ -34,6 +24,7 @@ class Pupil
34
24
  # @return [Array] list of blocking users
35
25
  def blocking
36
26
  response = self.get("/blocks/blocking.json")
27
+ return [] if response["nilclasses"]
37
28
  users = Array.new
38
29
  response["users"].each do |element|
39
30
  user = User.new element
@@ -0,0 +1,61 @@
1
+ class Pupil
2
+ module Essentials
3
+ # @param [Hash] parameter
4
+ # @return [String] URL Serialized parameters
5
+ def serialize_parameter parameter
6
+ return "" unless parameter.class == Hash
7
+ ant = Hash.new
8
+ parameter.each do |key, value|
9
+ case key.to_sym
10
+ when :include
11
+ if value.class == String || Symbol
12
+ ant[:"include_#{value}"] = "true"
13
+ break
14
+ end
15
+ when :exclude
16
+ if value.class == String || Symbol
17
+ ant[:"exclude_#{value}"] = "true"
18
+ break
19
+ end
20
+ else
21
+ ant[key.to_sym] = value.to_s
22
+ end
23
+ end
24
+ param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
25
+ return param ? param : ""
26
+ end
27
+
28
+ def guess_parameter(parameter)
29
+ case parameter.class.to_s
30
+ when "Fixnum", "Bignum"
31
+ :user_id
32
+ when "Symbol", "String"
33
+ :screen_name
34
+ else
35
+ false
36
+ end
37
+ end
38
+
39
+ def get(url, param={})
40
+ param_s = serialize_parameter(param)
41
+ begin
42
+ response = @access_token.get(url+param_s).body
43
+ rescue => vars
44
+ raise NetworkError, vars
45
+ end
46
+ result = JSON.parse(response)
47
+ return (result["error"].nil? rescue true)? result : false
48
+ end
49
+
50
+ def post(url, param={})
51
+ param_s = serialize_parameter(param)
52
+ begin
53
+ response = @access_token.post(url+param_s).body
54
+ rescue => vars
55
+ raise NetworkError, vars
56
+ end
57
+ result = JSON.parse(response)
58
+ return (result["error"].nil? rescue true)? result : false
59
+ end
60
+ end
61
+ end
@@ -1,16 +1,18 @@
1
1
  class Pupil
2
- def friends_ids name
2
+ def friends_ids(name=@screen_name)
3
3
  response = self.get("/friends/ids/#{name}.json")
4
- ids = Array.new
4
+ return false unless response
5
+ ids = []
5
6
  response.each do |element|
6
7
  ids << element
7
8
  end
8
9
  return ids
9
10
  end
10
11
 
11
- def followers_ids name=@screen_name
12
+ def followers_ids(name=@screen_name)
12
13
  response = self.get("/1/followers/ids/#{name}.json")
13
- ids = Array.new
14
+ return false unless response
15
+ ids = []
14
16
  response.each do |element|
15
17
  ids << element
16
18
  end
@@ -22,8 +24,9 @@ class Pupil
22
24
  # @param [String] dst destination screen_name
23
25
  # @return [Boolean] return true if paired users have friendship, or ruturn false
24
26
  def friendship?(src, dst)
25
- param = {:source_screen_name => src, :target_screen_name => dst}
27
+ param = {"source_#{guess_parameter(src)}" => src, "target_#{guess_parameter(dst)}" => dst}
26
28
  response = self.get("/friendships/show.json", param)
29
+ return nil unless response
27
30
  if response["relationship"]["source"]["following"] == true && response["relationship"]["target"]["following"] == true then
28
31
  return true
29
32
  else
@@ -37,34 +40,18 @@ class Pupil
37
40
  # Follow user for screen_name
38
41
  # @param [String] name screen_name
39
42
  # @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
43
+ def follow(param)
44
+ response = self.post("/friendships/create.json", {guess_parameter(param) => param})
45
+ return false unless response
46
+ User.new(response, @access_token)
52
47
  end
53
48
 
54
49
  # Unfollow user for screen_name
55
50
  # @param [String] name screen_name
56
51
  # @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
52
+ def unfollow(param)
53
+ response = self.post("/friendships/destroy.json", {guess_parameter(param) => param})
54
+ return false unless response
55
+ User.new response
69
56
  end
70
57
  end
data/lib/pupil/keygen.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  $LOAD_PATH << File.dirname(File.expand_path(__FILE__)) if RUBY_VERSION >= "1.9.0"
4
+ require "essentials"
4
5
  require "base"
5
6
  require "keygen/base"
@@ -6,7 +6,7 @@ class Pupil
6
6
  attr_reader :access_token_secret
7
7
  class MissingRequiredTokens < StandardError; end
8
8
 
9
- def initialize opts={}
9
+ def initialize(opts={})
10
10
  @consumer_key = opts[:consumer_key] rescue nil
11
11
  @consumer_secret = opts[:consumer_secret] rescue nil
12
12
  @access_token = opts[:access_token] rescue nil
data/lib/pupil/lists.rb CHANGED
@@ -1,34 +1,22 @@
1
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
2
  # @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
3
+ def lists(param={})
4
+ if param[:contains]
5
+ response = self.get("/1/lists/memberships.json", param.reject{|p|p==:contains}.update(guess_parameter(param[:contains]) => param[:contains]))
6
+ response = response["lists"]
28
7
  else
29
- return response
8
+ response = self.get("/1/lists/all.json", param)
30
9
  end
10
+ return [] unless response
11
+ lists = Array.new
12
+ response.each do |list|
13
+ lists << List.new(list, @access_token)
14
+ end
15
+ return lists
31
16
  end
32
17
 
33
-
18
+ def create_list(name, param={})
19
+ response = self.post("/1/lists/create.json", {:name => name}.update(param))
20
+ return List.new(response, @access_token)
21
+ end
34
22
  end
data/lib/pupil/schemes.rb CHANGED
@@ -1,220 +1,149 @@
1
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 = Pupil::Status.new j["status"] rescue nil
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"]
2
+ class Scheme
3
+ protected
4
+ include Essentials
5
+
6
+ def method_missing(action, *args)
7
+ return @element[action.to_s] rescue nil
83
8
  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']
9
+
10
+ public
11
+
12
+ def initialize(element, access_token=nil)
13
+ @access_token = access_token
14
+ @element = element
97
15
  end
16
+
17
+ def methods() @element.keys.map{|k|k.to_sym} ; end
98
18
  end
99
19
 
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 = Pupil::User.new j["user"] rescue nil
20
+ class User < Scheme
21
+ def status
22
+ Pupil::Status.new(@element["status"], @access_token) rescue nil
23
+ end
24
+
25
+ def reply(sentence, status=nil)
26
+ response = self.post(
27
+ "/1/statuses/update.json",
28
+ "status"=> "@#{@element[:screen_name]} #{sentence}",
29
+ "in_reply_to_status_id" => status
30
+ )
31
+ return response
142
32
  end
143
33
  end
144
34
 
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 = Pupil::User.new(element.elements['user'])
35
+ class Status < Scheme
36
+ def user() Pupil::User.new(@element["user"], @access_token) rescue nil; end
37
+
38
+ def source()
39
+ @element["source"] =~ /href=\"(.+?)\".+?>(.+?)</
40
+ {:url => $1, :name => $2}
41
+ end
42
+
43
+ def entities() Pupil::Entities.new(@element["entities"]) rescue nil; end
44
+
45
+ def destroy()
46
+ self.post("/1/statuses/destroy/#{@element["id"]}.json")
170
47
  end
48
+
49
+ alias_method :delete, :destroy
171
50
  end
172
51
 
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 = Pupil::URL.new(element.elements['urls'])
181
- @hashtags = Pupil::Hashtag.new(element.elements['hashtags'])
52
+ class List < Scheme
53
+ def user() Pupil::User.new(@element["user"], @access_token) rescue nil; end
54
+
55
+ def statuses(param={})
56
+ response = self.get("/1/lists/statuses.json", {:list_id => @element["id_str"]}.update(param))
57
+ return false unless response
58
+ statuses = []
59
+ response.each do |status|
60
+ statuses << Pupil::Status.new(status, @access_token)
61
+ end
62
+ return statuses
63
+ end
64
+
65
+ def subscribers(param={})
66
+ response = self.get("/1/lists/subscribers.json", {:list_id => @element["id_str"]}.update(param))
67
+ return false unless response
68
+ users = []
69
+ response["users"].each do |user|
70
+ users << Pupil::User.new(user, @access_token)
71
+ end
72
+ return users
73
+ end
74
+
75
+ def members(param={})
76
+ response = self.get("/1/lists/members.json", {:list_id => @element["id_str"]}.update(param))
77
+ return false unless response
78
+ users = []
79
+ response["users"].each do |u|
80
+ user = User.new(u.update("_list_id" => @element["id_str"]), @access_token)
81
+ def user.destroy()
82
+ response = self.post("/1/lists/members/destroy.json", {:list_id => @element["_list_id"], :user_id => @element["id_str"]})
83
+ end
84
+ users << user
85
+ end
86
+ return users
87
+ end
88
+
89
+ def add(param)
90
+ response = self.post("/1/lists/members/create.json", {:list_id => @element["id_str"], guess_parameter(param) => param})
91
+ return List.new(response)
182
92
  end
93
+
94
+ def strike(param, opts={})
95
+ response = self.post("/1/lists/members/destroy.json", {guess_parameter(param) => param, :list_id => @element["id_str"]}.update(opts))
96
+ return response
97
+ end
98
+
99
+ alias_method :off, :strike
100
+
101
+ def destroy()
102
+ response = self.post("/1/lists/destroy.json", {:list_id => @element["id_str"]})
103
+ return List.new(response)
104
+ end
105
+
106
+ alias_method :delete, :destroy
183
107
  end
184
108
 
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']
109
+ class Entities < Scheme
110
+ def urls()
111
+ urls = []
112
+ @element["urls"].each do |url|
113
+ urls << Pupil::URL.new(url)
114
+ end
115
+ return urls
116
+ rescue
117
+ nil
118
+ end
119
+
120
+ def hashtags()
121
+ hashtags = []
122
+ @element["hashtags"].each do |hashtag|
123
+ hashtags << Pupil::Hashtag.new(hashtag)
124
+ end
125
+ return hashtags
126
+ rescue
127
+ nil
128
+ end
129
+
130
+ def user_mentions()
131
+ user_mentions = []
132
+ @element["user_mentions"].each do |user_mention|
133
+ user_mentions << Pupil::UserMention.new(user_mention)
134
+ end
135
+ return user_mentions
136
+ rescue
137
+ nil
194
138
  end
195
139
  end
196
140
 
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 = Pupil::User.new(element.elements['sender'])
217
- @recipient = Pupil::User.new(element.elements['recipient'])
218
- end
141
+ class URL < Scheme; end
142
+ class Hashtag < Scheme; end
143
+ class UserMention < Scheme; end
144
+
145
+ class DirectMessage < Scheme
146
+ def sender() Pupil::User.new(@element["sender"], @access_token) rescue nil; end
147
+ def recipient() Pupil::User.new(@element["recipient"], @access_token) rescue nil; end
219
148
  end
220
149
  end
@@ -10,10 +10,11 @@ class Pupil
10
10
  # @option param [Symbol] :exclude #=> [:replies]
11
11
  # @option param [Symbol] :contributor_details
12
12
  def home_timeline(param={})
13
- response = self.get("/statuses/home_timeline.json", param)
14
- statuses = Array.new
13
+ response = self.get("/1/statuses/home_timeline.json", param)
14
+ return false unless response
15
+ statuses = []
15
16
  response.each do |element|
16
- status = Status.new element
17
+ status = Status.new(element, @access_token)
17
18
  statuses << status
18
19
  end
19
20
  return statuses
@@ -22,10 +23,11 @@ class Pupil
22
23
  # @return [Array] Mention
23
24
  # @param [Hash] param
24
25
  def mentions(param={})
25
- response = self.get("/statuses/mentions.json", param)
26
- statuses = Array.new
26
+ response = self.get("/1/statuses/mentions.json", param)
27
+ return false unless response
28
+ statuses = []
27
29
  response.each do |element|
28
- status = Status.new element
30
+ status = Status.new(element, @access_token)
29
31
  statuses << status
30
32
  end
31
33
  return statuses
@@ -49,11 +51,12 @@ class Pupil
49
51
  # twitter.user_timeline(:screen_name => 'o_ame', :exclude => :replies).each do |status|
50
52
  # puts "#{status.user.screen_name}: #{status.text}"
51
53
  # end
52
- def user_timeline(param={})
53
- response = self.get("/statuses/user_timeline.json", param)
54
- statuses = Array.new
54
+ def user_timeline(param)
55
+ response = self.get("/1/statuses/user_timeline.json", {guess_parameter(param) => param})
56
+ return false unless response
57
+ statuses = []
55
58
  response.each do |element|
56
- status = Status.new element
59
+ status = Status.new(element, @access_token)
57
60
  statuses << status
58
61
  end
59
62
  return statuses
@@ -62,35 +65,38 @@ class Pupil
62
65
  # @return [Array] Timeline
63
66
  # @param [Hash] param
64
67
  def public_timeline(param={})
65
- response = self.get("/statuses/public_timeline.json", param)
68
+ response = self.get("/1/statuses/public_timeline.json", param)
69
+ return false unless response
66
70
  statuses = Array.new
67
71
  response.each do |element|
68
- status = Status.new element
72
+ status = Status.new(element, @access_token)
69
73
  statuses << status
70
74
  end
71
75
  return statuses
72
76
  end
73
77
 
74
- def show_status(status_id)
75
- response = @access_token.get("/statuses/show/#{status_id}.json").body
76
- return response
77
- status = Status.new response
78
+ def status(status_id)
79
+ response = self.get("/statuses/show/#{status_id}.json")
80
+ return false unless response
81
+ status = Status.new(response, @access_token)
78
82
  return status
79
83
  end
80
84
 
81
85
  def update(status, irt='')
82
86
  response = self.post(
83
- "/statuses/update.json",
87
+ "/1/statuses/update.json",
84
88
  "status"=> status,
85
89
  "in_reply_to_status_id" => irt
86
90
  )
87
- return response
91
+ return false unless response
92
+ response
88
93
  end
89
94
 
90
95
  alias_method :tweet, :update
91
96
 
92
- def destroy status_id
93
- response = self.post("/statuses/destroy/#{status_id}.json")
94
- return response
97
+ def destroy(status_id)
98
+ response = self.post("/1/statuses/destroy/#{status_id}.json")
99
+ return false unless response
100
+ response
95
101
  end
96
102
  end
data/lib/pupil/stream.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  $LOAD_PATH << File.dirname(File.expand_path(__FILE__)) if RUBY_VERSION >= "1.9.0"
4
+ require "essentials"
4
5
  require "base"
5
6
  require "schemes"
6
7
  require "stream/base"
@@ -5,6 +5,8 @@ class Pupil
5
5
  :userstream => "https://userstream.twitter.com/2/user.json",
6
6
  :search => "https://stream.twitter.com/1/statuses/filter.json%s"
7
7
  }
8
+
9
+ include Essentials
8
10
 
9
11
  def initialize key
10
12
  @screen_name = key[:screen_name]
@@ -29,7 +31,7 @@ class Pupil
29
31
  end
30
32
 
31
33
  def run_get_stream(type, param=nil, &block)
32
- uri = URI.parse(STREAM_APIS[type] % Pupil.param_serializer(param))
34
+ uri = URI.parse(STREAM_APIS[type] % serialize_parameter(param))
33
35
  https = Net::HTTP.new(uri.host, uri.port)
34
36
  https.use_ssl = true
35
37
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -70,9 +72,9 @@ class Pupil
70
72
  elsif status["event"] == "favorite"
71
73
  return Pupil::Stream::Hash.new(status, :favorite)
72
74
  elsif status["retweeted_status"]
73
- return Pupil::Stream::Status.new(status, :retweeted)
75
+ return Pupil::Stream::Status.new(status, @access_token, :retweeted)
74
76
  elsif status["text"]
75
- return Pupil::Stream::Status.new(status)
77
+ return Pupil::Stream::Status.new(status, @access_token)
76
78
  else
77
79
  return Pupil::Stream::Hash.new(status, :unknown)
78
80
  end
@@ -83,10 +85,10 @@ class Pupil
83
85
  attr_reader :event
84
86
  attr_reader :retweeted_status
85
87
 
86
- def initialize(status, event=nil)
87
- super(status)
88
+ def initialize(status, access_token, event=nil)
89
+ super(status, access_token)
88
90
  @event = (event)? event : :status
89
- @retweeted_status = status["retweeted_status"]
91
+ #@retweeted_status = status["retweeted_status"]
90
92
  end
91
93
  end
92
94
 
data/pupil.gemspec CHANGED
@@ -5,27 +5,23 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "pupil"
8
- s.version = "0.1.5"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oame"]
12
- s.date = "2012-01-07"
12
+ s.date = "2012-01-14"
13
13
  s.description = "The \"Lazy\" Twitter API Library for Ruby 1.9.x. Easy to use."
14
14
  s.email = "oame@oameya.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.md",
18
- "README.rdoc"
17
+ "README.md"
19
18
  ]
20
19
  s.files = [
21
- ".document",
22
- ".rspec",
23
20
  "CHANGELOG.md",
24
21
  "Gemfile",
25
22
  "Gemfile.lock",
26
23
  "LICENSE.txt",
27
24
  "README.md",
28
- "README.rdoc",
29
25
  "Rakefile",
30
26
  "VERSION",
31
27
  "lib/pupil.rb",
@@ -33,6 +29,7 @@ Gem::Specification.new do |s|
33
29
  "lib/pupil/base.rb",
34
30
  "lib/pupil/blocks.rb",
35
31
  "lib/pupil/direct_messages.rb",
32
+ "lib/pupil/essentials.rb",
36
33
  "lib/pupil/friendships.rb",
37
34
  "lib/pupil/keygen.rb",
38
35
  "lib/pupil/keygen/base.rb",
@@ -53,7 +50,7 @@ Gem::Specification.new do |s|
53
50
  s.licenses = ["MIT"]
54
51
  s.require_paths = ["lib"]
55
52
  s.required_ruby_version = Gem::Requirement.new("~> 1.9.0")
56
- s.rubygems_version = "1.8.10"
53
+ s.rubygems_version = "1.8.15"
57
54
  s.summary = "The \"Lazy\" Twitter API Library for Ruby 1.9.x"
58
55
 
59
56
  if s.respond_to? :specification_version then
data/spec/pupil_spec.rb CHANGED
@@ -50,7 +50,7 @@ end
50
50
  describe Pupil, "が #user_timeline を呼ぶ時は" do
51
51
  before do
52
52
  pupil = Pupil.new PUPIL_TESTKEY
53
- @user_timeline = pupil.user_timeline :screen_name => pupil.screen_name
53
+ @user_timeline = pupil.user_timeline pupil.screen_name
54
54
  end
55
55
 
56
56
  it "Array型を返すこと" do
@@ -77,8 +77,8 @@ end
77
77
  describe Pupil, "が #follow を呼ぶ時は" do
78
78
  before do
79
79
  pupil = Pupil.new PUPIL_TESTKEY
80
- @follow = pupil.follow :screen_name => KNOWN_NONFOLLOWED_USER
81
- @follow_fail = pupil.follow :screen_name => UNKNOWN_USER
80
+ @follow = pupil.follow KNOWN_NONFOLLOWED_USER
81
+ @follow_fail = pupil.follow UNKNOWN_USER
82
82
  end
83
83
 
84
84
  it "フォローに成功した場合はPupil::User型を返すこと" do
@@ -93,8 +93,8 @@ end
93
93
  describe Pupil, "が #unfollow を呼ぶ時は" do
94
94
  before do
95
95
  pupil = Pupil.new PUPIL_TESTKEY
96
- @unfollow = pupil.unfollow :screen_name => KNOWN_NONFOLLOWED_USER
97
- @unfollow_fail = pupil.unfollow :screen_name => UNKNOWN_USER
96
+ @unfollow = pupil.unfollow KNOWN_NONFOLLOWED_USER
97
+ @unfollow_fail = pupil.unfollow UNKNOWN_USER
98
98
  end
99
99
 
100
100
  it "成功した場合はPupil::User型を返すこと" do
@@ -109,8 +109,8 @@ end
109
109
  describe Pupil, "が #block を呼ぶ時は" do
110
110
  before do
111
111
  pupil = Pupil.new PUPIL_TESTKEY
112
- @block = pupil.block :screen_name => KNOWN_NONFOLLOWED_USER
113
- @block_fail = pupil.block :screen_name => UNKNOWN_USER
112
+ @block = pupil.block KNOWN_NONFOLLOWED_USER
113
+ @block_fail = pupil.block UNKNOWN_USER
114
114
  end
115
115
 
116
116
  it "成功した場合はPupil::User型を返すこと" do
@@ -125,8 +125,8 @@ end
125
125
  describe Pupil, "が #unblock を呼ぶ時は" do
126
126
  before do
127
127
  pupil = Pupil.new PUPIL_TESTKEY
128
- @unblock = pupil.unblock :screen_name => KNOWN_NONFOLLOWED_USER
129
- @unblock_fail = pupil.unblock :screen_name => UNKNOWN_USER
128
+ @unblock = pupil.unblock KNOWN_NONFOLLOWED_USER
129
+ @unblock_fail = pupil.unblock UNKNOWN_USER
130
130
  end
131
131
 
132
132
  it "成功した場合はPupil::User型を返すこと" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pupil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-07 00:00:00.000000000Z
12
+ date: 2012-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70291003102160 !ruby/object:Gem::Requirement
16
+ requirement: &70158376735540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70291003102160
24
+ version_requirements: *70158376735540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70291003100160 !ruby/object:Gem::Requirement
27
+ requirement: &70158376782300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70291003100160
35
+ version_requirements: *70158376782300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70291003097540 !ruby/object:Gem::Requirement
38
+ requirement: &70158376779420 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.3.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70291003097540
46
+ version_requirements: *70158376779420
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70291003096360 !ruby/object:Gem::Requirement
49
+ requirement: &70158376778060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.6.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70291003096360
57
+ version_requirements: *70158376778060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70291003094740 !ruby/object:Gem::Requirement
60
+ requirement: &70158376776600 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70291003094740
68
+ version_requirements: *70158376776600
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &70291003093000 !ruby/object:Gem::Requirement
71
+ requirement: &70158376775060 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.6.4
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70291003093000
79
+ version_requirements: *70158376775060
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rcov
82
- requirement: &70291003091900 !ruby/object:Gem::Requirement
82
+ requirement: &70158376811520 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70291003091900
90
+ version_requirements: *70158376811520
91
91
  description: The "Lazy" Twitter API Library for Ruby 1.9.x. Easy to use.
92
92
  email: oame@oameya.com
93
93
  executables: []
@@ -95,16 +95,12 @@ extensions: []
95
95
  extra_rdoc_files:
96
96
  - LICENSE.txt
97
97
  - README.md
98
- - README.rdoc
99
98
  files:
100
- - .document
101
- - .rspec
102
99
  - CHANGELOG.md
103
100
  - Gemfile
104
101
  - Gemfile.lock
105
102
  - LICENSE.txt
106
103
  - README.md
107
- - README.rdoc
108
104
  - Rakefile
109
105
  - VERSION
110
106
  - lib/pupil.rb
@@ -112,6 +108,7 @@ files:
112
108
  - lib/pupil/base.rb
113
109
  - lib/pupil/blocks.rb
114
110
  - lib/pupil/direct_messages.rb
111
+ - lib/pupil/essentials.rb
115
112
  - lib/pupil/friendships.rb
116
113
  - lib/pupil/keygen.rb
117
114
  - lib/pupil/keygen/base.rb
@@ -148,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
145
  version: '0'
149
146
  requirements: []
150
147
  rubyforge_project:
151
- rubygems_version: 1.8.10
148
+ rubygems_version: 1.8.15
152
149
  signing_key:
153
150
  specification_version: 3
154
151
  summary: The "Lazy" Twitter API Library for Ruby 1.9.x
data/.document DELETED
@@ -1,6 +0,0 @@
1
- lib/**/*.rb
2
- lib/*.rb
3
- bin/*
4
- -
5
- features/**/*.feature
6
- LICENSE.txt
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = pupil
2
-
3
- Pupil is lazy Twitter API Wrapper for Ruby 1.9.x
4
-
5
- == Contributing to pupil
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 Oame. See LICENSE.txt for
18
- further details.
19
-