Class: OASRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_request.rb

Defined Under Namespace

Classes: HTTP, PathTemplate

Class Method Summary collapse

Class Method Details

.spec(oas) ⇒ Object



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
70
71
72
73
74
75
76
77
78
# File 'lib/oas_request.rb', line 5

def self.spec(oas)
  Class.new do
    def initialize(server:, headers: {}, params: {}, query: {})
      @server = server

      @headers = headers
      @params = params
      @query = query
    end

    def __request(method:, url:, options: {})
      # merge params with global defaults
      params = @params.merge(options.fetch(:params, {}))

      # process path template
      url_path = OASRequest::PathTemplate.template url, params

      # construct final host & url parts
      uri = URI "#{@server}#{url_path}"

      # convert query back to regular hash
      search_obj = Rack::Utils.parse_query uri.query

      authorization_headers = {}

      # Overrides
      headers = @headers.merge(authorization_headers).merge(options.fetch(:headers, {}))
      query = search_obj.merge(@query).merge(options.fetch(:query, {}))

      # final query string
      search = Rack::Utils.build_query query

      OASRequest::HTTP.http(
          headers: headers,
          host: uri.host,
          method: method,
          port: uri.port,
          body: options.fetch(:body, nil),
          path: uri.path + (search.empty? ? "" : "?#{search}"),
          protocol: uri.scheme
      )
    end


    oas["paths"].each do |url, methods|
      methods.each do |method, definition|
        # filter to paths that contain an operationId
        next unless definition.is_a?(Hash) && definition["operationId"]

        operation_id = definition["operationId"]
        underscored_operation_id = operation_id.underscore

        request_method = Proc.new do |headers: {}, params: {}, query: {}, body: nil|
          __request(
              method: method,
              url: url,
              options: {
                  headers: headers,
                  params: params,
                  query: query,
                  body: body
              }
          )
        end

        # process each method
        define_method(operation_id, &request_method)

        # Rubyify it getFooByBar -> get_foo_by_bar
        define_method(underscored_operation_id, &request_method) if operation_id != underscored_operation_id
      end
    end
  end
end