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

Cleanup #47

Merged
merged 1 commit into from
Aug 14, 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
Binary file added .DS_Store
Binary file not shown.
80 changes: 32 additions & 48 deletions lib/app_store_connect/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'app_store_connect/request'

module AppStoreConnect
class Client # rubocop:disable Metrics/ClassLength
class Client
def initialize(**kwargs)
@options = options(**kwargs)

Expand Down Expand Up @@ -41,27 +41,24 @@ def web_service_endpoint_names
end

def call(web_service_endpoint, **kwargs)
parser = proc do |response|
JSON.parse(response.body)
end

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

def build_url(web_service_endpoint, **kwargs)
web_service_endpoint
.url
.gsub(/(\{(\w+)\})/) { kwargs.fetch(Regexp.last_match(2).to_sym) }
end

def url_parameter_names(web_service_endpoint)
web_service_endpoint
def build_uri(web_service_endpoint, **kwargs)
URI(web_service_endpoint
.url
.scan(/(\{(\w+)\})/)
.map { |_, n| n.to_sym }
.gsub(/(\{(\w+)\})/) { kwargs.fetch(Regexp.last_match(2).to_sym) })
end

def web_service_endpoint_by(name:)
Expand All @@ -88,49 +85,36 @@ def options(**kwargs)
end
end

def build_body(request)
request
.to_h
.deep_transform_keys { |k| k.to_s.camelize(:lower) }
.to_json
end

def build_query(web_service_endpoint, **kwargs)
query_parameters = kwargs.dup.tap do |hash|
url_parameter_names(web_service_endpoint).each do |name|
hash.delete(name.to_sym)
end
end

query_parameters.to_query
end

def get(web_service_endpoint, **kwargs)
url = build_url(web_service_endpoint, **kwargs)
query = build_query(web_service_endpoint, **kwargs)

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

request.execute
request.execute(&block)
end

def post(web_service_endpoint, **kwargs)
url = build_url(web_service_endpoint, **kwargs)
request = "AppStoreConnect::#{web_service_endpoint.http_body_type}".constantize.new(**kwargs)
body = build_body(request)

response = execute(:post, url, headers: headers, body: body)

response
def http_body(web_service_endpoint, **kwargs)
"AppStoreConnect::#{web_service_endpoint.http_body_type}"
.constantize
.new(**kwargs)
.to_json
end

def execute(http_method, url, **options)
HTTParty.send(http_method, url, options)
def post(web_service_endpoint, **kwargs, &block)
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)
)

request.execute(&block)
end

def headers
Expand Down
63 changes: 46 additions & 17 deletions lib/app_store_connect/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@
require 'net/http'

module AppStoreConnect
attr_reader :uri

class Request
def initialize(**options)
@uri = options.fetch(:uri)
@options = options
end

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

JSON.parse(response.body)
yield response if block_given?
end
end

private

def web_service_endpoint
@options.fetch(:web_service_endpoint)
end

def query
return unless http_method == :get

kwargs.dup.tap do |hash|
url_parameter_names(web_service_endpoint).each do |name|
hash.delete(name.to_sym)
end
end.to_query
end

def http_method
@options.fetch(:http_method).to_sym
end
Expand All @@ -26,33 +43,45 @@ def net_http_options
{ use_ssl: uri.scheme == 'https' }
end

def query
return unless http_method == :get
return unless @options[:query]

@options[:query]
def kwargs
@options.fetch(:kwargs, {})
end

def uri
URI(@options.fetch(:url)).tap do |uri|
uri.query = query
@options.fetch(:uri).tap do |uri|
uri.query = query if http_method == :get
end
end

def headers
@options.fetch(:headers, {})
end

def body
return if http_method == :get

@options
.fetch(:request)
.to_h
.deep_transform_keys { |k| k.to_s.camelize(:lower) }
.to_json
end

def url_parameter_names(web_service_endpoint)
web_service_endpoint
.url
.scan(/(\{(\w+)\})/)
.map { |_, n| n.to_sym }
end

def request
case http_method
when :get
Net::HTTP::Get.new(uri).tap do |request|
headers.each do |key, value|
request[key] = value
end
end
Net::HTTP::Get.new(uri, headers)
when :post
Net::HTTP::Post.new(uri, headers)
Net::HTTP::Post.new(uri, headers).tap do |request|
request.body = body
end
else
raise "unsupported http method: #{http_method}"
end
Expand Down