Sha256: 0078b32c24f59c0a7a44a9f04e138935b1c6ea917aa8b9934d593b2f85e17fd6

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module Ahub
  class Question
    include Ahub::APIResource
    include Ahub::Deletable

    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.downcase.strip.delete('?'))}", headers: admin_headers, klass: Ahub::Question)
    end

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

    attr_accessor :title, :body, :body_as_html

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

    def find_answers_by_username(username)
      fetched_answers.select{|answer| answer.author.username == username}
    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

1 entries across 1 versions & 1 rubygems

Version Path
ahub-0.11.4 lib/ahub/question.rb