Sha256: b7c78c2588e07f82d7acf6704feca8027244cac3feeb758d6fef11ad79d87c4f

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'httparty'
require 'json'
require 'zobbix/api_response'

class Zobbix
  class ApiRequest
    include HTTParty

    headers 'Content-Type' => 'application/json-rpc'

    def self.default_params
      { 'jsonrpc' => '2.0' }
    end

    def self.path
      '/api_jsonrpc.php'
    end

    def self.perform(*args)
      new(*args).perform
    end

    # @param [String] uri Zabbix Server uri
    # @param [String] method API method
    # @param [Hash] params API method params
    def initialize(uri, method, params)
      @uri    = uri.sub(/\/$/, '')
      @method = method
      @auth   = params.delete(:auth)
      @params = params
    end

    # Execute http request
    #
    # @return [Zobbix::ApiResponse]
    def perform
      ApiResponse.new { self.class.post("#{@uri}#{self.class.path}", body: payload) }
    end

    private

    def id
      rand(9999) # Id should be totally random
    end

    def payload
      JSON.generate(self.class.default_params.merge(method: @method,
                                                    params: @params,
                                                    auth: @auth,
                                                    id: id))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
zobbix-0.0.4 lib/zobbix/api_request.rb
zobbix-0.0.3 lib/zobbix/api_request.rb
zobbix-0.0.2 lib/zobbix/api_request.rb