Sha256: fc7a0ded77d171b9f450186ba5565a07a6fa13e209ebc10f7c3e416901ea8fa5

Contents?: true

Size: 1.31 KB

Versions: 30

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require_relative '../helpers/os_helper'
require_relative '../github/api_interface'
require_relative '../github/gist'

module Geet
  module Services
    class CreateGist
      include Geet::Helpers::OsHelper

      API_TOKEN_KEY = 'GITHUB_API_TOKEN'
      DEFAULT_GIT_CLIENT = Geet::Utils::GitClient.new

      def initialize(out: $stdout)
        @out = out

        api_token = extract_env_api_token
        @api_interface = Geet::Github::ApiInterface.new(api_token)
      end

      # options:
      #   :description
      #   :publik:      defaults to false
      #   :no_browse    defaults to false
      #
      def execute(full_filename, description: nil, publik: false, no_browse: false)
        content = IO.read(full_filename)

        gist_access = publik ? 'public' : 'private'
        @out.puts "Creating a #{gist_access} gist..."

        filename = File.basename(full_filename)
        gist = Geet::Github::Gist.create(filename, content, @api_interface, description: description, publik: publik)

        if no_browse
          @out.puts "Gist address: #{gist.link}"
        else
          open_file_with_default_application(gist.link)
        end
      end

      private

      def extract_env_api_token
        ENV[API_TOKEN_KEY] || raise("#{API_TOKEN_KEY} not set!")
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
geet-0.14.0 lib/geet/services/create_gist.rb
geet-0.13.0 lib/geet/services/create_gist.rb
geet-0.12.0 lib/geet/services/create_gist.rb
geet-0.11.0 lib/geet/services/create_gist.rb
geet-0.10.0 lib/geet/services/create_gist.rb
geet-0.9.0 lib/geet/services/create_gist.rb
geet-0.8.0 lib/geet/services/create_gist.rb
geet-0.7.0 lib/geet/services/create_gist.rb
geet-0.6.0 lib/geet/services/create_gist.rb
geet-0.5.0 lib/geet/services/create_gist.rb
geet-0.4.4 lib/geet/services/create_gist.rb
geet-0.4.3 lib/geet/services/create_gist.rb
geet-0.4.2 lib/geet/services/create_gist.rb
geet-0.4.1 lib/geet/services/create_gist.rb
geet-0.4.0 lib/geet/services/create_gist.rb
geet-0.3.18 lib/geet/services/create_gist.rb
geet-0.3.17 lib/geet/services/create_gist.rb
geet-0.3.16 lib/geet/services/create_gist.rb
geet-0.3.15 lib/geet/services/create_gist.rb
geet-0.3.14 lib/geet/services/create_gist.rb