Class: OPDS::Support::Browser

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/opds/support/browser.rb

Overview

Browser class, it will be used to access the Internet. Currently based on open-uri only

Instance Method Summary collapse

Methods included from Logging

#log, log

Instance Method Details

#bodyString

Returns Last page body.

Returns:

  • (String)

    Last page body



46
47
48
# File 'lib/opds/support/browser.rb', line 46

def body
	@last_response.body if @last_response
end

#current_locationString

Returns current uri.

Returns:

  • (String)

    current uri



51
52
53
# File 'lib/opds/support/browser.rb', line 51

def current_location
	@current_location
end

#discover(url) ⇒ OPDS::Support::LinkSet, false

Try to discover catalog links at the given url

Parameters:

  • url (String)

    to search

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opds/support/browser.rb', line 58

def discover(url)
	go_to(url)
	if ok?
		doc=Nokogiri::HTML(body)
		tab=OPDS::Support::LinkSet.new(self)
		extract_links(tab,doc,'//*[@type="application/atom+xml;type=entry;profile=opds-catalog"]')		
		extract_links(tab,doc,'//*[@type="application/atom+xml;profile=opds-catalog"]')

		return false if tab.size == 0
		tab
	else
		return false
	end

end

#go_to(uri) ⇒ Object

Navigate to the provided uri

Parameters:

  • uri (String)

    uri to go to



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/opds/support/browser.rb', line 11

def go_to(uri)
	log("Accessing #{uri}")
	url=URI.parse(uri)
	@last_response=nil
	Net::HTTP.start(url.host,url.port) {|http|
		path="/"
		path=url.path unless url.path==''
		req = Net::HTTP::Get.new(path)
		#					req.basic_auth user,pass  unless user.nil?
		@last_response = http.request(req)
	}
	@current_location=url.to_s
	if status/10==30 && headers['location']
		log("Following redirection (code: #{status}) to #{headers['location']}")
		go_to(headers['location'].first)
	end
end

#headersHash

Returns Last page HTTP headers.

Returns:

  • (Hash)

    Last page HTTP headers



41
42
43
# File 'lib/opds/support/browser.rb', line 41

def headers
	@last_response.to_hash if @last_response
end

#ok?boolean

Last page load was ok ?

Returns:

  • (boolean)


31
32
33
# File 'lib/opds/support/browser.rb', line 31

def ok?
	status==200
end

#statusinteger

Returns Last page load return code.

Returns:

  • (integer)

    Last page load return code



36
37
38
# File 'lib/opds/support/browser.rb', line 36

def status
	@last_response.code.to_i if @last_response
end