Sha256: 8780ebbb52a03822f75a83bb7abc017fd0f454d9fb9aea23dbbd01060fbd4f56

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require_relative "../../boilercode"

module Boilercode
  module Commands
    class News < Boilercode::Command
      def initialize(options)
        @options = options
      end

      def execute(input: $stdin, output: $stdout)
        res = client.get("/news")
        if res.success?
          handle_search_response(JSON.parse(res.body.to_s))
        else
          puts pastel.red "Something went wrong."
          puts pastel.red res.body
        end
      end

      private

      def handle_search_response(response)
        choice = prompt.select("Choose your article:", articles(response))

        puts pastel.green(article(response, choice)["url"])
        Clipboard.copy(article(response, choice)["url"])
        puts pastel.green("article url copied to clipboard.")
        open_in_browser(article(response, choice)["url"])
      end

      def articles(response)
        @articles ||= response.map { |article| article["name"] }
      end

      def article(response, choice)
        @article ||= response.find { |article| article["name"] == choice }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
boilercode-0.1.2 lib/boilercode/commands/news.rb
boilercode-0.1.1 lib/boilercode/commands/news.rb
boilercode-0.1.0 lib/boilercode/commands/news.rb