Class: Alexandria::WWWAgent
- Inherits:
-
Object
- Object
- Alexandria::WWWAgent
- Defined in:
- lib/alexandria/net.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(url) ⇒ Object
-
#initialize ⇒ WWWAgent
constructor
A new instance of WWWAgent.
- #language=(lang) ⇒ Object
- #user_agent=(agent_string) ⇒ Object
Constructor Details
#initialize ⇒ WWWAgent
Returns a new instance of WWWAgent.
25 26 27 28 |
# File 'lib/alexandria/net.rb', line 25 def initialize user_agent = "Ruby #{RUBY_VERSION} #{Alexandria::TITLE}/#{Alexandria::VERSION}" @extra_request_headers = { "User-Agent" => user_agent } end |
Class Method Details
.transport ⇒ Object
30 31 32 33 |
# File 'lib/alexandria/net.rb', line 30 def self.transport config = Alexandria::Preferences.instance.http_proxy_config config ? Net::HTTP.Proxy(*config) : Net::HTTP end |
Instance Method Details
#get(url) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/alexandria/net.rb', line 35 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 |
#language=(lang) ⇒ Object
47 48 49 |
# File 'lib/alexandria/net.rb', line 47 def language=(lang) @extra_request_headers["Accept-Language"] = lang.to_s end |
#user_agent=(agent_string) ⇒ Object
51 52 53 |
# File 'lib/alexandria/net.rb', line 51 def user_agent=(agent_string) @extra_request_headers["User-Agent"] = agent_string end |