Sha256: b3000b27bd7539ce91e4c1c40d8d2c80b985494271c98df78ea931195111e990

Contents?: true

Size: 1.82 KB

Versions: 21

Compression:

Stored size: 1.82 KB

Contents

class Eco::API::UseCases::Default::Meta::GraphQLSchema < Eco::API::Common::Loaders::UseCase
  include Eco::API::UseCases::GraphQL::Helpers::Base

  require_relative 'cli/graphql_schema_cli'

  OUT_FOLDER      = "sftp".freeze
  OUT_TIME_FORMAT = '%Y%m%dT%H%M%S'.freeze
  OUT_FILENAME    = "graphql_schema".freeze

  name "graphql-schema"
  type :other

  def main(*_args)
    return unless graphql_schema

    File.open(output_filename, 'wb') { |fd| fd << schema_content }
    log(:info) {
      "Generated file: '#{output_filename}'"
    }
  end

  private

  def schema_content
    case output_format
    when :definition
      graphql_schema.to_definition
    when :json
      graphql_schema.to_json
    when :yaml
      graphql_schema.to_yaml
    else
      raise ArgumentError, "Unknown '-format' #{output_format}"
    end
  end

  def graphql_schema
    @graphql_schema ||= graphql.client.schema
  end

  def output_filename(name = output_basename)
    File.join(output_folder, "#{timestamp}_#{enviro_name}_#{name}.#{output_extension}")
  end

  def enviro_name
    @enviro_name ||= ''.then do
      host = graphql.client.host
      uri  = URI.parse("https://#{host}")
      uri.host.split('.')[0].gsub('-', '_')
    end
  end

  def output_extension
    case output_format
    when :definition then 'graphql'
    when :json       then 'json'
    when :yaml       then 'yml'
    else
      'txt'
    end
  end

  def output_folder
    "#{config.active_enviro}/#{self.class::OUT_FOLDER}"
  end

  def timestamp(date = Time.now)
    date.strftime(self.class::OUT_TIME_FORMAT)
  end

  def output_basename
    options.dig(:output, :file, :name) || self.class::OUT_FILENAME
  end

  def output_format
    @output_format ||= (options.dig(:output, :format) || :definition).to_sym
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.20 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.19 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.18 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.17 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.16 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.15 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.14 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.13 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.12 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.11 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.10 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.9 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.8 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.7 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.6 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.5 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.4 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.3 lib/eco/api/usecases/default/meta/graphql_schema.rb
eco-helpers-3.0.2 lib/eco/api/usecases/default/meta/graphql_schema.rb