Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors Client #57

Merged
merged 6 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions lib/app_store_connect.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
# frozen_string_literal: true

require 'jwt'

require 'app_store_connect/authorization'
require 'app_store_connect/parser'

require 'app_store_connect/schema'
require 'app_store_connect/client'
require 'app_store_connect/factory'
require 'app_store_connect/object/type'
require 'app_store_connect/object/attributes'
require 'app_store_connect/object/properties'
require 'app_store_connect/object/data'
require 'app_store_connect/create_request'
require 'app_store_connect/client'
require 'app_store_connect/schema'
require 'app_store_connect/type'
require 'app_store_connect/version'

require 'app_store_connect/bundle_id_create_request'
require 'app_store_connect/device_create_request'
require 'app_store_connect/profile_create_request'
require 'app_store_connect/certificate_create_request'
require 'app_store_connect/device_create_request'
require 'app_store_connect/user_invitation_create_request'
require 'app_store_connect/version'

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

require 'app_store_connect/factory'
require 'app_store_connect/factory/builder/enum'
require 'app_store_connect/profile_create_request'

module AppStoreConnect
Factory.register('enum', Factory::Builder::Enum)

@config = {}

SCHEMA = Schema.new(File.join(__dir__, './config/schema.json'))
Parser.parse!(SCHEMA)

class << self
attr_reader :config
attr_accessor :config

attr_writer :config
def load!
SCHEMA.types.each do |type_schema|
options = type_schema.options
klass = Factory.type(type_schema)
name = options[:type]

AppStoreConnect::Type.const_set(name, klass)
end
end
end

load!
end
2 changes: 2 additions & 0 deletions lib/app_store_connect/authorization.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'jwt'

module AppStoreConnect
class Authorization
AUDIENCE = 'appstoreconnect-v1'
Expand Down
2 changes: 2 additions & 0 deletions lib/app_store_connect/bundle_id_create_request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'app_store_connect/create_request'

module AppStoreConnect
class BundleIdCreateRequest < CreateRequest
data do
Expand Down
2 changes: 2 additions & 0 deletions lib/app_store_connect/certificate_create_request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'app_store_connect/create_request'

module AppStoreConnect
class CertificateCreateRequest < CreateRequest
data do
Expand Down
80 changes: 24 additions & 56 deletions lib/app_store_connect/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require 'active_support/all'

require 'app_store_connect/web_service_endpoint'
require 'app_store_connect/request'
require 'app_store_connect/authorization'

module AppStoreConnect
class Client
Expand All @@ -15,46 +15,37 @@ def initialize(**kwargs)
key_id: @options[:key_id],
issuer_id: @options[:issuer_id]
)
@web_service_endpoints_by_name ||= begin
AppStoreConnect::SCHEMA.web_service_endpoints.map do |config|
[config.fetch('alias').to_sym, WebServiceEndpoint.new(**config.deep_symbolize_keys)]
end.to_h
end
@web_service_endpoints_by_alias ||= AppStoreConnect::SCHEMA
.web_service_endpoints
.map { |s| [s.alias, s] }
.to_h
end

def respond_to_missing?(method_name, include_private = false)
web_service_endpoint_names.include?(method_name) || super
web_service_endpoint_aliases.include?(method_name) || super
end

def method_missing(method_name, *kwargs)
super unless web_service_endpoint_names.include?(method_name)
super unless web_service_endpoint_aliases.include?(method_name)

web_service_endpoint = web_service_endpoint_by(name: method_name)
web_service_endpoint = web_service_endpoint_by(method_name)

call(web_service_endpoint, *kwargs)
end

private

def web_service_endpoint_names
@web_service_endpoints_by_name.keys
def web_service_endpoint_aliases
@web_service_endpoints_by_alias.keys
end

def call(web_service_endpoint, **kwargs)
parser = proc do |response|
JSON.parse(response.body) if response.body
end
raise "invalid http method: #{web_service_endpoint.http_method}" unless %i[get delete post].include?(web_service_endpoint.http_method)

case web_service_endpoint.http_method
when :get
get(web_service_endpoint, **kwargs, &parser)
when :post
post(web_service_endpoint, **kwargs, &parser)
when :delete
delete(web_service_endpoint, **kwargs, &parser)
else
raise "invalid http method: #{web_service_endpoint.http_method}"
end
request = build_request(web_service_endpoint, **kwargs)
response = request.execute

JSON.parse(response.body) if response.body
end

def build_uri(web_service_endpoint, **kwargs)
Expand All @@ -63,8 +54,8 @@ def build_uri(web_service_endpoint, **kwargs)
.gsub(/(\{(\w+)\})/) { kwargs.fetch(Regexp.last_match(2).to_sym) })
end

