Sha256: 8fa0ae50b2b8289dae6c2c9413bf201c9543b4128922f35802c7514ae9e9a136

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require "uri"
require_relative "../../../boilercode"

module Boilercode
  module Commands
    class Bookmarks
      class Create < Boilercode::Command
        def initialize(options)
          @options = options
          @check_valid_url = proc { |url|
            uri_regex = URI::DEFAULT_PARSER.make_regexp(%w[http https])
            !!(url =~ uri_regex)
          }
        end

        def execute(input: $stdin, output: $stdout)
          res = client.post("/bookmarks", payload)
          if res.code == 201
            response = parse_json(res)
            output.puts pastel.green response.url
          else
            output.puts pastel.red res.body
          end
        end

        private

        def title
          prompt.ask("Enter a bookmark title (Optional)")
        end

        def url
          prompt.ask("Provide a URL to bookmark") do |q|
            q.required true
            q.validate proc { |s| valid_url?(s) }, "Not valid"
          end
        end

        def valid_url?(url)
          uri_regex = URI::DEFAULT_PARSER.make_regexp(%w[http https])
          !!(url =~ uri_regex)
        end

        def payload
          {bookmark: {url: url}}
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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