1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
# frozen_string_literal: true
require 'active_support/inflector'
module AppStoreConnect
class Request
module Builder
class Create
TEMPLATE = <<~SOURCE
class <%= name %>CreateRequest < Request::Body
data do
type '<%= type %>'
<%- if properties.any? -%>
attributes do
<%- properties.each do |property| -%>
property :<%= property %>
<%- end -%>
end
<%- end -%>
end
end
SOURCE
private_constant :TEMPLATE
attr_reader :name, :type, :properties
def self.from(schema)
type = schema.properties['data']['properties']['type']['enum'][0]
properties = schema.properties['data']['properties']['attributes']['properties'].keys
new(type, properties)
end
def initialize(type, properties, version = 'v1')
@name = type.singularize.classify
@name = name
@type = type
@properties = properties
@version = version
end
def source
@source ||= begin
require 'erb'
erb = ERB.new(TEMPLATE, trim_mode: '%<>-')
erb.result(binding)
end
@source
end
def alias
"create_#{@type.underscore.singularize}"
end
def url
"https://api.appstoreconnect.apple.com/#{@version}/#{@type}"
end
def http_method
'post'
end
end
end
end
end
|