Sha256: 7651334cfa67a2f0759d848ce916109a3d46f99c6c330934f97119ffd92649cc

Contents?: true

Size: 1.86 KB

Versions: 22

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'net/http'

module AppStoreConnect
  class Request
    class UnsupportedHTTPMethod < ArgumentError
      def initialize(http_method)
        super "Unsupported HTTP Method: #{http_method}"
      end
    end

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

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

    private

    def web_service_endpoint
      @options.fetch(:web_service_endpoint)
    end

    def query
      return unless http_method == :get

      names = url_parameter_names(web_service_endpoint)

      kwargs
        .reject { |n| names.include?(n) }
        .deep_transform_keys { |k| k.to_s.camelize(:lower) }
        .to_query
    end

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

    def net_http_options
      { use_ssl: uri.scheme == 'https' }
    end

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

    def uri
      @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(:http_body)
    end

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

    def net_http_request_class
      case http_method
      when :get then Net::HTTP::Get
      when :post then Net::HTTP::Post
      when :delete then Net::HTTP::Delete
      when :patch then Net::HTTP::Patch
      else
        raise UnsupportedHTTPMethod, http_method
      end
    end

    def request
      net_http_request_class
        .new(uri, headers)
        .tap { |r| r.body = body if r.request_body_permitted? }
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
app_store_connect-0.37.0 lib/app_store_connect/request.rb
app_store_connect-0.36.0 lib/app_store_connect/request.rb
app_store_connect-0.35.0 lib/app_store_connect/request.rb
app_store_connect-0.34.0 lib/app_store_connect/request.rb
app_store_connect-0.33.0 lib/app_store_connect/request.rb
app_store_connect-0.32.0 lib/app_store_connect/request.rb
app_store_connect-0.30.0 lib/app_store_connect/request.rb
app_store_connect-0.29.0 lib/app_store_connect/request.rb
app_store_connect-0.28.0 lib/app_store_connect/request.rb
app_store_connect-0.27.0 lib/app_store_connect/request.rb
app_store_connect-0.26.0 lib/app_store_connect/request.rb
app_store_connect-0.25.0 lib/app_store_connect/request.rb
app_store_connect-0.23.0 lib/app_store_connect/request.rb
app_store_connect-0.22.0 lib/app_store_connect/request.rb
app_store_connect-0.21.0 lib/app_store_connect/request.rb
app_store_connect-0.20.0 lib/app_store_connect/request.rb
app_store_connect-0.19.0 lib/app_store_connect/request.rb
app_store_connect-0.18.0 lib/app_store_connect/request.rb
app_store_connect-0.17.0 lib/app_store_connect/request.rb
app_store_connect-0.16.0 lib/app_store_connect/request.rb