Sha256: d478a2b60ed1bd17e5b32d6782d76a7142e958af626d2992c03ca0fdba21d484

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'net/http'
require 'open-uri'
require 'yajl/json_gem'

module Kurchatov
  module Mixin
    module Http

      USER_AGENT = "Kurchatov (Riemann client v#{Kurchatov::Version})".freeze

      # /path/to/file, https://ya.ru, http://a:a@yandex.ru
      def rest_get(url)
        uri = URI(url)
        if uri.userinfo
          open("#{uri.scheme}://#{uri.hostname}:#{uri.port}#{uri.request_uri}",
               :http_basic_authentication => [uri.user, uri.password],
               'User-Agent' => USER_AGENT).read
        else
          open(url, 'User-Agent' => USER_AGENT).read
        end
      end

      # return: body, http_code
      def http_get(url)
        uri = URI(url)
        req = Net::HTTP::Get.new(uri)
        req['User-Agent'] = USER_AGENT
        res = nil
        begin
          Net::HTTP.start(uri.hostname, uri.port) {|http| res = http.request(req)}
        rescue SocketError, Errno::ECONNREFUSED
          return nil, 0
        end
        return res.body, res.code.to_i
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kurchatov-0.1.4 lib/kurchatov/mixin/http.rb
kurchatov-0.1.3 lib/kurchatov/mixin/http.rb