Sha256: 6e25a62aa9e26d832fe0f5860a387a67aecfd27e45f12a2a80a88c9b701ebeb9

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8

module FIR
  module Http

    DEFAULT_TIMEOUT = 300

    def get url, params = {}
      begin
        res = ::RestClient::Request.execute(
          method:  :get,
          url:     url,
          timeout: DEFAULT_TIMEOUT,
          headers: default_headers.merge(params: params)
        )
      rescue => e
        logger.error e.message.to_s + " - " + e.response.to_s
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    %w(post patch put).each do |method|
      define_method method do |url, query|
        begin
          res = ::RestClient::Request.execute(
            method:  method.to_sym,
            url:     url,
            payload: query,
            timeout: DEFAULT_TIMEOUT,
            headers: default_headers
          )
        rescue => e
          logger.error e.message.to_s + " - " + e.response.to_s
          exit 1
        end

        JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
      end
    end

    private

      def default_headers
        { content_type: :json, source: 'fir-cli', version: FIR::VERSION }
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fir-cli-1.2.2 lib/fir/util/http.rb
fir-cli-1.2.1 lib/fir/util/http.rb
fir-cli-1.2.0 lib/fir/util/http.rb
fir-cli-1.1.9 lib/fir/util/http.rb
fir-cli-1.1.8 lib/fir/util/http.rb
fir-cli-1.1.7 lib/fir/util/http.rb