oas_request 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/oas_request.rb +72 -0
- data/lib/oas_request/http.rb +84 -0
- data/lib/oas_request/path_template.rb +19 -0
- metadata +86 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 593cfa0b1fa48a445b268d45e90e72b2516579b31a02ead2c4708bd1859abf3a
         | 
| 4 | 
            +
              data.tar.gz: 4593d5538116b316301a812edf816ce440fe6079748317785794923705c57946
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: c6e176558e2915d09bc02843e0bd5e264b5152bb12d8651c43008dafcf1aabef5b60472acc864d552947c0321eea1e800957752eca005390be8d357d4a925ad8
         | 
| 7 | 
            +
              data.tar.gz: 50a7f726a3c938b3f004318b7a16f210081861d7412d46cef4cf16b8c603d24c5467e653827a6937b5bc7e7a9dcc306f3cb283d5a3c34af6b550af851062ac94
         | 
    
        data/lib/oas_request.rb
    ADDED
    
    | @@ -0,0 +1,72 @@ | |
| 1 | 
            +
            require "rack"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class OASRequest
         | 
| 4 | 
            +
              def self.spec(oas)
         | 
| 5 | 
            +
                Class.new do
         | 
| 6 | 
            +
                  attr_reader :values
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  define_method :initialize do |server:, headers: {}, params: {}, query: {}|
         | 
| 9 | 
            +
                    @server = server
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    @headers = headers
         | 
| 12 | 
            +
                    @params = params
         | 
| 13 | 
            +
                    @query = query
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  define_method "__request" do |method:, url:, options: {}|
         | 
| 17 | 
            +
                    # merge params with global defaults
         | 
| 18 | 
            +
                    params = @params.merge(options.fetch(:params, {}))
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                    # process path template
         | 
| 21 | 
            +
                    url_path = OASRequest::PathTemplate.template url, params
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    # construct final host & url parts
         | 
| 24 | 
            +
                    uri = URI "#{@server}#{url_path}"
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                    # convert query back to regular hash
         | 
| 27 | 
            +
                    search_obj = Rack::Utils.parse_query uri.query
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    # Overrides
         | 
| 30 | 
            +
                    headers = @headers.merge(options.fetch(:headers, {}))
         | 
| 31 | 
            +
                    query = search_obj.merge(@query).merge(options.fetch(:query, {}))
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                    # final query string
         | 
| 34 | 
            +
                    search = Rack::Utils.build_query query
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                    OASRequest::HTTP.http(
         | 
| 37 | 
            +
                      headers: headers,
         | 
| 38 | 
            +
                      host: uri.host,
         | 
| 39 | 
            +
                      method: method,
         | 
| 40 | 
            +
                      port: uri.port,
         | 
| 41 | 
            +
                      body: options.fetch(:body, nil),
         | 
| 42 | 
            +
                      path: uri.path + (search.empty? ? "" : "?#{search}"),
         | 
| 43 | 
            +
                      protocol: uri.scheme
         | 
| 44 | 
            +
                    )
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  oas["paths"].each do |url, methods|
         | 
| 48 | 
            +
                    methods.each do |method, definition|
         | 
| 49 | 
            +
                      # filter to paths that contain an operationId
         | 
| 50 | 
            +
                      next unless definition.is_a?(Hash) && definition["operationId"]
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                      # process each method
         | 
| 53 | 
            +
                      define_method definition["operationId"] do |headers: {}, params: {}, query: {}, body: nil|
         | 
| 54 | 
            +
                        __request(
         | 
| 55 | 
            +
                          method: method,
         | 
| 56 | 
            +
                          url: url,
         | 
| 57 | 
            +
                          options: {
         | 
| 58 | 
            +
                            headers: headers,
         | 
| 59 | 
            +
                            params: params,
         | 
| 60 | 
            +
                            query: query,
         | 
| 61 | 
            +
                            body: body
         | 
| 62 | 
            +
                          }
         | 
| 63 | 
            +
                        )
         | 
