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

notEthan / scorpio / 13386690045

18 Feb 2025 08:51AM UTC coverage: 85.102%. First build
13386690045

push

github

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

133 of 175 new or added lines in 10 files covered. (76.0%)

1211 of 1423 relevant lines covered (85.1%)

383.02 hits per line

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

80.19
/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

36
        # Parses JSON from the given file and passes that to {from_instance}
37
        # @param filename [String]
38
        def from_json_file(filename, **new_param)
14✔
NEW
39
          instance = JSON.parse(File.open(filename, 'r:UTF-8', &:read), freeze: true)
×
NEW
40
          from_instance(instance, **new_param)
×
41
        end
42

43
        # Parses YAML from the given file and passes that to {from_instance}
44
        # @param filename [String]
45
        def from_yaml_file(filename, **new_param)
14✔
NEW
46
          instance = YAML.safe_load_file(filename, freeze: true)
×
NEW
47
          from_instance(instance, **new_param)
×
48
        end
49
      end
50

51
      module Configurables
14✔
52
        attr_writer :request_headers
14✔
53
        def request_headers
14✔
54
          return @request_headers if instance_variable_defined?(:@request_headers)
5,264✔
55
          {}.freeze
5,264✔
56
        end
57

58
        attr_writer :user_agent
14✔
59
        def user_agent
14✔
60
          return @user_agent if instance_variable_defined?(:@user_agent)
2,072✔
61
          "Scorpio/#{Scorpio::VERSION} (https://github.com/notEthan/scorpio) Faraday/#{Faraday::VERSION} Ruby/#{RUBY_VERSION}"
2,072✔
62
        end
63

64
        attr_writer :faraday_builder
14✔
65
        def faraday_builder
14✔
66
          return @faraday_builder if instance_variable_defined?(:@faraday_builder)
252✔
67
          nil
144✔
68
        end
69

70
        attr_writer :faraday_adapter
14✔
71
        def faraday_adapter
14✔
72
          return @faraday_adapter if instance_variable_defined?(:@faraday_adapter)
854✔
73
          [Faraday.default_adapter].freeze
854✔
74
        end
75

76
        attr_writer :logger
14✔
77
        def logger
14✔
78
          return @logger if instance_variable_defined?(:@logger)
1,036✔
79
          (Object.const_defined?(:Rails) && ::Rails.respond_to?(:logger) ? ::Rails.logger : nil)
1,036✔
80
        end
81
      end
82
      include Configurables
14✔
83

84
      def v2?
14✔
85
        is_a?(OpenAPI::V2::Document)
14✔
86
      end
87

88
      def v3?
14✔
89
        is_a?(OpenAPI::V3_0::Document)
×
90
      end
91

92
      def operations
14✔
93
        return @operations if instance_variable_defined?(:@operations)
476✔
94
        @operations = OperationsScope.new(each_operation)
126✔
95
      end
96

97
      def each_operation(&block)
14✔
98
        return(to_enum(__method__)) unless block
588✔
99

100
        paths.each do |path, path_item|
462✔
101
          path_item.each do |http_method, operation|
952✔
102
            if operation.is_a?(Scorpio::OpenAPI::Operation)
1,162✔
103
              yield(operation)
1,162✔
104
            end
105
          end
106
        end
107
      end
108

109
      def title
14✔
110
        info && info.title
154✔
111
      end
112
    end
113

114
    module Document
14✔
115
      module V3Methods
14✔
116
        module Configurables
14✔
117
          def scheme
14✔
118
            nil
119
          end
120
          attr_writer :server
14✔
121
          def server
14✔
122
            return @server if instance_variable_defined?(:@server)
1,736✔
123
            if servers.respond_to?(:to_ary) && servers.size == 1
1,736✔
124
              servers.first
1,736✔
125
            else
126
              nil
127
            end
128
          end
129
          attr_writer :server_variables
14✔
130
          def server_variables
14✔
131
            return @server_variables if instance_variable_defined?(:@server_variables)
1,736✔
132
            {}.freeze
×
133
          end
134
          attr_writer :base_url
14✔
135
          def base_url(scheme: nil, server: self.server, server_variables: self.server_variables)
14✔
136
            return @base_url if instance_variable_defined?(:@base_url)
1,736✔
137
            if server
1,736✔
138
              server.expanded_url(server_variables)
1,736✔
139
            end
140
          end
141

142
          attr_writer :request_media_type
14✔
143
          def request_media_type
14✔
144
            return @request_media_type if instance_variable_defined?(:@request_media_type)
1,442✔
145
            nil
824✔
146
          end
147
        end
148
        include Configurables
14✔
149
        include(OpenAPI::Document)
14✔
150
      end
151
    end
152

153
    module Document
14✔
154
      module V2Methods
14✔
155
        module Configurables
14✔
156
          attr_writer :scheme
14✔
157
          def scheme
14✔
158
            return @scheme if instance_variable_defined?(:@scheme)
×
159
            if schemes.nil?
×
160
              'https'
×
161
            elsif schemes.respond_to?(:to_ary)
162
              # prefer https, then http, then anything else since we probably don't support.
163
              schemes.sort_by { |s| ['https', 'http'].index(s) || (1.0 / 0) }.first
×
164
            end
165
          end
166

167
          def server
14✔
168
            nil
169
          end
170
          def server_variables
14✔
171
            nil
172
          end
173

174
          attr_writer :base_url
14✔
175
          # the base url to which paths are appended.
176
          # by default this looks at the openapi document's schemes, picking https or http first.
177
          # it looks at the openapi_document's host and basePath.
178
          def base_url(scheme: self.scheme, server: nil, server_variables: nil)
14✔
179
            return @base_url if instance_variable_defined?(:@base_url)
×
180
            if host && scheme
×
181
              Addressable::URI.new(
182
                scheme: scheme,
183
                host: host,
184
                path: basePath,
185
              ).freeze
186
            end
187
          end
188

189
          attr_writer :request_media_type
14✔
190
          def request_media_type
14✔
191
            return @request_media_type if instance_variable_defined?(:@request_media_type)
×
192
            if consumes.respond_to?(:to_ary)
×
193
              Request.best_media_type(consumes)
×
194
            else
195
              nil
196
            end
197
          end
198
        end
199
        include Configurables
14✔
200
        include(OpenAPI::Document)
14✔
201
      end
202
    end
203
  end
204
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