Class: Spidr::AuthStore
- Inherits:
-
Object
- Object
- Spidr::AuthStore
- Defined in:
- lib/spidr/auth_store.rb
Overview
Stores AuthCredential objects organized by a website's scheme, host-name and sub-directory.
Instance Method Summary collapse
-
#[](url) ⇒ AuthCredential?
Given a URL, return the most specific matching auth credential.
-
#[]=(url, auth) ⇒ AuthCredential
Add an auth credential to the store for supplied base URL.
-
#add(url, username, password) ⇒ AuthCredential
Convenience method to add username and password credentials for a named URL.
-
#clear! ⇒ AuthStore
Clear the contents of the auth store.
-
#for_url(url) ⇒ String?
Returns the base64 encoded authorization string for the URL or
nil
if no authorization exists. -
#initialize ⇒ AuthStore
constructor
Creates a new auth store.
-
#inspect ⇒ String
Inspects the auth store.
-
#size ⇒ Integer
Size of the current auth store (number of URL paths stored).
Constructor Details
#initialize ⇒ AuthStore
Creates a new auth store.
19 20 21 |
# File 'lib/spidr/auth_store.rb', line 19 def initialize @credentials = {} end |
Instance Method Details
#[](url) ⇒ AuthCredential?
Given a URL, return the most specific matching auth credential.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/spidr/auth_store.rb', line 35 def [](url) # normalize the url url = URI(url.to_s) unless url.kind_of?(URI) key = [url.scheme, url.host, url.port] paths = @credentials[key] return nil unless paths # longest path first ordered_paths = paths.keys.sort_by { |key| key.length }.reverse # directories of the path path_dirs = URI.(url.path).split('/') ordered_paths.each do |path| return paths[path] if path_dirs[0,path.length] == path end return nil end |
#[]=(url, auth) ⇒ AuthCredential
Add an auth credential to the store for supplied base URL.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/spidr/auth_store.rb', line 71 def []=(url,auth) # normalize the url url = URI(url.to_s) unless url.kind_of?(URI) # normalize the URL path path = URI.(url.path) key = [url.scheme, url.host, url.port] @credentials[key] ||= {} @credentials[key][path.split('/')] = auth return auth end |
#add(url, username, password) ⇒ AuthCredential
Convenience method to add username and password credentials for a named URL.
103 104 105 |
# File 'lib/spidr/auth_store.rb', line 103 def add(url,username,password) self[url] = AuthCredential.new(username,password) end |
#clear! ⇒ AuthStore
Clear the contents of the auth store.
133 134 135 136 |
# File 'lib/spidr/auth_store.rb', line 133 def clear! @credentials.clear return self end |
#for_url(url) ⇒ String?
Returns the base64 encoded authorization string for the URL
or nil
if no authorization exists.
119 120 121 122 123 |
# File 'lib/spidr/auth_store.rb', line 119 def for_url(url) if (auth = self[url]) return Base64.encode64("#{auth.username}:#{auth.password}") end end |
#inspect ⇒ String
Inspects the auth store.
156 157 158 |
# File 'lib/spidr/auth_store.rb', line 156 def inspect "#<#{self.class}: #{@credentials.inspect}>" end |
#size ⇒ Integer
Size of the current auth store (number of URL paths stored).
146 147 148 |
# File 'lib/spidr/auth_store.rb', line 146 def size @credentials.inject(0) { |res, arr| res + arr[1].length } end |