ruby-stackoverflow 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/.gitignore +17 -0
  2. data/.rake_tasks +4 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +50 -0
  6. data/Rakefile +6 -0
  7. data/lib/generators/ruby_stackoverflow/ruby_stackoverflow_generator.rb +31 -0
  8. data/lib/generators/ruby_stackoverflow/templates/ruby_stackoverflow.rb +1 -0
  9. data/lib/ruby-stackoverflow.rb +29 -0
  10. data/lib/ruby-stackoverflow/client.rb +68 -0
  11. data/lib/ruby-stackoverflow/client/.DS_Store +0 -0
  12. data/lib/ruby-stackoverflow/client/badges_helper.rb +50 -0
  13. data/lib/ruby-stackoverflow/client/comments_helper.rb +23 -0
  14. data/lib/ruby-stackoverflow/client/parse_options.rb +25 -0
  15. data/lib/ruby-stackoverflow/client/question_helper.rb +61 -0
  16. data/lib/ruby-stackoverflow/client/resource/answer.rb +7 -0
  17. data/lib/ruby-stackoverflow/client/resource/badge.rb +7 -0
  18. data/lib/ruby-stackoverflow/client/resource/comment.rb +8 -0
  19. data/lib/ruby-stackoverflow/client/resource/notification.rb +8 -0
  20. data/lib/ruby-stackoverflow/client/resource/permission.rb +8 -0
  21. data/lib/ruby-stackoverflow/client/resource/post.rb +7 -0
  22. data/lib/ruby-stackoverflow/client/resource/question.rb +66 -0
  23. data/lib/ruby-stackoverflow/client/resource/reputation.rb +8 -0
  24. data/lib/ruby-stackoverflow/client/resource/resource.rb +28 -0
  25. data/lib/ruby-stackoverflow/client/resource/stackoverflow_error.rb +13 -0
  26. data/lib/ruby-stackoverflow/client/resource/suggested_edit.rb +8 -0
  27. data/lib/ruby-stackoverflow/client/resource/tag.rb +19 -0
  28. data/lib/ruby-stackoverflow/client/resource/user.rb +125 -0
  29. data/lib/ruby-stackoverflow/client/response_data.rb +20 -0
  30. data/lib/ruby-stackoverflow/client/user_helper.rb +152 -0
  31. data/lib/ruby-stackoverflow/configuration.rb +10 -0
  32. data/lib/ruby-stackoverflow/ruby-stackoverflow_generator.rb +29 -0
  33. data/lib/ruby-stackoverflow/version.rb +4 -0
  34. data/ruby-stackoverflow.gemspec +28 -0
  35. data/spec/fixtures/error.json +5 -0
  36. data/spec/fixtures/questions.json +28 -0
  37. data/spec/fixtures/questions_answers.json +23 -0
  38. data/spec/fixtures/questions_comments.json +39 -0
  39. data/spec/fixtures/questions_timeline.json +200 -0
  40. data/spec/fixtures/users.json +32 -0
  41. data/spec/fixtures/vcr_cassettes/badges.yml +67 -0
  42. data/spec/fixtures/vcr_cassettes/badges_by_ids.yml +56 -0
  43. data/spec/fixtures/vcr_cassettes/badges_by_name.yml +55 -0
  44. data/spec/fixtures/vcr_cassettes/badges_by_recipients.yml +73 -0
  45. data/spec/fixtures/vcr_cassettes/badges_by_recipients_by_ids.yml +70 -0
  46. data/spec/fixtures/vcr_cassettes/badges_by_tags.yml +58 -0
  47. data/spec/fixtures/vcr_cassettes/comments.yml +104 -0
  48. data/spec/fixtures/vcr_cassettes/comments_by_ids.yml +60 -0
  49. data/spec/fixtures/vcr_cassettes/error.yml +53 -0
  50. data/spec/fixtures/vcr_cassettes/featured_questions.yml +62 -0
  51. data/spec/fixtures/vcr_cassettes/linked_questions.yml +53 -0
  52. data/spec/fixtures/vcr_cassettes/noanswered_questions.yml +62 -0
  53. data/spec/fixtures/vcr_cassettes/questions.yml +165 -0
  54. data/spec/fixtures/vcr_cassettes/questions_answers.yml +63 -0
  55. data/spec/fixtures/vcr_cassettes/questions_by_ids.yml +62 -0
  56. data/spec/fixtures/vcr_cassettes/questions_comments.yml +60 -0
  57. data/spec/fixtures/vcr_cassettes/related_questions.yml +94 -0
  58. data/spec/fixtures/vcr_cassettes/timeline_questions.yml +68 -0
  59. data/spec/fixtures/vcr_cassettes/unanswered_questions.yml +62 -0
  60. data/spec/fixtures/vcr_cassettes/user_timeline.yml +73 -0
  61. data/spec/fixtures/vcr_cassettes/user_top_answers_with_given_tags.yml +64 -0
  62. data/spec/fixtures/vcr_cassettes/user_top_questions_with_given_tags.yml +66 -0
  63. data/spec/fixtures/vcr_cassettes/user_top_tags_by_answers.yml +62 -0
  64. data/spec/fixtures/vcr_cassettes/user_top_tags_by_questions.yml +58 -0
  65. data/spec/fixtures/vcr_cassettes/user_write_permissions.yml +56 -0
  66. data/spec/fixtures/vcr_cassettes/users.yml +61 -0
  67. data/spec/fixtures/vcr_cassettes/users_answers.yml +65 -0
  68. data/spec/fixtures/vcr_cassettes/users_badges.yml +60 -0
  69. data/spec/fixtures/vcr_cassettes/users_by_ids.yml +61 -0
  70. data/spec/fixtures/vcr_cassettes/users_comments.yml +74 -0
  71. data/spec/fixtures/vcr_cassettes/users_favorites_questions.yml +74 -0
  72. data/spec/fixtures/vcr_cassettes/users_featured_questions.yml +53 -0
  73. data/spec/fixtures/vcr_cassettes/users_mentioned_questions.yml +97 -0
  74. data/spec/fixtures/vcr_cassettes/users_noanswerquestions.yml +60 -0
  75. data/spec/fixtures/vcr_cassettes/users_notifications.yml +80 -0
  76. data/spec/fixtures/vcr_cassettes/users_questions.yml +67 -0
  77. data/spec/fixtures/vcr_cassettes/users_replied_comments.yml +60 -0
  78. data/spec/fixtures/vcr_cassettes/users_reputation_questions.yml +59 -0
  79. data/spec/fixtures/vcr_cassettes/users_suggested_edits.yml +72 -0
  80. data/spec/fixtures/vcr_cassettes/users_tags.yml +60 -0
  81. data/spec/fixtures/vcr_cassettes/users_unaccepted_questions.yml +65 -0
  82. data/spec/fixtures/vcr_cassettes/users_unanswered_questions.yml +77 -0
  83. data/spec/fixtures/vcr_cassettes/users_unread_notifications.yml +53 -0
  84. data/spec/helper.rb +45 -0
  85. data/spec/ruby-stackoverflow/badges_spec.rb +63 -0
  86. data/spec/ruby-stackoverflow/comments_spec.rb +31 -0
  87. data/spec/ruby-stackoverflow/question_spec.rb +90 -0
  88. data/spec/ruby-stackoverflow/user_spec.rb +289 -0
  89. data/spec/spec_helper.rb +16 -0
  90. metadata +292 -0