| 64 | 
            +
                      end
         | 
| 65 | 
            +
                    end
         | 
| 66 | 
            +
                  end
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
            end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            require "oas_request/path_template"
         | 
| 72 | 
            +
            require "oas_request/http"
         | 
| @@ -0,0 +1,84 @@ | |
| 1 | 
            +
            require "net/http"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            REQUEST_CLASSES = {
         | 
| 4 | 
            +
              get: Net::HTTP::Get,
         | 
| 5 | 
            +
              head: Net::HTTP::Head,
         | 
| 6 | 
            +
              post: Net::HTTP::Post,
         | 
| 7 | 
            +
              patch: Net::HTTP::Patch,
         | 
| 8 | 
            +
              put: Net::HTTP::Put,
         | 
| 9 | 
            +
              options: Net::HTTP::Options,
         | 
| 10 | 
            +
              delete: Net::HTTP::Delete
         | 
| 11 | 
            +
            }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            class OASRequest::HTTP
         | 
| 14 | 
            +
              def self.config(opts = {})
         | 
| 15 | 
            +
                # set default options
         | 
| 16 | 
            +
                opts[:host] ||= "localhost"
         | 
| 17 | 
            +
                opts[:path] ||= "/"
         | 
| 18 | 
            +
                opts[:port] ||= 443
         | 
| 19 | 
            +
                opts[:protocol] ||= "https"
         | 
| 20 | 
            +
                opts[:headers] ||= {}
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                # set standard header values
         | 
| 23 | 
            +
                opts[:headers]["accept"] = "application/json"
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                if opts[:body]
         | 
| 26 | 
            +
                  # set content-type header when body is present
         | 
| 27 | 
            +
                  opts[:headers]["content-type"] = "application/json"
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  # ensure body is in JSON format
         | 
| 30 | 
            +
                  opts[:body] = opts[:body].to_json
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                opts
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              def self.get_request(method, opts = {})
         | 
| 37 | 
            +
                raise "Unknown method #{method}" unless REQUEST_CLASSES.has_key? method.downcase.to_sym
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                uri = URI "#{opts[:protocol]}://#{opts[:host]}:#{opts[:port]}#{opts[:path]}"
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                request_class = REQUEST_CLASSES[method.downcase.to_sym]
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                req = request_class.new uri, opts[:headers]
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                req.body = opts[:body] if opts[:body]
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                req
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def self.http(headers:, host:, method:, port:, body:, path:, protocol:)
         | 
| 51 | 
            +
                options = config(
         | 
| 52 | 
            +
                  host: host,
         | 
| 53 | 
            +
                  path: path,
         | 
| 54 | 
            +
                  port: port,
         | 
| 55 | 
            +
                  protocol: protocol,
         | 
| 56 | 
            +
                  headers: headers,
         | 
| 57 | 
            +
                  body: body
         | 
| 58 | 
            +
                )
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                req = get_request method, options
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                http = Net::HTTP.new options[:host], options[:port]
         | 
| 63 | 
            +
                http.use_ssl = options[:protocol] == "https"
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                response = http.request req
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                headers = response.to_hash
         | 
| 68 | 
            +
                body = response.body
         | 
| 69 | 
            +
                if headers.fetch("content-type", []).join.include? "application/json"
         | 
| 70 | 
            +
                  begin
         | 
| 71 | 
            +
                    body = JSON.parse response.body
         | 
| 72 | 
            +
                  rescue
         | 
| 73 | 
            +
                    body = response.body
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                {
         | 
| 78 | 
            +
                  headers: headers,
         | 
| 79 | 
            +
                  status_code: response.code.to_i,
         | 
| 80 | 
            +
                  status_message: response.message,
         | 
| 81 | 
            +
                  body: body
         | 
| 82 | 
            +
                }
         | 
| 83 | 
            +
              end
         | 
| 84 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            class OASRequest::PathTemplate
         | 
