pilha 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  module StackExchange
2
2
  module StackOverflow
3
- class Answer
3
+ class Answer < Base
4
4
  extend Forwardable
5
5
 
6
6
  def_delegators :@struct, :answer_id, :accepted, :answer_comments_url, :question_id,
@@ -10,48 +10,26 @@ module StackExchange
10
10
  class << self
11
11
  attr_reader :client
12
12
 
13
- def find_by_id(id, options = {})
14
- options.merge! :id => id
15
- response = client.request('/answers/:id', options)
16
- OpenStruct.new(parse response)
13
+ def find(id, options = {})
14
+ request('/answers/:id', id, options).answers.first
17
15
  end
18
16
 
19
17
  def find_by_user_id(id, options = {})
20
- options.merge! :id => id
21
- response = client.request('/users/:id/answers', options)
22
- OpenStruct.new(parse response)
18
+ request('/users/:id/answers', id, options)
23
19
  end
24
20
 
25
- private
26
- def setup_associations!(response, hash)
27
- setup_comments! hash
28
- setup_owner! hash
29
- end
30
-
31
- def setup_comments!(hash)
32
- if hash['comments']
33
- hash['comments'] = hash['comments'].map {|c| Comment.new c }
34
- end
35
- end
36
-
37
- def setup_owner!(hash)
38
- if hash['owner']
39
- hash['owner'] = User.new(hash['owner'])
40
- end
41
- end
42
-
43
- def setup_answers!(response)
44
- if response['answers']
45
- response['answers'] = response['answers'].map { |a| Answer.new a }
46
- end
47
- end
21
+ def find_by_question_id(id, options = {})
22
+ request('/questions/:id/answers', id, options)
23
+ end
48
24
 
25
+ private
49
26
  def parse(response)
50
- response['answers'].each do |answer_hash|
51
- setup_associations!(response, answer_hash)
27
+ response['answers'].each do |answer|
28
+ parse_with_class(answer, 'comments', Comment)
29
+ parse_with_class(answer, 'owner', User)
52
30
  end
53
- setup_answers! response
54
- response
31
+ parse_with_class(response, 'answers', Answer)
32
+ OpenStruct.new response
55
33
  end
56
34
  end
57
35
 
@@ -62,7 +40,6 @@ module StackExchange
62
40
  def id
63
41
  @struct.answer_id
64
42
  end
65
-
66
43
  end
67
44
  end
68
45
  end
@@ -1,6 +1,6 @@
1
1
  module StackExchange
2
2
  module StackOverflow
3
- class Comment
3
+ class Comment < Base
4
4
  extend Forwardable
5
5
 
6
6
  def_delegators :@struct, :comment_id, :creation_date, :owner, :post_id,
@@ -10,14 +10,25 @@ module StackExchange
10
10
  attr_reader :client
11
11
 
12
12
  def find_by_id(id, options = {})
13
- options.merge! :id => id
14
- response = client.request('/comments/:id/', options)
13
+ request('/comments/:id/', id, options)
14
+ end
15
15
 
16
- comment = Comment.new(response['comments'].first)
17
- response['comments'] = [comment]
16
+ def find_by_question_id(id, options = {})
17
+ request('/questions/:id/comments', id, options)
18
+ end
18
19
 
19
- OpenStruct.new response
20
+ def find_by_user_id(id, options = {})
21
+ request('/users/:id/comments', id, options)
20
22
  end
23
+
24
+ private
25
+ def parse(response)
26
+ response['comments'].each do |comment|
27
+ parse_with_class(comment, 'owner', User)
28
+ end
29
+ parse_with_class(response, 'comments', Comment)
30
+ OpenStruct.new response
31
+ end
21
32
  end
22
33
 
23
34
  def initialize(hash)
@@ -1,7 +1,7 @@
1
1
  module StackExchange
2
2
  module StackOverflow
3
3
 
4
- class User
4
+ class User < Base
5
5
  extend Forwardable
6
6
 
