Class: Ahub::Question

Inherits:
Object
  • Object
show all
Includes:
APIResource, Deletable
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 Deletable

#delete, #undelete

Methods included from APIResource

#update

Constructor Details

#initialize(attrs) ⇒ Question

Returns a new instance of Question.



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

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#body_as_htmlObject

Returns the value of attribute body_as_html.



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

def body_as_html
  @body_as_html
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

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



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

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



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

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

.find_by_title(title) ⇒ Object



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

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

.is_question(query:) ⇒ Object



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

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



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

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

#find_answers_by_username(username) ⇒ Object



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

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

#html_urlObject



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

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

#json_urlObject



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

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

#to_sObject



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

def to_s
  html_url
end

#userObject



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

def user
  @author
end