Sha256: 9a2bbc2da1049389f3ec841451efaade66de74623e6064349f026749cbe21fa1

Contents?: true

Size: 1.46 KB

Versions: 21

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require_relative "../base"

module Neetob
  class CLI
    module Heroku
      class Stack < Base
        attr_accessor :app

        def initialize(app)
          super()
          @app = app
        end

        def run
          raise(StandardError, "HEROKU_API_KEY is not given") if ENV["HEROKU_API_KEY"].nil?

          url = create_url(app)
          response = get(url)
          ui.success(response[:build_stack][:name])
        end

        private

          def create_url(app)
            "https://api.heroku.com/apps/#{app}"
          end

          def parse_response(http_result)
            case http_result
            when Net::HTTPSuccess
              JSON.parse(http_result.body, symbolize_names: true)
            else
              error_message = JSON.parse(http_result.body)["message"]
              raise(StandardError, "Request failed with status code #{http_result.code}: #{error_message}")
            end
          end

          def get(url,
headers: { "Accept" => "application/vnd.heroku+json; version=3", "Authorization" => "Bearer #{ENV["HEROKU_API_KEY"]}" })
            uri = URI(url)
            request = Net::HTTP::Get.new(uri)
            headers.each { |key, value| request[key] = value }

            response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
              http.request(request)
            end

            parse_response(response)
          end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
neetob-0.5.16 lib/neetob/cli/heroku/stack.rb
neetob-0.5.15 lib/neetob/cli/heroku/stack.rb
neetob-0.5.14 lib/neetob/cli/heroku/stack.rb
neetob-0.5.13 lib/neetob/cli/heroku/stack.rb
neetob-0.5.12 lib/neetob/cli/heroku/stack.rb
neetob-0.5.11 lib/neetob/cli/heroku/stack.rb
neetob-0.5.10 lib/neetob/cli/heroku/stack.rb
neetob-0.5.9 lib/neetob/cli/heroku/stack.rb
neetob-0.5.8 lib/neetob/cli/heroku/stack.rb
neetob-0.5.7 lib/neetob/cli/heroku/stack.rb
neetob-0.5.6 lib/neetob/cli/heroku/stack.rb
neetob-0.5.5 lib/neetob/cli/heroku/stack.rb
neetob-0.5.4 lib/neetob/cli/heroku/stack.rb
neetob-0.5.3 lib/neetob/cli/heroku/stack.rb
neetob-0.5.2 lib/neetob/cli/heroku/stack.rb
neetob-0.5.1 lib/neetob/cli/heroku/stack.rb
neetob-0.5.0 lib/neetob/cli/heroku/stack.rb
neetob-0.4.37 lib/neetob/cli/heroku/stack.rb
neetob-0.4.36 lib/neetob/cli/heroku/stack.rb
neetob-0.4.35 lib/neetob/cli/heroku/stack.rb