rubyoverflow 0.5 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/README.rdoc +22 -11
  2. data/RELEASENOTES +97 -1
  3. data/Rakefile +49 -7
  4. data/VERSION +1 -1
  5. data/lib/rubyoverflow.rb +92 -18
  6. data/lib/rubyoverflow/answer.rb +61 -0
  7. data/lib/rubyoverflow/answers.rb +60 -0
  8. data/lib/rubyoverflow/apiSite.rb +32 -0
  9. data/lib/rubyoverflow/apiSites.rb +25 -0
  10. data/lib/rubyoverflow/apiVersion.rb +6 -0
  11. data/lib/rubyoverflow/badge.rb +44 -0
  12. data/lib/rubyoverflow/badgeCounts.rb +22 -0
  13. data/lib/rubyoverflow/badges.rb +59 -0
  14. data/lib/rubyoverflow/base.rb +76 -0
  15. data/lib/rubyoverflow/comment.rb +41 -0
  16. data/lib/rubyoverflow/comments.rb +115 -0
  17. data/lib/rubyoverflow/errors.rb +17 -0
  18. data/lib/rubyoverflow/pagedBase.rb +27 -0
  19. data/lib/rubyoverflow/pagedDash.rb +7 -0
  20. data/lib/rubyoverflow/postTimelineEvent.rb +93 -0
  21. data/lib/rubyoverflow/postTimelineEvents.rb +39 -0
  22. data/lib/rubyoverflow/question.rb +110 -0
  23. data/lib/rubyoverflow/questions.rb +104 -0
  24. data/lib/rubyoverflow/repChange.rb +34 -0
  25. data/lib/rubyoverflow/repChanges.rb +41 -0
  26. data/lib/rubyoverflow/revision.rb +62 -0
  27. data/lib/rubyoverflow/revisions.rb +52 -0
  28. data/lib/rubyoverflow/statistics.rb +57 -0
  29. data/lib/rubyoverflow/styling.rb +19 -0
  30. data/lib/rubyoverflow/tag.rb +27 -0
  31. data/lib/rubyoverflow/tags.rb +46 -0
  32. data/lib/rubyoverflow/user.rb +181 -0
  33. data/lib/rubyoverflow/userTimelineEvent.rb +43 -0
  34. data/lib/rubyoverflow/userTimelineEvents.rb +35 -0
  35. data/lib/rubyoverflow/users.rb +67 -18
  36. data/test/apiKey.rb +5 -0
  37. data/test/helper.rb +159 -0
  38. data/test/test_answer.rb +20 -0
  39. data/test/test_answers.rb +32 -0
  40. data/test/test_badge.rb +18 -0
  41. data/test/test_badges.rb +30 -0
  42. data/test/test_base.rb +63 -0
  43. data/test/test_statistics.rb +26 -0
  44. data/test/test_user.rb +56 -0
  45. data/test/test_users.rb +37 -0
  46. metadata +122 -87
  47. data/.gitignore +0 -4
  48. data/.rvmrc +0 -47
  49. data/.travis.yml +0 -5
  50. data/Gemfile +0 -3
  51. data/lib/rubyoverflow/sites.rb +0 -20
  52. data/lib/rubyoverflow/version.rb +0 -3
  53. data/rubyoverflow.gemspec +0 -26
  54. data/spec/sites_spec.rb +0 -18
  55. data/spec/spec_helper.rb +0 -3
  56. data/spec/users_spec.rb +0 -23
