Class: APIGatewayDSL::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/api_gateway_dsl/template.rb,
lib/api_gateway_dsl/template/collection.rb

Defined Under Namespace

Classes: Collection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, **options) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
16
17
18
19
# File 'lib/api_gateway_dsl/template.rb', line 11

def initialize(context, **options)
  @context = context

  @schema   = options[:schema]   || @context.default_body_file
  @velocity = options[:velocity] || @context.default_body_file

  @description  = options[:description].try(:strip_heredoc)
  @content_type = options[:content_type] || 'application/json'
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



4
5
6
# File 'lib/api_gateway_dsl/template.rb', line 4

def content_type
  @content_type
end

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/api_gateway_dsl/template.rb', line 4

def description
  @description
end

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/api_gateway_dsl/template.rb', line 4

def schema
  @schema
end

Class Method Details

.new_if_schema_present(context, **options) ⇒ Object



6
7
8
9
# File 'lib/api_gateway_dsl/template.rb', line 6

def self.new_if_schema_present(context, **options)
  template = new(context, **options)
  template.schema_value ? template : nil
end

Instance Method Details

#as_jsonObject



21
22
23
# File 'lib/api_gateway_dsl/template.rb', line 21

def as_json
  current_dir.join("#{@velocity}.vtl").read
end

#parameterObject



35
36
37
# File 'lib/api_gateway_dsl/template.rb', line 35

def parameter
  Parameter::Body.new(self)
end

#schema_valueObject



25
26
27
28
29
30
31
32
33
# File 'lib/api_gateway_dsl/template.rb', line 25

def schema_value
  return unless current_dir

  if (file = current_dir.join("#{@schema}.json")).exist?
    JSON.parse(file.read)
  elsif (file = current_dir.join("#{@schema}.yml")).exist?
    YAML.safe_load(file.read)
  end
end