Sha256: 73cb33e8216a3a87207dab8ee677e7e1bfe9cd577f24386f6590d9c0115f0c5a

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Ahub
  class Question
    include Ahub::APIResource

    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

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

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

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

    attr_accessor :title, :body, :body_as_html

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

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

    def user
      @author
    end

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

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

    def to_s
      html_url
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ahub-0.7.0 lib/ahub/question.rb
ahub-0.6.0 lib/ahub/question.rb
ahub-0.5.2 lib/ahub/question.rb