• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

notEthan / scorpio / 12043223725

27 Nov 2024 03:30AM UTC coverage: 86.188%. First build
12043223725

push

github

notEthan
Merge branches 'mv_openapi', 'dev' and 'infra' into HEAD

41 of 48 new or added lines in 8 files covered. (85.42%)

1092 of 1267 relevant lines covered (86.19%)

313.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.51
/lib/scorpio/openapi/document.rb
1
# frozen_string_literal: true
2

3
module Scorpio
14✔
4
  module OpenAPI
14✔
5
    # A document that defines or describes an API.
6
    # An OpenAPI description document uses and conforms to the OpenAPI Specification.
7
    #
8
    # Scorpio::OpenAPI::Document is a module common to V2 and V3 documents.
9
    module Document
14✔
10
      class << self
14✔
11
        # takes a document, generally a Hash, and returns a Scorpio OpenAPI Document
12
        # instantiating it.
13
        #
14
        # @param instance [#to_hash] the document to represent as a Scorpio OpenAPI Document
15
        # @return [Scorpio::OpenAPI::V2::Document, Scorpio::OpenAPI::V3::Document]
16
        def from_instance(instance, **new_param)
14✔
17
          if instance.is_a?(Scorpio::OpenAPI::Document)
14✔
18
            instance
×
19
          elsif instance.is_a?(JSI::Base)
12✔
20
            raise(TypeError, "instance is unexpected JSI type: #{instance.class.inspect}")
×
21
          elsif instance.respond_to?(:to_hash)
12✔
22
            if instance['swagger'] =~ /\A2(\.|\z)/
14✔
NEW
23
              instance = Scorpio::OpenAPI::V2::Document.new_jsi(instance, **new_param)
×
24
            elsif instance['openapi'] =~ /\A3(\.|\z)/
12✔
25
              instance = Scorpio::OpenAPI::V3::Document.new_jsi(instance, **new_param)
14✔
26
            else
27
              raise(ArgumentError, "instance does not look like a recognized openapi document")
×
28
            end
29
          else
30
            raise(TypeError, "instance does not look like a hash (json object)")
×
31
          end
32
        end
33
      end
34

35
      module Configurables
14✔
36
        attr_writer :request_headers
14✔
37
        def request_headers
14✔
38
          return @request_headers if instance_variable_defined?(:@request_headers)
3,990✔
39
          {}.freeze
3,990✔
40
        end
41

42
        attr_writer :user_agent
14✔
43
        def user_agent
14✔
44
          return @user_agent if instance_variable_defined?(:@user_agent)
1,484✔
45
          "Scorpio/#{Scorpio::VERSION} (https://github.com/notEthan/scorpio) Faraday/#{Faraday::VERSION} Ruby/#{RUBY_VERSION}"
1,484✔
46
        end
47

48
        attr_writer :faraday_builder
14✔
49
        def faraday_builder
14✔
50
          return @faraday_builder if instance_variable_defined?(:@faraday_builder)
70✔
51
          -> (_) { }
70✔
52
        end
53

54
        attr_writer :faraday_adapter
14✔
55
        def faraday_adapter
14✔
56
          return @faraday_adapter if instance_variable_defined?(:@faraday_adapter)
742✔
57
          [Faraday.default_adapter].freeze
742✔
58
        end
59

60
        attr_writer :logger
14✔
61
        def logger
14✔
62
          return @logger if instance_variable_defined?(:@logger)
742✔
63
          (Object.const_defined?(:Rails) && ::Rails.respond_to?(:logger) ? ::Rails.logger : nil)
742✔
64
        end
65
      end
66
      include Configurables
14✔
67

68
      def v2?
14✔
69
        is_a?(OpenAPI::V2::Document)
14✔
70
      end
71

72
      def v3?
14✔
73
        is_a?(OpenAPI::V3::Document)
×
74
      end
75

76
      def operations
14✔
77
        return @operations if instance_variable_defined?(:@operations)
84✔
78
        @operations = OperationsScope.new(each_operation)
14✔
79
      end
80

81
      def each_operation(&block)
14✔
82
        return(to_enum(__method__)) unless block
98✔
83

84
        paths.each do |path, path_item|
84✔
85
          path_item.each do |http_method, operation|
196✔
86
            if operation.is_a?(Scorpio::OpenAPI::Operation)
294✔
87
              yield(operation)
294✔
88
            end
89
          end
90
        end
91
      end
92
    end
93

94
    module Document
14✔
95
      module V3Methods
14✔
96
        module Configurables
14✔
97
          def scheme
14✔
98
            nil
99
          end
100
          attr_writer :server
14✔
101
          def server
14✔
102
            return @server if instance_variable_defined?(:@server)
1,512✔
103
            if servers.respond_to?(:to_ary) && servers.size == 1
1,512✔
104
              servers.first
1,512✔
105
            else
106
              nil
107
            end
108
          end
109
          attr_writer :server_variables
14✔
110
          def server_variables
14✔
111
            return @server_variables if instance_variable_defined?(:@server_variables)
1,512✔
112
            {}.freeze
×
113
          end
114
          attr_writer :base_url
14✔
115
          def base_url(scheme: nil, server: self.server, server_variables: self.server_variables)
14✔
116
            return @base_url if instance_variable_defined?(:@base_url)
1,512✔
117
            if server
1,512✔
118
              server.expanded_url(server_variables)
1,512✔
119
            end
120
          end
121

122
          attr_writer :request_media_type
14✔
123
          def request_media_type
14✔
124
            return @request_media_type if instance_variable_defined?(:@request_media_type)
910✔
125
            nil
520✔
126
          end
127
        end
128
        include Configurables
14✔
129
        include(OpenAPI::Document)
14✔
130
      end
131
    end
132

133
    module Document
14✔
134
      module V2Methods
14✔
135
        module Configurables
14✔
136
          attr_writer :scheme
14✔
137
          def scheme
14✔
138
            return @scheme if instance_variable_defined?(:@scheme)
×
139
            if schemes.nil?
×
140
              'https'
×
141
            elsif schemes.respond_to?(:to_ary)
142
              # prefer https, then http, then anything else since we probably don't support.
143
              schemes.sort_by { |s| ['https', 'http'].index(s) || (1.0 / 0) }.first
×
144
            end
145
          end
146

147
          def server
14✔
148
            nil
149
          end
150
          def server_variables
14✔
151
            nil
152
          end
153

154
          attr_writer :base_url
14✔
155
          # the base url to which paths are appended.
156
          # by default this looks at the openapi document's schemes, picking https or http first.
157
          # it looks at the openapi_document's host and basePath.
158
          def base_url(scheme: self.scheme, server: nil, server_variables: nil)
14✔
159
            return @base_url if instance_variable_defined?(:@base_url)
×
160
            if host && scheme
×
161
              Addressable::URI.new(
162
                scheme: scheme,
163
                host: host,
164
                path: basePath,
165
              ).freeze
166
            end
167
          end
168

169
          attr_writer :request_media_type
14✔
170
          def request_media_type
14✔
171
            return @request_media_type if instance_variable_defined?(:@request_media_type)
×
172
            if consumes.respond_to?(:to_ary)
×
173
              Request.best_media_type(consumes)
×
174
            else
175
              nil
176
            end
177
          end
178
        end
179
        include Configurables
14✔
180
        include(OpenAPI::Document)
14✔
181
      end
182
    end
183
  end
184
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc