Sha256: 908ecb3d7fa93c052ce4d5959deada087a03ad1d78c7d86ea53383b8e18e8684

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'net/http'
require 'json'

require 'lusnoc/configuration'
require 'lusnoc/session'
require 'lusnoc/mutex'


module Lusnoc

  class << self

    attr_accessor :configuration

  end

  self.configuration ||= Lusnoc::Configuration.new

  class << self

    def configure
      self.configuration ||= Lusnoc::Configuration.new
      yield(configuration)
    end

    def http_get(url, timeout: 1)
      uri = URI(url)

      with_http(uri, timeout: timeout) do |http|
        req = Net::HTTP::Get.new(uri)

        # configure http and request before send
        yield(http, req) if block_given?
        http.request(req)
      end
    end

    def http_put(url, value = nil, timeout: 1)
      uri = URI(url)
      data = value.is_a?(String) ? value : JSON.generate(value) unless value.nil?

      with_http(uri, timeout: timeout) do |http|
        req = Net::HTTP::Put.new(uri).tap do |r|
          r.body = data
          r['Content-Type'] = 'application/json'
        end

        # configure http and request before send
        yield(http, req) if block_given?
        http.request(req)
      end
    end

    private

      def with_http(uri, timeout:)
        Net::HTTP.start(uri.host, uri.port,
                        use_ssl:          uri.scheme == 'https',
                        read_timeout:     timeout,
                        open_timeout:     1,
                        continue_timeout: 1,
                        write_timeout:    1,
                        max_retries:      0) do |http|
          yield(http)
        end
      end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lusnoc-0.1.0 lib/lusnoc.rb
lusnoc-0.0.2 lib/lusnoc.rb
lusnoc-0.0.1 lib/lusnoc.rb