7
7
  def_delegators :@struct, :user_id, :user_type, :creation_date, :display_name,
@@ -18,21 +18,22 @@ module StackExchange
18
18
  attr_reader :client
19
19
 
20
20
  def all(options = {})
21
- response = client.request('/users', options)
22
- OpenStruct.new(parse response)
21
+ parse client.request('/users', options)
23
22
  end
24
23
 
25
24
  def find_by_badge_id(id, options = {})
26
- options.merge! :id => id
27
- response = client.request('/badges/:id', options)
28
- OpenStruct.new(parse response)
25
+ request('/badges/:id', id, options)
26
+ end
27
+
28
+ def find(id, options = {})
29
+ request('/users/:id', id, options).users.first
29
30
  end
30
31
 
31
32
  private
32
33
  def parse(response)
33
34
  users = response['users'].map { |user| User.new(user) }
34
35
  response['users'] = users
35
- response
36
+ OpenStruct.new response
36
37
  end
37
38
  end
38
39
 
data/lib/pilha.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  path = File.expand_path(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
3
3
 
4
+ require 'rubygems'
4
5
  require 'zlib'
5
6
  require 'json'
6
7
  require 'open-uri'
7
8
  require 'forwardable'
8
9
 
10
+ require 'pilha/stack_overflow/base'
9
11
  require 'pilha/stack_overflow/statistics'
10
12
  require 'pilha/stack_overflow/badge'
11
13
  require 'pilha/stack_overflow/user'
data/pilha.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "pilha"
3
- gem.version = "0.1.3"
3
+ gem.version = "0.1.5"
4
4
  gem.authors = ["Dalto Curvelano Junior"]
5
5
  gem.description = "A ruby wrapper to the Stack Exchange (Stack Overflow and friends) API."
6
6
  gem.summary = "A ruby wrapper to the Stack Exchange (Stack Overflow and friends) API."
@@ -3,11 +3,8 @@ require 'spec_helper'
3
3
  describe StackExchange::StackOverflow::Answer do
4
4
 
5
5
  it 'should return a answer identified by its id' do
6
- response = StackOverflow::Answer.find_by_id(666)
7
- response.total.should == 1
8
- response.pagesize.should == 30
6
+ answer = StackOverflow::Answer.find(666)
9
7
 
10
- answer = response.answers.first
11
8
  answer.id.should == 666
12
9
  answer.accepted.should be_false
13
10
  answer.answer_comments_url.should == "/answers/666/comments"
@@ -21,10 +18,10 @@ describe StackExchange::StackOverflow::Answer do
21
18
  user.email_hash.should == "298e0497aa6b76a573f17e6a2bb22dec"
22
19
  end
23
20
 
24
- it 'should include user association when option :include => :comments' do
25
- response = StackOverflow::Answer.find_by_id 555, :query => { :comments => true }
26
- response.answers.first.comments.size.should_not == 1
27
- comment = response.answers.first.comments.first
21
+ it 'should include comments associations' do
22
+ answer = StackOverflow::Answer.find 555
23
+ answer.comments.size.should_not == 1
24
+ comment = answer.comments.first
28
25
  comment.id.should == 278816
29
26
  end
30
27
 
@@ -36,6 +33,15 @@ describe StackExchange::StackOverflow::Answer do
36
33
  first_answer = response.answers.first
37
34
  first_answer.answer_id.should == 1250987
38
35
  end
36
+
37
+ it 'should find all answers associated to a question' do
38
+ response = StackOverflow::Answer.find_by_question_id(549)
39
+ response.answers.size.should == 20
40
+
41
+ first_answer = response.answers.first
42
+ first_answer.id.should == 2801312
43
+ first_answer.accepted.should be_false
44
+ end
39
45
  end
40
46
 
41
47
 
@@ -16,4 +16,43 @@ describe StackExchange::StackOverflow::Comment do
16
16
  comment.score.should == 7
17
17
  comment.body.should == "not sure why this is getting downvoted -- it is correct! Double check it in your compiler if you don't believe him!"
18
18
  end
19
+
20
+ it 'should get comments associated with a question identified by id' do
21
+ response = StackOverflow::Comment.find_by_question_id 549
22
+ response.total.should == 1
23
+
24
+ comment = response.comments.first
25
+ comment.id.should == 858028
26
+ comment.creation_date.should == 1245970256
27
+
28
+ comment.post_id.should == 549
29
+ comment.post_type.should == "question"
30
+ comment.score.should be_zero
31
+ comment.body.should == "Why exclude HTTP Basic Authentication? It can work in HTML Forms via Ajax: <a href=\"http://www.peej.co.uk/articles/http-auth-with-html-forms.html\" rel=\"nofollow\">peej.co.uk/articles/&hellip;</a>"
32
+
33
+ comment.owner.id.should == 52963
34
+ comment.owner.user_type.should == "registered"
35
+ comment.owner.display_name.should == "system PAUSE"
36
+ comment.owner.reputation.should == 2143
37
+ comment.owner.email_hash.should == "4e47334ae70850cd210b952cdb0cb9be"
38
+ end
39
+
40
+ it 'should get comments associated with a user identified by id' do
41
+ response = StackOverflow::Comment.find_by_user_id 549
42
+ response.total.should == 117
43
+
44
+ comment = response.comments.first
45
+ comment.id.should == 2739275
46
+ comment.creation_date.should == 1272292520
47
+ comment.post_id.should == 2580831
48
+ comment.post_type.should == "answer"
49
+ comment.score.should be_zero
50
+ comment.body.should == "Thanks so much for this. I had a devilish time finding this in JetBrains' documentation."
51
+
52
+ comment.owner.id.should == 549
53
+ comment.owner.user_type.should == "registered"
54
+ comment.owner.display_name.should == "Josh Kodroff"
55
+ comment.owner.reputation.should == 1542
56
+ comment.owner.email_hash.should == "9e7ef08258f04dab37b43842d261aea8"
57
+ end
19
58
  end
@@ -44,4 +44,15 @@ describe StackExchange::StackOverflow::User do
44
44
  response.pagesize.should == 50
45
45
  response.users.size.should == 50
46
46
  end
47
+
48
+ it 'should find a user by its id' do
49
+ user = StackOverflow::User.find 555
50
+ user.id.should == 555
51
+ user.user_type.should == "registered"
52
+ user.creation_date.should == 1218041039
53
+ user.display_name == "Hunter"
54
+ user.reputation.should == 980
55
+ user.email_hash.should == "fed6c543fa3473193cecf34d0ccb1b77"
56
+ user.age.should == 30
57
+ end
47
58
  end
data/spec/spec.opts CHANGED
@@ -1,2 +1,2 @@
1
1
  --color
2
- --format profile
2
+ --format specdoc
data/spec/spec_helper.rb CHANGED
@@ -33,6 +33,10 @@ register(:url => 'badges/9/', :body => 'badges_by_id')
33
33
  register(:url => 'badges/9/?pagesize=50', :body => 'badges_by_id_page2')
34
34
  register(:url => 'badges/tags/', :body => 'badges_tag_based')
35
35
  register(:url => 'answers/666/', :body => 'answers_by_id')
36
- register(:url => 'answers/555/?comments=true', :body => 'answer_with_comments')
36
+ register(:url => 'answers/555/', :body => 'answer_with_comments')
37
37
  register(:url => 'comments/1/', :body => 'comments')
38
38
  register(:url => 'users/1/answers/', :body => 'users_answers')
39
+ register(:url => 'questions/549/answers/', :body => 'answers_by_question_id')
40
+ register(:url => 'questions/549/comments/', :body => 'comments_by_question_id')
41
+ register(:url => 'users/555/', :body => 'users_by_id')
42
+ register(:url => 'users/549/comments/', :body => 'comments_by_user_id')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dalto Curvelano Junior
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-27 00:00:00 -03:00
17
+ date: 2010-05-28 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20