Sha256: 9a2bbc2da1049389f3ec841451efaade66de74623e6064349f026749cbe21fa1

Contents?: true

Size: 1.46 KB

Versions: 34

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

34 entries across 34 versions & 1 rubygems

Version Path
neetob-0.5.29 lib/neetob/cli/heroku/stack.rb
neetob-0.5.28 lib/neetob/cli/heroku/stack.rb
neetob-0.5.27 lib/neetob/cli/heroku/stack.rb
neetob-0.5.26 lib/neetob/cli/heroku/stack.rb
neetob-0.5.25 lib/neetob/cli/heroku/stack.rb
neetob-0.5.24 lib/neetob/cli/heroku/stack.rb
neetob-0.5.23 lib/neetob/cli/heroku/stack.rb
neetob-0.5.22 lib/neetob/cli/heroku/stack.rb
neetob-0.5.21 lib/neetob/cli/heroku/stack.rb
neetob-0.5.20 lib/neetob/cli/heroku/stack.rb
neetob-0.5.19 lib/neetob/cli/heroku/stack.rb
neetob-0.5.18 lib/neetob/cli/heroku/stack.rb
neetob-0.5.17 lib/neetob/cli/heroku/stack.rb
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