Class: RubyStackoverflow::Client::Question
- Defined in:
- lib/ruby-stackoverflow/client/resource/question.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
readonly
Returns the value of attribute answers.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
- .create_question(attr_hash, questions, hash_key) ⇒ Object
- .data_has_answer?(data) ⇒ Boolean
- .data_has_comment?(data) ⇒ Boolean
- .data_has_timeline?(data) ⇒ Boolean
- .find_or_create_question(questions, qid) ⇒ Object
- .parse_data(data) ⇒ Object
- .question_exists?(questions, question_id) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(attributes_hash = {}) ⇒ Question
constructor
A new instance of Question.
Constructor Details
permalink #initialize(attributes_hash = {}) ⇒ Question
Returns a new instance of Question.
5 6 7 8 9 10 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 5 def initialize(attributes_hash={}) @answers = [] @comments = [] @posts = [] super end |
Instance Attribute Details
permalink #answers ⇒ Object (readonly)
Returns the value of attribute answers.
4 5 6 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 4 def answers @answers end |
permalink #comments ⇒ Object (readonly)
Returns the value of attribute comments.
4 5 6 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 4 def comments @comments end |
permalink #posts ⇒ Object (readonly)
Returns the value of attribute posts.
4 5 6 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 4 def posts @posts end |
permalink #user ⇒ Object (readonly)
Returns the value of attribute user.
4 5 6 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 4 def user @user end |
Class Method Details
permalink .create_question(attr_hash, questions, hash_key) ⇒ Object
[View source]
50 51 52 53 54 55 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 50 def create_question(attr_hash, questions, hash_key) qid = attr_hash.delete(hash_key) question = find_or_create_question(questions, qid) questions << question unless question_exists?(questions,qid) question end |
permalink .data_has_answer?(data) ⇒ Boolean
32 33 34 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 32 def data_has_answer?(data) data.first.include?(:answer_id) end |
permalink .data_has_comment?(data) ⇒ Boolean
36 37 38 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 36 def data_has_comment?(data) data.first.include?(:comment_id) && !data.first.include?(:timeline_type) end |
permalink .data_has_timeline?(data) ⇒ Boolean
40 41 42 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 40 def data_has_timeline?(data) data.first.include?(:timeline_type) end |
permalink .find_or_create_question(questions, qid) ⇒ Object
[View source]
44 45 46 47 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 44 def find_or_create_question(questions, qid) question_array = questions.select{|q|q.question_id == qid} !question_array.empty? ? question_array.first : new({question_id: qid}) end |
permalink .parse_data(data) ⇒ Object
[View source]
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 13 def parse_data(data) questions = [] data.each do|attr_hash| if data_has_answer?(data) question = create_question(attr_hash, questions, :question_id) question.answers.push(Answer.new(attr_hash)) elsif data_has_comment?(data) question = create_question(attr_hash, questions,:post_id) question.comments.push(Comment.new(attr_hash)) elsif data_has_timeline?(data) question = create_question(attr_hash, questions,:question_id) question.posts.push(Post.new(attr_hash)) else questions << new(attr_hash) end end questions end |
permalink .question_exists?(questions, question_id) ⇒ Boolean
57 58 59 60 |
# File 'lib/ruby-stackoverflow/client/resource/question.rb', line 57 def question_exists?(questions, question_id) question_array = questions.select{|q|q.question_id == question_id } !question_array.empty? end |