Sha256: 3a7f231cda1d5afcc94af28d6428372e4466ee248c68684372e64dee3438fae8

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

require 'net/http'
require 'json'

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


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

9 entries across 9 versions & 1 rubygems

Version Path
lusnoc-1.0.1.70330 lib/lusnoc.rb
lusnoc-1.0.0.70325 lib/lusnoc.rb
lusnoc-1.0.0.44554 lib/lusnoc.rb
lusnoc-0.9.0.44532 lib/lusnoc.rb
lusnoc-0.1.2.16875 lib/lusnoc.rb
lusnoc-0.1.2.16587 lib/lusnoc.rb
lusnoc-0.1.2.16562 lib/lusnoc.rb
lusnoc-0.1.2.16550 lib/lusnoc.rb
lusnoc-0.1.2.16548 lib/lusnoc.rb