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

notEthan / scorpio / 21613631388

03 Feb 2026 01:51AM UTC coverage: 85.331%. First build
21613631388

push

github

notEthan
Merge branches 'mv_openapi', 'google', 'test_resource_base', 'dev641' and 'doc' into HEAD

132 of 170 new or added lines in 10 files covered. (77.65%)

1210 of 1418 relevant lines covered (85.33%)

384.36 hits per line

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

83.0
/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_0::Document]
16
        def from_instance(instance, **new_param)
14✔
17
          if instance.is_a?(Scorpio::OpenAPI::Document)
126✔
18
            instance
×
19
          elsif instance.is_a?(JSI::Base)
124✔
20
            raise(TypeError, "instance is unexpected JSI type: #{instance.class.inspect}")
×
21
          elsif instance.respond_to?(:to_hash)
124✔
22
            if (instance['swagger'].is_a?(String) && instance['swagger'] =~ /\A2(\.|\z)/) || instance['swagger'] == 2
126✔
23
              instance = Scorpio::OpenAPI::V2::Document.new_jsi(instance, **new_param)
×
24
            elsif (instance['openapi'].is_a?(String) && instance['openapi'] =~ /\A3\.0(\.|\z)/) || instance['openapi'] == 3.0
126✔
25
              instance = Scorpio::OpenAPI::V3::Document.new_jsi(instance, **new_param)
126✔
26
            elsif instance['kind'] == 'discovery#restDescription'
NEW
27
              Scorpio::Google::RestDescription.new_jsi(instance, register: true, **new_param)
×
28
            else
29
              raise(ArgumentError, "instance does not look like a recognized openapi document")
×
30
            end
31
          else
32
            raise(TypeError, "instance does not look like a hash (json object)")
×
33
          end
34
        end
35
      end
36

37
      module Configurables
14✔
38
        attr_writer :request_headers
14✔
39
        def request_headers
14✔
40
          return @request_headers if instance_variable_defined?(:@request_headers)
5,264✔
41
          {}.freeze
5,264✔
42
        end
43

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

50
        attr_writer :faraday_builder
14✔
51
        def faraday_builder
14✔
52
          return @faraday_builder if instance_variable_defined?(:@faraday_builder)
252✔
53
          nil
144✔
54
        end
55

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

62
        attr_writer :logger
14✔
63
        def logger
14✔
64
          return @logger if instance_variable_defined?(:@logger)
1,036✔
65
          (Object.const_defined?(:Rails) && ::Rails.respond_to?(:logger) ? ::Rails.logger : nil)
1,036✔
66
        end
67
      end
68
      include Configurables
14✔
69

70
      def v2?
14✔
71
        is_a?(OpenAPI::V2::Document)
14✔
72
      end
73

74
      def v3?
14✔
75
        is_a?(OpenAPI::V3_0::Document)
×
76
      end
77

78
      def operations
14✔
79
        return @operations if instance_variable_defined?(:@operations)
476✔
80
        @operations = OperationsScope.new(each_operation)
126✔
81
      end
82

83
      def each_operation(&block)
14✔
84
        return(to_enum(__method__)) unless block
588✔
85

86
        paths.each do |path, path_item|
462✔
87
          path_item.each do |http_method, operation|
952✔
88
            if operation.is_a?(Scorpio::OpenAPI::Operation)
1,162✔
89
              yield(operation)
1,162✔
90
            end
91
          end
92
        end
93
      end
94

95
      def title
14✔
96
        info && info.title
154✔
97
      end
98
    end
99

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

128
          attr_writer :request_media_type
14✔
129
          def request_media_type
14✔
130
            return @request_media_type if instance_variable_defined?(:@request_media_type)
1,442✔
131
            nil
824✔
132
          end
133
        end
134
        include Configurables
14✔
135
        include(OpenAPI::Document)
14✔
136
      end
137
    end
138

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

153
          def server
14✔
154
            nil
155
          end
156
          def server_variables
14✔
157
            nil
158
          end
159

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

175
          attr_writer :request_media_type
14✔
176
          def request_media_type
14✔
177
            return @request_media_type if instance_variable_defined?(:@request_media_type)
×
178
            if consumes.respond_to?(:to_ary)
×
179
              Request.best_media_type(consumes)
×
180
            else
181
              nil
182
            end
183
          end
184
        end
185
        include Configurables
14✔
186
        include(OpenAPI::Document)
14✔
187
      end
188
    end
189
  end
190
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