@@ -0,0 +1,8 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Notification < Resource
4
+ end
5
+ end
6
+ end
7
+
8
+
@@ -0,0 +1,8 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Permission < Resource
4
+ end
5
+ end
6
+ end
7
+
8
+
@@ -0,0 +1,7 @@
1
+
2
+ module RubyStackoverflow
3
+ class Client
4
+ class Post < Resource
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,66 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Question < Resource
4
+ attr_reader :answers , :comments,:user, :posts
5
+ def initialize(attributes_hash={})
6
+ @answers = []
7
+ @comments = []
8
+ @posts = []
9
+ super
10
+ end
11
+
12
+ class << self
13
+ def parse_data(data)
14
+ questions = []
15
+ data.each do|attr_hash|
16
+ if data_has_answer?(data)
17
+ question = create_question(attr_hash, questions, :question_id)
18
+ question.answers.push(Answer.new(attr_hash))
19
+ elsif data_has_comment?(data)
20
+ question = create_question(attr_hash, questions,:post_id)
21
+ question.comments.push(Comment.new(attr_hash))
22
+ elsif data_has_timeline?(data)
23
+ question = create_question(attr_hash, questions,:question_id)
24
+ question.posts.push(Post.new(attr_hash))
25
+ else
26
+ questions << new(attr_hash)
27
+ end
28
+ end
29
+ questions
30
+ end
31
+
32
+ def data_has_answer?(data)
33
+ data.first.include?(:answer_id)
34
+ end
35
+
36
+ def data_has_comment?(data)
37
+ data.first.include?(:comment_id) && !data.first.include?(:timeline_type)
38
+ end
39
+
40
+ def data_has_timeline?(data)
41
+ data.first.include?(:timeline_type)
42
+ end
43
+
44
+ def find_or_create_question(questions, qid)
45
+ question_array = questions.select{|q|q.question_id == qid}
46
+ !question_array.empty? ? question_array.first : new({question_id: qid})
47
+ end
48
+
49
+
50
+ def create_question(attr_hash, questions, hash_key)
51
+ qid = attr_hash.delete(hash_key)
52
+ question = find_or_create_question(questions, qid)
53
+ questions << question unless question_exists?(questions,qid)
54
+ question
55
+ end
56
+
57
+ def question_exists?(questions, question_id)
58
+ question_array = questions.select{|q|q.question_id == question_id }
59
+ !question_array.empty?
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,8 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Reputation < Resource
4
+ end
5
+ end
6
+ end
7
+
8
+
@@ -0,0 +1,28 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Resource
4
+ def initialize(attributes_hash)
5
+ attributes_hash.each do|k,v|
6
+ self.class.send :attr_accessor, k
7
+ var = "@#{k}"
8
+ case k.to_sym
9
+ when :creation_date, :last_activity_date, :launch_date,:last_edit_date
10
+ value = Time.at(v).utc.to_s
11
+ else
12
+ value = v
13
+ end
14
+ instance_variable_set(var, value)
15
+ end
16
+ end
17
+
18
+ class << self
19
+ def parse_data(data)
20
+ datas = data.map do|attr_hash|
21
+ new(attr_hash)
22
+ end
23
+ datas
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class StackoverflowError
4
+ attr_reader :error_code, :error_message, :error_name
5
+
6
+ def initialize(params)
7
+ @error_name = params[:error_name]
8
+ @error_code = params[:error_id]
9
+ @error_message = params[:error_message]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class SuggestedEdit < Resource
4
+ end
5
+ end
6
+ end
7
+
8
+
@@ -0,0 +1,19 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class Tag < Resource
4
+ #def initialize(attributes_hash)
5
+ #Tag.define_atribute_methods(attributes_hash)
6
+ #end
7
+
8
+ #class << self
9
+ #def define_atribute_methods(attributes_hash)
10
+ #attributes_hash.each do|k,v|
11
+ #define_method(k) do v; end
12
+ #end
13
+ #end
14
+ #end
15
+ end
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,125 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ class User < Resource
4
+ attr_reader :badges, :answers, :comments,
5
+ :questions, :reputations,
6
+ :suggested_edits, :tags, :posts, :permissions
7
+
8
+ def initialize(attributes_hash)
9
+ @badges = []
10
+ @answers = []
11
+ @comments = []
12
+ @questions = []
13
+ @reputations = []
14
+ @suggested_edits=[]
15
+ @tags = []
16
+ @posts = []
17
+ @permissions = []
18
+ super
19
+ end
20
+
21
+ class << self
22
+ def parse_data(data)
23
+ users = []
24
+ data.each do|attr_hash|
25
+ if data_has_badge?(attr_hash)
26
+ user = create_user(attr_hash, users, :user)
27
+ user.badges.push(Badge.new(attr_hash))
28
+ elsif data_has_answer?(attr_hash)
29
+ user = create_user(attr_hash, users)
30
+ user.answers.push(Answer.new(attr_hash))
31
+ elsif data_has_comment?(attr_hash)
32
+ user = create_user(attr_hash, users)
33
+ user.comments.push(Comment.new(attr_hash))
34
+ elsif data_has_question?(attr_hash)
35
+ user = create_user(attr_hash, users)
36
+ user.questions.push(Question.new(attr_hash))
37
+ elsif data_has_reputation?(attr_hash)
38
+ user = create_user(attr_hash, users, :user_id)
39
+ user.reputations.push(Reputation.new(attr_hash))
40
+ elsif data_has_suggested_edit?(attr_hash)
41
+ user = create_user(attr_hash, users, :proposing_user)
42
+ user.suggested_edits.push(SuggestedEdit.new(attr_hash))
43
+ elsif data_has_tag_name?(attr_hash)
44
+ user = create_user(attr_hash, users, :user_id)
45
+ user.tags.push(Tag.new(attr_hash))
46
+ elsif data_has_timeline?(attr_hash)
47
+ user = create_user(attr_hash, users, :user_id)
48
+ user.posts.push(Post.new(attr_hash))
49
+ elsif data_has_permission_object?(attr_hash)
50
+ user = create_user(attr_hash, users, :user_id)
51
+ user.permissions.push(Permission.new(attr_hash))
52
+ elsif data_has_notification?(attr_hash)
53
+ users << Notification.new(attr_hash)
54
+ else
55
+ users << new(attr_hash)
56
+ end
57
+ end
58
+ users
59
+ end
60
+
61
+ private
62
+
63
+ def find_or_create_user(users, user_attr)
64
+ user_array = users.select{|u|u.user_id == user_attr[:user_id] }
65
+ !user_array.empty? ? user_array.first : new(user_attr)
66
+ end
67
+
68
+ def create_user(attr_hash, users, hash_key=:owner)
69
+ user_attr = attr_hash.delete(hash_key)
70
+ user_attr = user_attr.is_a?(Hash) ? user_attr : {user_id: user_attr }
71
+ user = find_or_create_user(users, user_attr)
72
+ users << user unless user_exists?(users, user_attr[:user_id])
73
+ user
74
+ end
75
+
76
+ def user_exists?(users, user_id)
77
+ user_array = users.select{|u|u.user_id == user_id }
78
+ !user_array.empty?
79
+ end
80
+
81
+ def data_has_badge?(data)
82
+ data.include?(:badge_id)
83
+ end
84
+
85
+ def data_has_answer?(data)
86
+ data.include?(:answer_id)
87
+ end
88
+ def data_has_comment?(data)
89
+ data.include?(:comment_id) && !data.include?(:timeline_type)
90
+ end
91
+
92
+ def data_has_question?(data)
93
+ data.include?(:question_id)
94
+ end
95
+
96
+ def data_has_reputation?(data)
97
+ data.include?(:reputation_change)
98
+ end
99
+
100
+ def data_has_suggested_edit?(data)
101
+ data.include?(:suggested_edit_id)
102
+ end
103
+
104
+ def data_has_tag_name?(data)
105
+ data.include?(:name) || data_has_tag_score?(data)
106
+ end
107
+
108
+ def data_has_tag_score?(data)
109
+ data.include?(:tag_name) && data.include?(:answer_score)
110
+ end
111
+ def data_has_timeline?(data)
112
+ data.include?(:timeline_type)
113
+ end
114
+
115
+ def data_has_permission_object?(data)
116
+ data.include?(:object_type)
117
+ end
118
+
119
+ def data_has_notification?(data)
120
+ data.include?(:notification_type)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module RubyStackoverflow
3
+ class Client
4
+ class ResponseData
5
+ attr_reader :data, :error
6
+
7
+ def initialize(response, klass)
8
+ if response[:items].nil?
9
+ @error = StackoverflowError.new(response)
10
+ else
11
+ @data = format_data(response[:items], klass)
12
+ end
13
+ end
14
+
15
+ def format_data(data, klass)
16
+ eval(klass.capitalize).parse_data(data)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,152 @@
1
+ module RubyStackoverflow
2
+ class Client
3
+ module UserHelper
4
+ def users(options = {})
5
+ user_response(options)
6
+ end
7
+
8
+ def users_by_ids(ids, options = {})
9
+ ids = join_ids(ids)
10
+ user_response(options, ids)
11
+ end
12
+
13
+ def users_with_answers(ids, options = {})
14
+ ids = join_ids(ids)
15
+ url = ids + '/answers'
16
+ user_response(options, url)
17
+ end
18
+
19
+ def users_with_badges(ids, options = {})
20
+ ids = join_ids(ids)
21
+ url = ids + '/badges'
22
+ user_response(options, url)
23
+ end
24
+
25
+ def users_with_comments(ids, options = {})
26
+ ids = join_ids(ids)
27
+ url = ids + '/comments'
28
+ user_response(options, url)
29
+ end
30
+
31
+ def users_with_replied_comments(ids, toid, options = {})
32
+ ids = join_ids(ids)
33
+ url = ids + '/comments/' + toid
34
+ user_response(options, url)
35
+ end
36
+
37
+ def users_notifications(id, options = {})
38
+ url = id + '/notifications'
39
+ user_response(options, url)
40
+ end
41
+
42
+ def users_unread_notifications(id, options = {})
43
+ url = id + '/notifications/unread'
44
+ user_response(options, url)
45
+ end
46
+
47
+ def users_with_favorites_questions(ids, options = {})
48
+ ids = join_ids(ids)
49
+ url = ids + '/favorites'
50
+ user_response(options, url)
51
+ end
52
+
53
+ def users_with_mentioned_comments(ids, options = {})
54
+ ids = join_ids(ids)
55
+ url = ids + '/mentioned'
56
+ user_response(options, url)
57
+ end
58
+
59
+ def users_questions(ids, options = {})
60
+ ids = join_ids(ids)
61
+ url = ids + '/questions'
62
+ user_response(options, url)
63
+ end
64
+
65
+ def users_featured_questions(ids, options = {})
66
+ ids = join_ids(ids)
67
+ url = ids + '/questions/featured'
68
+ user_response(options, url)
69
+ end
70
+
71
+ def users_noanswers_questions(ids, options = {})
72
+ ids = join_ids(ids)
73
+ url = ids + '/questions/no-answers'
74
+ user_response(options, url)
75
+ end
76
+
77
+ def users_unaccepted_questions(ids, options = {})
78
+ ids = join_ids(ids)
79
+ url = ids + '/questions/unaccepted'
80
+ user_response(options, url)
81
+ end
82
+
83
+ def users_unanswered_questions(ids, options = {})
84
+ ids = join_ids(ids)
85
+ url = ids + '/questions/unanswered'
86
+ user_response(options, url)
87
+ end
88
+
89
+ def users_reputations(ids, options = {})
90
+ ids = join_ids(ids)
91
+ url = ids + '/reputation'
92
+ user_response(options, url)
93
+ end
94
+
95
+ def users_suggested_edits(ids, options = {})
96
+ ids = join_ids(ids)
97
+ url = ids + '/suggested-edits'
98
+ user_response(options, url)
99
+ end
100
+
101
+ def users_tags(ids, options = {})
102
+ ids = join_ids(ids)
103
+ url = ids + '/tags'
104
+ user_response(options, url)
105
+ end
106
+
107
+ def users_top_answers_with_given_tags(id, tags, options = {})
108
+ url = id + '/tags/' + join_ids(tags) + '/top-answers'
109
+ user_response(options, url)
110
+ end
111
+
112
+ def users_top_questions_with_given_tags(id, tags, options = {})
113
+ url = id + '/tags/' + join_ids(tags) + '/top-questions'
114
+ user_response(options, url)
115
+ end
116
+
117
+ def user_top_tags_by_answers(id, options ={})
118
+ url = id + '/top-answer-tags'
119
+ user_response(options, url)
120
+ end
121
+
122
+ def user_top_tags_by_questions(id, options ={})
123
+ url = id + '/top-question-tags'
124
+ user_response(options, url)
125
+ end
126
+
127
+ def users_timeline(ids, options = {})
128
+ ids = join_ids(ids)
129
+ url = ids + '/timeline'
130
+ user_response(options, url)
131
+ end
132
+
133
+ def user_write_permissions(id, options = {})
134
+ url = id + '/write-permissions'
135
+ user_response(options, url)
136
+ end
137
+
138
+ def user_full_reputation_history(id, options = {})
139
+ url = id + '/write-permissions'
140
+ user_response(options, url)
141
+ end
142
+
143
+ private
144
+
145
+
146
+ def user_response(options={}, url='')
147
+ url = 'users/'+ url
148
+ getr(url,'user',options)
149
+ end
150
+ end
151
+ end
152
+ end