Sha256: 9ad7ce1d2560a751848a2b320a10ab5e19be2d93c52d10c7bc7c8f06d3d54d6b

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

# Copyright (C) 2009 Cathal Mc Ginley
# Copyright (C) 2014 Matijs van Zuijlen
#
# This file is part of Alexandria.
#
# Alexandria is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Alexandria is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with Alexandria; see the file COPYING.  If not,
# write to the Free Software Foundation, Inc., 51 Franklin Street,
# Fifth Floor, Boston, MA 02110-1301 USA.

module Alexandria
  class WWWAgent
    def initialize
      user_agent = "Ruby #{RUBY_VERSION} #{Alexandria::TITLE}/#{Alexandria::VERSION}"
      @extra_request_headers = { "User-Agent" => user_agent }
    end

    def self.transport
      config = Alexandria::Preferences.instance.http_proxy_config
      config ? Net::HTTP.Proxy(*config) : Net::HTTP
    end

    def get(url)
      uri = URI.parse(url)
      req = Net::HTTP::Get.new(uri.request_uri)
      @extra_request_headers.each_pair do |header_name, value|
        req.add_field(header_name, value)
      end
      res = WWWAgent.transport.start(uri.host, uri.port) do |http|
        http.request(req)
      end
      res
    end

    def language=(lang)
      @extra_request_headers["Accept-Language"] = lang.to_s
    end

    def user_agent=(agent_string)
      @extra_request_headers["User-Agent"] = agent_string
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alexandria-book-collection-manager-0.7.5 lib/alexandria/net.rb
alexandria-book-collection-manager-0.7.4 lib/alexandria/net.rb