Sha256: 4c41b56fddaa168a6312863b32cfb41654d1141b5224c637d3764898d7e97a6e

Contents?: true

Size: 1.01 KB

Versions: 22

Compression:

Stored size: 1.01 KB

Contents

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

module Kurchatov
  module Mixin
    module Http

      USER_AGENT = "Kurchatov (Riemann client)".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

22 entries across 22 versions & 1 rubygems

Version Path
kurchatov-0.3.8 lib/kurchatov/mixin/http.rb
kurchatov-0.3.7 lib/kurchatov/mixin/http.rb
kurchatov-0.3.6 lib/kurchatov/mixin/http.rb
kurchatov-0.3.5 lib/kurchatov/mixin/http.rb
kurchatov-0.3.4 lib/kurchatov/mixin/http.rb
kurchatov-0.3.3 lib/kurchatov/mixin/http.rb
kurchatov-0.3.2 lib/kurchatov/mixin/http.rb
kurchatov-0.3.1 lib/kurchatov/mixin/http.rb
kurchatov-0.3.0 lib/kurchatov/mixin/http.rb
kurchatov-0.2.9 lib/kurchatov/mixin/http.rb
kurchatov-0.2.8 lib/kurchatov/mixin/http.rb
kurchatov-0.2.7 lib/kurchatov/mixin/http.rb
kurchatov-0.2.6 lib/kurchatov/mixin/http.rb
kurchatov-0.2.5 lib/kurchatov/mixin/http.rb
kurchatov-0.2.4 lib/kurchatov/mixin/http.rb
kurchatov-0.2.3 lib/kurchatov/mixin/http.rb
kurchatov-0.2.2 lib/kurchatov/mixin/http.rb
kurchatov-0.2.1 lib/kurchatov/mixin/http.rb
kurchatov-0.2.0 lib/kurchatov/mixin/http.rb
kurchatov-0.1.8 lib/kurchatov/mixin/http.rb