@@ -0,0 +1,43 @@
1
+ module Rubyoverflow
2
+ class UserTimelineEvent
3
+ attr_reader :user_id
4
+ attr_reader :timeline_type
5
+ attr_reader :post_id
6
+ attr_reader :post_type
7
+ attr_reader :comment_id
8
+ attr_reader :action
9
+ attr_reader :creation_date
10
+ attr_reader :description
11
+ attr_reader :detail
12
+
13
+ def initialize(hash, request_path = '')
14
+ dash = UserTimelineEventDash.new hash
15
+
16
+ @user_id = dash.user_id
17
+ @timeline_type = dash.timeline_type
18
+ @post_id = dash.post_id
19
+ @post_type = dash.post_type
20
+ @comment_id = dash.comment_id
21
+ @action = dash.action
22
+ @creation_date = dash.creation_date
23
+ @description = dash.description
24
+ @detail = dash.detail
25
+ end
26
+
27
+
28
+ end
29
+
30
+ class UserTimelineEventDash < BaseDash
31
+
32
+ property :user_id
33
+ property :timeline_type
34
+ property :post_id
35
+ property :post_type
36
+ property :comment_id
37
+ property :action
38
+ property :creation_date
39
+ property :description
40
+ property :detail
41
+
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module Rubyoverflow
2
+ class UserTimelineEvents < PagedBase
3
+ attr_reader :user_timelines
4
+ def initialize(hash, request_path = '')
5
+ dash = UserTimelineEventsDash.new hash
6
+ super(dash, request_path)
7
+ @user_timelines = Array.new
8
+ dash.user_timelines.each{|timelineHash| @user_timelines.push(UserTimelineEvent.new timelineHash)}
9
+ end
10
+
11
+ #Retrieves the next set of UserTimelineEvents using the same parameters used to retrieve the current set
12
+ def get_next_set
13
+ hash,url = perform_next_page_request
14
+ UserTimelineEvents.new hash,url
15
+ end
16
+
17
+ class << self
18
+ #Retrieves a set of timeline events for a set of user(s) by their id(s)
19
+ #
20
+ #id can be an int, string or an array of ints or strings
21
+ #
22
+ #Maps to 'users/{id}/timeline
23
+ def retrieve_by_user(id, parameters = {})
24
+ id = convert_to_id_list(id)
25
+ hash, url = request('users/' + id.to_s + '/timeline',parameters)
26
+ UserTimelineEvents.new hash, url
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ class UserTimelineEventsDash < PagedDash
33
+ property :user_timelines
34
+ end
35
+ end
@@ -1,24 +1,73 @@
1
1
  module Rubyoverflow
2
- class Users
3
-
4
- def initialize(client)
5
- @client = client
2
+ class Users < PagedBase
3
+ attr_reader :users
4
+
5
+ def initialize(hash, request_path = '')
6
+ dash = UsersDash.new hash
7
+ super(dash, request_path)
8
+ @users = Array.new
9
+
10
+ dash.users.each{|userHash| @users.push(User.new userHash)} if dash.users
11
+ dash.associated_users.each{|userHash| @users.push(User.new userHash)} if dash.associated_users
12
+
6
13
  end
7
-
8
- def fetch(params = {})
9
- ids = params.delete(:id) if params[:id]
10
- ids = ids.join(';') if ids and ids.kind_of? Array
11
- hash,url = @client.request "users#{"/#{ids}" if ids}", params
12
-
13
- Hashie::Mash.new hash
14
+
15
+ #Retrieves the next set of users using the same parameters used to retrieve the current set
16
+ def get_next_set
17
+ hash,url = perform_next_page_request
18
+ Users.new hash,url
19
+ end
20
+
21
+ class << self
22
+
23
+ #Retieves all users using the parameters provided
24
+ #
25
+ #Maps to '/users'
26
+ def retrieve_all(parameters = {})
27
+ hash,url = request('users/', parameters)
28
+ Users.new hash, url
29
+ end
30
+
31
+ #Retrieves users by id(s) using the parameters provided
32
+ #
33
+ #id can be an int, string, or an array of either
34
+ #
35
+ #Maps to '/users/{id}'
36
+ def retrieve_by_id(id, parameters = {})
37
+ id = convert_to_id_list(id)
38
+ hash,url = request('users/'+id.to_s, parameters)
39
+ Users.new hash, url
40
+ end
41
+
42
+ #Retrieves users that have received badge(s) by badge id(s) using the parameters provided
43
+ #
44
+ #id can be an int, string, badge or an array of any of the three
45
+ #
46
+ #Maps to '/badges/{id}'
47
+ def retrieve_by_badge(id, parameters = {})
48
+ id = convert_to_id_list(id)
49
+ hash,url = request('badges/'+id.to_s, parameters)
50
+ Users.new hash, url
51
+ end
52
+
53
+ #Retrieves moderators for the current site the parameters provided
54
+ #
55
+ #Maps to '/users/moderators'
56
+ def retrieve_moderators(parameters = {})
57
+ hash,url = request('users/moderators', parameters)
58
+ Users.new hash, url
59
+ end
60
+
61
+ def retrieve_associated_accounts(guid)
62
+ client = Client.stackauth_client(Base.client.api_key)
63
+ hash,url = client.request('users/' + guid +'/associated',{})
64
+ Users.new hash,url
65
+ end
14
66
  end