| 2 | 
            +
              def self.template(url, params)
         | 
| 3 | 
            +
                params_symbol = params.transform_keys(&:to_s)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                params_in_url = url.scan(/\{(?<param>[^\/]+)\}/)
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                params_in_url.each do |params_group|
         | 
| 8 | 
            +
                  params_group.each do |param|
         | 
| 9 | 
            +
                    next unless params_symbol.key? param
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    r = Regexp.new "{#{param}}"
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    url = url.gsub(r, params_symbol.fetch(param).to_s)
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                url.gsub('{', '%7B').gsub('}', '%7D')
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,86 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: oas_request
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - ''
         | 
| 8 | 
            +
            autorequire:
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2020-07-28 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: rack
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '2.2'
         | 
| 20 | 
            +
                - - ">="
         | 
| 21 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            +
                    version: 2.2.3
         | 
| 23 | 
            +
              type: :runtime
         | 
| 24 | 
            +
              prerelease: false
         | 
| 25 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 26 | 
            +
                requirements:
         | 
| 27 | 
            +
                - - "~>"
         | 
| 28 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 29 | 
            +
                    version: '2.2'
         | 
| 30 | 
            +
                - - ">="
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: 2.2.3
         | 
| 33 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 34 | 
            +
              name: webmock
         | 
| 35 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 36 | 
            +
                requirements:
         | 
| 37 | 
            +
                - - "~>"
         | 
| 38 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 39 | 
            +
                    version: '3.8'
         | 
| 40 | 
            +
                - - ">="
         | 
| 41 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 42 | 
            +
                    version: 3.8.3
         | 
| 43 | 
            +
              type: :development
         | 
| 44 | 
            +
              prerelease: false
         | 
| 45 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 46 | 
            +
                requirements:
         | 
| 47 | 
            +
                - - "~>"
         | 
| 48 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 49 | 
            +
                    version: '3.8'
         | 
| 50 | 
            +
                - - ">="
         | 
| 51 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 52 | 
            +
                    version: 3.8.3
         | 
| 53 | 
            +
            description: A simple OAS request generator
         | 
| 54 | 
            +
            email: ''
         | 
| 55 | 
            +
            executables: []
         | 
| 56 | 
            +
            extensions: []
         | 
| 57 | 
            +
            extra_rdoc_files: []
         | 
| 58 | 
            +
            files:
         | 
| 59 | 
            +
            - lib/oas_request.rb
         | 
| 60 | 
            +
            - lib/oas_request/http.rb
         | 
| 61 | 
            +
            - lib/oas_request/path_template.rb
         | 
| 62 | 
            +
            homepage: https://rubygems.org/gems/oas_request
         | 
| 63 | 
            +
            licenses:
         | 
| 64 | 
            +
            - MIT
         | 
| 65 | 
            +
            metadata: {}
         | 
| 66 | 
            +
            post_install_message:
         | 
| 67 | 
            +
            rdoc_options: []
         | 
| 68 | 
            +
            require_paths:
         | 
| 69 | 
            +
            - lib
         | 
| 70 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 71 | 
            +
              requirements:
         | 
| 72 | 
            +
              - - ">="
         | 
| 73 | 
            +
                - !ruby/object:Gem::Version
         | 
| 74 | 
            +
                  version: 2.5.0
         | 
| 75 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 76 | 
            +
              requirements:
         | 
| 77 | 
            +
              - - ">="
         | 
| 78 | 
            +
                - !ruby/object:Gem::Version
         | 
| 79 | 
            +
                  version: '0'
         | 
| 80 | 
            +
            requirements: []
         | 
| 81 | 
            +
            rubyforge_project:
         | 
| 82 | 
            +
            rubygems_version: 2.7.6.2
         | 
| 83 | 
            +
            signing_key:
         | 
| 84 | 
            +
            specification_version: 4
         | 
| 85 | 
            +
            summary: OAS request generator
         | 
| 86 | 
            +
            test_files: []
         |