Sha256: 42155b7f3952d4582e23005e228b0273f88d46127a6ae43f5cbe52b44c67c67b

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 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
      WWWAgent.transport.start(uri.host, uri.port) do |http|
        http.request(req)
      end
    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

5 entries across 5 versions & 1 rubygems

Version Path
alexandria-book-collection-manager-0.7.10 lib/alexandria/net.rb
alexandria-book-collection-manager-0.7.9 lib/alexandria/net.rb
alexandria-book-collection-manager-0.7.8 lib/alexandria/net.rb
alexandria-book-collection-manager-0.7.7 lib/alexandria/net.rb
alexandria-book-collection-manager-0.7.6 lib/alexandria/net.rb