Sha256: 4719036465f588bbdeef40c2b18835ab31410198e1d27b84d0fcd245748e681f

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true
module Wazuh
  module Sawyer
    module Request
      def get(path, options = {})
        request(:get, path, options)
      end

      def post(path, options = {})
        request(:post, path, options)
      end

      def put(path, options = {})
        request(:put, path, options)
      end

      def delete(path, options = {})
        request(:delete, path, options)
      end

      def offset_request(method, path, options = {})
        items = []
        data = send(method, path, options)
        0.step(data.totalItems, 500) { |offset|
          options[:offset] = offset
          d = send(method, path, options)
          items.concat(d.items)
        }

        items
      end

      private

      def request(method, path, options, query_options={})
        response = case method
        when :get, :delete
          connection.call(method, URI::Parser.new.escape(path), nil, {query: options})
        when :post, :put
          data = options unless options.empty?
          connection.call(method, URI::Parser.new.escape(path), data, {query: query_options})
        end

        return response.data.data if response.status == 200

        raise Wazuh::Api::Errors::WazuhError.new(response.body, response)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wazuh-ruby-client-0.3.1 lib/wazuh/sawyer/request.rb
wazuh-ruby-client-0.3.0 lib/wazuh/sawyer/request.rb