Sha256: f8315920e71ebb69b68fd376b26bd5962dffebceb3942e1be47d50d60c1c9546

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

module ArticleMethod
  def create_article(grpc_req, _unused_call)
    logger = Logger.new(STDOUT)
    firestore = Google::Cloud::Firestore.new
    article = firestore.col("articles").doc
    article.set({})
    data = firestore.col("articles").doc(article.document_id)
    article_hash = {
      id: article.document_id,
      user_id: grpc_req.user_id,
      title: grpc_req.title,
      body: grpc_req.body,
      thumnail_url: grpc_req.thumnail_url,
      public_date: grpc_req.public_date,
      is_public: grpc_req.is_public,
      tag: grpc_req.tag.to_a,
      created_at: Time.now.utc,
      updated_at: Time.now.utc
    }
    data.update article_hash
    res = Souls::CreateArticleReply.new(
      { article: article_hash }
    )
    logger.info res.article
    res
  rescue StandardError => error
    logger.debug error
  end

  def get_article(grpc_req, _unused_call)
    logger = Logger.new(STDOUT)
    article = Article.find(grpc_req.id)
    reply = JSON.parse(article.to_json)
    reply["created_at"] = article.created_at.to_s
    reply["updated_at"] = article.updated_at.to_s
    res = Souls::GetArticleReply.new(
      { article: Souls::Article.new(reply) }
    )
    logger.info res.article
    res
  rescue StandardError => error
    logger.debug error
  end

  def get_articles
    return enum_for(:get_articles) unless block_given?
    q = ::Article
    limit = 100
    q.limit(limit).each do |article|
      sleep(rand(0.01..0.3))
      Souls::Article.new article.to_proto
    end
  rescue StandardError => e
    fail!(:internal, :unknown, "Unknown error when listing Articles: #{e.message}")
  end

  def update_article(grpc_req, _unused_call); end

  def delete_article(grpc_req, _unused_call); end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
souls-0.8.3 souls_service-0.0.8/app/controllers/article_method.rb
souls-0.8.2 souls_service-0.0.8/app/controllers/article_method.rb
souls-0.8.1 souls_service-0.0.8/app/controllers/article_method.rb