Class: Ahub::Question

Inherits:
Object
  • Object
show all
Includes:
APIResource
Defined in:
lib/ahub/question.rb

Instance Attribute Summary collapse

Attributes included from APIResource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIResource

#update

Constructor Details

#initialize(attrs) ⇒ Question

Returns a new instance of Question.



30
31
32
33
# File 'lib/ahub/question.rb', line 30

def initialize(attrs)
  super
  @author = Ahub::User.new(attrs[:author]) if attrs[:author]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



28
29
30
# File 'lib/ahub/question.rb', line 28

def body
  @body
end

#body_as_htmlObject

Returns the value of attribute body_as_html.



28
29
30
# File 'lib/ahub/question.rb', line 28

def body_as_html
  @body_as_html
end

#titleObject

Returns the value of attribute title.



28
29
30
# File 'lib/ahub/question.rb', line 28

def title
  @title
end

Class Method Details

.create(title:, body:, topics:, space_id: nil, username:, password:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/ahub/question.rb', line 5

def self.create(title:, body:, topics:, space_id: nil, username:, password:)
  url = "#{base_url}.json"

  payload = {title: title, body: body, topics: topics}
  payload[:spaceId] = space_id if space_id

  user_headers = headers(username:username, password:password)

  create_resource(url: url, payload: payload, headers: user_headers)
end

.find_all_by_text(query:) ⇒ Object



20
21
22
# File 'lib/ahub/question.rb', line 20

def self.find_all_by_text(query:)
  get_resources(url: "#{base_url}.json?q=#{URI.encode(query)}", headers: admin_headers, klass: Ahub::Question)
end

.find_by_title(title) ⇒ Object



24
25
26
# File 'lib/ahub/question.rb', line 24

def self.find_by_title(title)
  find_all_by_text(query: title).find{|question| question.title == title}
end

.is_question(query:) ⇒ Object



16
17
18
# File 'lib/ahub/question.rb', line 16

def self.is_question(query:)
  get_resource(url: "#{base_url}/isQuestion.json?q=#{URI.encode(query)}", headers: admin_headers)[:result]
end

Instance Method Details

#fetched_answersObject



39
40
41
# File 'lib/ahub/question.rb', line 39

def fetched_answers
  @fetched_answers || @answers.map{|answer_id| Ahub::Answer.find(answer_id)}
end

#find_answers_by_username(username) ⇒ Object



35
36
37
# File 'lib/ahub/question.rb', line 35

def find_answers_by_username(username)
  fetched_answers.select{|answer| answer.author.username == username}
end

#html_urlObject



47
48
49
# File 'lib/ahub/question.rb', line 47

def html_url
  "#{Ahub::DOMAIN}/questions/#{id}/#{slug}.html" if id
end

#json_urlObject



51
52
53
# File 'lib/ahub/question.rb', line 51

def json_url
  "#{self.class.base_url}/#{id}.json" if id
end

#to_sObject



55
56
57
# File 'lib/ahub/question.rb', line 55

def to_s
  html_url
end

#userObject



43
44
45
# File 'lib/ahub/question.rb', line 43

def user
  @author
end