Skip to content

Commit

Permalink
Merge pull request #28 from kyledecot/kd-type
Browse files Browse the repository at this point in the history
Add Enum Type Factory
  • Loading branch information
Kyle Decot authored Jul 3, 2019
2 parents 2f66fb8 + b9a6c05 commit 9c2cc05
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/app_store_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
require 'httparty'

require 'app_store_connect/authorization'
require 'app_store_connect/parser'
require 'app_store_connect/client'
require 'app_store_connect/cli'
require 'app_store_connect/bundle_id_create_request'
require 'app_store_connect/user_invitation_create_request'
require 'app_store_connect/version'
require 'app_store_connect/factory'

require 'app_store_connect/type'
require 'app_store_connect/type/enum'

module AppStoreConnect
Parser.parse!
end
28 changes: 28 additions & 0 deletions lib/app_store_connect/factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module AppStoreConnect
class Factory
UnsupportedType = Class.new(StandardError)

TYPES = {
'enum' => 'Enum'
}.freeze

def self.type(type:, **options)
case type
when *TYPES.keys
klass = "AppStoreConnect::Type::#{TYPES[type]}".constantize
Class.new(klass, &send(type, options))
else
raise UnsupportedType, "Unsupported Type: #{type}"
end
end

def self.enum(options)
proc do |base|
base.const_set('VALUES', options.fetch(:values))
end
end
private_class_method :enum
end
end
19 changes: 19 additions & 0 deletions lib/app_store_connect/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module AppStoreConnect
class Parser
def self.parse!
json = JSON.parse(File.read(File.join(__dir__, '../config/api.json')))

parse_types(json['Type'])
end

def self.parse_types(types)
types.each do |name, type|
klass = Factory.type(type.deep_symbolize_keys)

AppStoreConnect::Type.const_set(name, klass)
end
end
end
end
6 changes: 6 additions & 0 deletions lib/app_store_connect/type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module AppStoreConnect
module Type
end
end
19 changes: 19 additions & 0 deletions lib/app_store_connect/type/enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module AppStoreConnect
module Type
class Enum
attr_reader :value

def initialize(value:)
@value = value
end

def self.const_missing(const)
return const_set(const, new(value: const.to_s)) if const.to_s.in?(const_get('VALUES'))

super(const)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/config/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Type": {
"BundleIdPlatform": {
"type": "enum",
"values": [
"IOS",
"MAC_OS"
]
}
}
}

0 comments on commit 9c2cc05

Please sign in to comment.