def web_service_endpoint_by(name:)
@web_service_endpoints_by_name[name]
def web_service_endpoint_by(alias_sym)
@web_service_endpoints_by_alias[alias_sym]
end

def env_options
Expand All @@ -87,18 +78,6 @@ def options(**kwargs)
end
end

def get(web_service_endpoint, **kwargs, &block)
request = Request.new(
kwargs: kwargs,
web_service_endpoint: web_service_endpoint,
http_method: :get,
uri: build_uri(web_service_endpoint, **kwargs),
headers: headers
)

request.execute(&block)
end

def http_body(web_service_endpoint, **kwargs)
"AppStoreConnect::#{web_service_endpoint.http_body_type}"
.constantize
Expand All @@ -108,29 +87,18 @@ def http_body(web_service_endpoint, **kwargs)
.to_json
end

def delete(web_service_endpoint, **kwargs, &block)
request = Request.new(
web_service_endpoint: web_service_endpoint,
def build_request(web_service_endpoint, **kwargs)
options = {
kwargs: kwargs,
http_method: :delete,
web_service_endpoint: web_service_endpoint,
http_method: web_service_endpoint.http_method,
uri: build_uri(web_service_endpoint, **kwargs),
headers: headers
)

request.execute(&block) || true
end
}

def post(web_service_endpoint, **kwargs, &block)
request = Request.new(
web_service_endpoint: web_service_endpoint,
kwargs: kwargs,
http_method: :post,
uri: build_uri(web_service_endpoint, **kwargs),
headers: headers,
http_body: http_body(web_service_endpoint, **kwargs)
)
options[:http_body] = http_body(web_service_endpoint, **kwargs) if web_service_endpoint.http_method == :post

request.execute(&block)
Request.new(options)
end

def headers
Expand Down
2 changes: 2 additions & 0 deletions lib/app_store_connect/device_create_request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'app_store_connect/create_request'

module AppStoreConnect
class DeviceCreateRequest < CreateRequest
data do
Expand Down
20 changes: 3 additions & 17 deletions lib/app_store_connect/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@

module AppStoreConnect
class Factory
class BuilderNotRegistered < StandardError
def initialize(name)
super("Builder not registered: #{name}")
def self.type(schema)
Class.new(Type::Enum) do |base|
base.const_set('VALUES', schema.values)
end
end

def self.register(name, builder)
builders[name] = builder
end

def self.builders
@builders ||= {}
end

def self.build(name, options = {})
builders.fetch(name) do
raise BuilderNotRegistered, name
end.call(options)
end
end
end
15 changes: 0 additions & 15 deletions lib/app_store_connect/factory/builder/enum.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/app_store_connect/parser.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/app_store_connect/profile_create_request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'app_store_connect/create_request'

module AppStoreConnect
class ProfileCreateRequest < CreateRequest
data do
Expand Down
4 changes: 1 addition & 3 deletions lib/app_store_connect/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def initialize(**options)

def execute
Net::HTTP.start(uri.host, uri.port, net_http_options) do |http|
response = http.request(request)

yield response if block_given?
http.request(request)
end
end

Expand Down
21 changes: 10 additions & 11 deletions lib/app_store_connect/schema.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# frozen_string_literal: true

require 'app_store_connect/schema/type'
require 'app_store_connect/schema/web_service_endpoint'

module AppStoreConnect
class Schema
def initialize(path)
@schema = JSON.parse(File.read(path))
end
attr_reader :types, :web_service_endpoints

def types
@schema['Type'].map do |name, options|
[name, Type.new(**options.deep_symbolize_keys)]
end.to_h
end

def web_service_endpoints
@schema['web_service_endpoints']
def initialize(path)
schema = JSON.parse(File.read(path)).deep_symbolize_keys
@types = schema[:types].map do |options|
Type.new(**options)
end
@web_service_endpoints = schema[:web_service_endpoints].map do |s|
WebServiceEndpoint.new(**s)
end
end
end
end
6 changes: 5 additions & 1 deletion lib/app_store_connect/schema/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ class Type
attr_reader :type, :options

def initialize(**options)
@type = options.delete(:type)
@type = options[:type]
@options = options
end

def values
@options[:values]
end
end
end
end
27 changes: 27 additions & 0 deletions lib/app_store_connect/schema/web_service_endpoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module AppStoreConnect
class Schema
class WebServiceEndpoint
def initialize(**options)
@options = options
end

def alias
@options.fetch(:alias).to_sym
end

def http_method
@options[:http_method].to_sym
end

def http_body_type
@options[:http_body_type]
end

def url
@options[:url]
end
end
end
end
2 changes: 2 additions & 0 deletions lib/app_store_connect/type.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'app_store_connect/type/enum'

module AppStoreConnect
module Type
end
Expand Down
Loading