15
67
  end
16
68
 
17
- class Client
18
- @users = nil
19
-
20
- def users
21
- @users ||= Users.new(self)
22
- end
69
+ class UsersDash < PagedDash
70
+ property :users
71
+ property :associated_users
23
72
  end
24
- end
73
+ end
@@ -0,0 +1,5 @@
1
+ class ApiKey
2
+ def self.api_key
3
+ 'WiNylIPGTkm4DrBH2yYZ3g'
4
+ end
5
+ end
@@ -0,0 +1,159 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'spec'
4
+ require 'apikey'
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rubyoverflow'
8
+ include Rubyoverflow
9
+
10
+ class Test::Unit::TestCase
11
+ end
12
+ def set_api_key
13
+ Rubyoverflow::Client.config do |options|
14
+ options.api_key = ApiKey.api_key
15
+ end
16
+ end
17
+ def check_paged_class(paged_class, page_size)
18
+ paged_class.should respond_to(:total)
19
+ paged_class.should respond_to(:pagesize)
20
+ paged_class.should respond_to(:page)
21
+ paged_class.should respond_to(:request_path)
22
+ paged_class.should respond_to(:query_parameters)
23
+ paged_class.pagesize.should == page_size
24
+ end
25
+
26
+ def single_item_set
27
+ {:pagesize => 1}
28
+ end
29
+
30
+ def check_user_set(users, page_size = 1)
31
+ check_paged_class(users, page_size)
32
+ users.should respond_to(:users)
33
+ check_user users.users.first
34
+ end
35
+
36
+ def check_answer_set(answers, page_size = 1)
37
+ check_paged_class(answers, page_size)
38
+ answers.should respond_to(:answers)
39
+ check_answer answers.answers.first
40
+ end
41
+
42
+ def check_api_site_set(api_sites, page_size = 1)
43
+ check_paged_class(api_sites, page_size)
44
+ api_sites.should respond_to(:api_sites)
45
+ #check_api_site api_sites.api_sites.first
46
+ end
47
+
48
+ def check_badge_set(badges)
49
+ badges.should respond_to(:badges)
50
+ check_badge badges.badges.first
51
+ end
52
+
53
+ def check_comment_set(comments, page_size = 1)
54
+ check_paged_class(comments, page_size)
55
+ comments.should respond_to(:comments)
56
+ #check_comment comments.comments.first
57
+ end
58
+
59
+ def check_post_timeline_event_set(post_timeline_events, page_size = 1)
60
+ check_paged_class(post_timeline_events, page_size)
61
+ post_timeline_events.should respond_to(:post_timelines)
62
+ #check_post_timeline_event post_timeline_events.post_timelines.first
63
+ end
64
+
65
+ def check_rep_change_set(rep_changes, page_size = 1)
66
+ check_paged_class(rep_changes, page_size)
67
+ rep_changes.should respond_to(:rep_changes)
68
+ #check_rep_change rep_changes.rep_changes.first
69
+ end
70
+
71
+ def check_revision_set(revisions, page_size = 1)
72
+ check_paged_class(revisions, page_size)
73
+ revisions.should respond_to(:revisions)
74
+ #check_revision revisions.revisions.first
75
+ end
76
+
77
+ def check_tag_set(tags, page_size = 1)
78
+ check_paged_class(tags, page_size)
79
+ tags.should respond_to(:tags)
80
+ #check_tag tags.tags.first
81
+ end
82
+
83
+ def check_user_timeline_event_set(user_timeline_events, page_size = 1)
84
+ check_paged_class(user_timeline_events, page_size)
85
+ user_timeline_events.should respond_to(:user_timelines)
86
+ #check_user_timeline_event user_timeline_events.user_timelines.first
87
+ end
88
+
89
+ def check_question_set(questions, page_size = 1)
90
+ check_paged_class(questions, page_size)
91
+ questions.should respond_to(:questions)
92
+ #check_question questions.questions.first
93
+ end
94
+
95
+ def check_answer(answer)
96
+ answer.should respond_to(:answer_id)
97
+ answer.should respond_to(:accepted)
98
+ answer.should respond_to(:answer_comments_url)
99
+ answer.should respond_to(:question_id)
100
+ answer.should respond_to(:locked_date)
101
+ answer.should respond_to(:owner)
102
+ answer.should respond_to(:creation_date)
103
+ answer.should respond_to(:last_edit_date)
104
+ answer.should respond_to(:last_activity_date)
105
+ answer.should respond_to(:up_vote_count)
106
+ answer.should respond_to(:down_vote_count)
107
+ answer.should respond_to(:view_count)
108
+ answer.should respond_to(:score)
109
+ answer.should respond_to(:community_owned)
110
+ answer.should respond_to(:title)
111
+ answer.should respond_to(:body)
112
+ answer.should respond_to(:comments)
113
+ end
114
+
115
+
116
+ def check_badge(badge)
117
+ badge.should respond_to(:badge_id)
118
+ badge.should respond_to(:rank)
119
+ badge.should respond_to(:name)
120
+ badge.should respond_to(:description)
121
+ badge.should respond_to(:award_count)
122
+ badge.should respond_to(:tag_based)
123
+ badge.should respond_to(:user)
124
+ badge.should respond_to(:badges_recipients_url)
125
+ end
126
+
127
+
128
+ def check_user(user)
129
+ user.should respond_to(:user_id)
130
+ user.should respond_to(:user_type)
131
+ user.should respond_to(:creation_date)
132
+ user.should respond_to(:display_name)
133
+ user.should respond_to(:reputation)
134
+ user.should respond_to(:email_hash)
135
+ user.should respond_to(:age)
136
+ user.should respond_to(:last_access_date)
137
+ user.should respond_to(:website_url)
138
+ user.should respond_to(:location)
139
+ user.should respond_to(:about_me)
140
+ user.should respond_to(:question_count)
141
+ user.should respond_to(:answer_count)
142
+ user.should respond_to(:view_count)
143
+ user.should respond_to(:up_vote_count)
144
+ user.should respond_to(:down_vote_count)
145
+ user.should respond_to(:accept_rate)
146
+ user.should respond_to(:user_questions_url)
147
+ user.should respond_to(:user_answers_url)
148
+ user.should respond_to(:user_favorites_url)
149
+ user.should respond_to(:user_tags_url)
150
+ user.should respond_to(:user_badges_url)
151
+ user.should respond_to(:user_timeline_url)
152
+ user.should respond_to(:user_mentioned_url)
153
+ user.should respond_to(:user_comments_url)
154
+ user.should respond_to(:user_reputation_url)
155
+ user.should respond_to(:badge_counts)
156
+ user.should respond_to(:timed_penalty_date)
157
+ user.should respond_to(:association_id)
158
+ user.should respond_to(:on_site)
159
+ end
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class TestAnswer < Test::Unit::TestCase
4
+ describe Answer do
5
+ before(:all) do
6
+ #@user = Users.retrieve_by_id(53587, single_item_set).users.first
7
+ @answer = Answers.retrieve_by_id(952594, single_item_set).answers.first
8
+ set_api_key
9
+ end
10
+
11
+ it "check answer" do
12
+ check_answer @answer
13
+ end
14
+
15
+ it "get_comments" do
16
+ check_comment_set @answer.get_comments single_item_set
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestAnswers < Test::Unit::TestCase
4
+ describe Answers do
5
+ before(:all) do
6
+ set_api_key
7
+ end
8
+
9
+ it "retrieve_by_id" do
10
+ answers = Answers.retrieve_by_id 952594, single_item_set
11
+ check_answer_set answers
12
+ end
13
+
14
+ it "retrieve_by_user" do
15
+ answers = Answers.retrieve_by_user 53587, single_item_set
16
+ check_answer_set answers
17
+ end
18
+
19
+ it "retrieve_by_question" do
20
+ answers = Answers.retrieve_by_question 1104543, single_item_set
21
+ check_answer_set answers
22
+ end
23
+
24
+ it "get_next_set" do
25
+ answers = Answers.retrieve_by_question 1104543, single_item_set
26
+ answers = answers.get_next_set
27
+ check_answer_set answers
28
+ answers.page.should == 2
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ class TestBadge < Test::Unit::TestCase
4
+ describe Badge do
5
+ before(:all) do
6
+ @badge = Badges.retrieve_all.badges.first
7
+ set_api_key
8
+ end
9
+
10
+ it 'check badge' do
11
+ check_badge @badge
12
+ end
13
+
14
+ it 'get_recipients' do
15
+ check_user_set @badge.get_recipients(single_item_set)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ class TestBadges < Test::Unit::TestCase
4
+ describe Badges do
5
+ before(:all) do
6
+ @users = Users.retrieve_all
7
+ set_api_key
8
+ end
9
+
10
+ it 'retrieve_all' do
11
+ badges = Badges.retrieve_all
12
+ check_badge_set badges
13
+ end
14
+
15
+ it 'retrieve_all_non_tag_based' do
16
+ badges = Badges.retrieve_all_non_tag_based
17
+ check_badge_set badges
18
+ end
19
+
20
+ it 'retrieve_all_tag_based' do
21
+ badges = Badges.retrieve_all_tag_based
22
+ check_badge_set badges
23
+ end
24
+
25
+ it 'retrieve_by_user' do
26
+ badges = Badges.retrieve_by_user @users.users.first.user_id
27
+ check_badge_set badges
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ class TestBase < Test::Unit::TestCase
4
+ describe Base do
5
+
6
+ it 'find_parse_querystring' do
7
+ base = Base.new
8
+ testVal = 'http://api.stackoverflow.com/1.0/users/?order=asc&page=2'
9
+ url,queryHash = base.find_parse_querystring testVal
10
+ url.should == 'users/'
11
+ queryHash.should == {'order'=>'asc','page'=>'2'}
12
+ end
13
+
14
+ it 'convert_to_id_list int' do
15
+ Base.convert_to_id_list(1).should == '1'
16
+ end
17
+
18
+ it 'convert_to_id_list string' do
19
+ Base.convert_to_id_list('1').should == '1'
20
+ end
21
+
22
+ it 'convert_to_id_list item' do
23
+ item = Item.new 1
24
+
25
+ Base.convert_to_id_list(item).should == '1'
26
+ end
27
+
28
+ it 'convert_to_id_list int[]' do
29
+ Base.convert_to_id_list([1,2,3]).should == '1;2;3'
30
+ end
31
+
32
+ it 'convert_to_id_list string[]' do
33
+ Base.convert_to_id_list(['1','2','3']).should == '1;2;3'
34
+ end
35
+
36
+ it 'convert_to_id_list item[]' do
37
+ item1 = Item.new 1
38
+ item2 = Item.new 2
39
+ item3 = Item.new 3
40
+
41
+ Base.convert_to_id_list([item1,item2,item3]).should == '1;2;3'
42
+ end
43
+
44
+ it 'convert_to_id_list mixed[]' do
45
+ item3 = Item.new 3
46
+ Base.convert_to_id_list([1,'2',item3]).should == '1;2;3'
47
+ end
48
+
49
+ end
50
+ end
51
+
52
+ class Item
53
+ attr_reader :val
54
+
55
+ def initialize(id)
56
+ @val = id
57
+ end
58
+
59
+ def item_id
60
+ @val
61
+ end
62
+
63
+ end