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.
21 22 23 |
# File 'lib/spidr/auth_store.rb', line 21 def initialize @credentials = {} end |
Instance Method Details
#[](url) ⇒ AuthCredential?
Given a URL, return the most specific matching auth credential.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/spidr/auth_store.rb', line 37 def [](url) # normalize the url url = URI(url) key = [url.scheme, url.host, url.port] paths = @credentials[key] return nil unless paths # longest path first ordered_paths = paths.keys.sort_by { |path_key| -path_key.length } # 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/spidr/auth_store.rb', line 73 def []=(url,auth) # normalize the url url = URI(url) # 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.
105 106 107 |
# File 'lib/spidr/auth_store.rb', line 105 def add(url,username,password) self[url] = AuthCredential.new(username,password) end |
#clear! ⇒ AuthStore
Clear the contents of the auth store.
135 136 137 138 |
# File 'lib/spidr/auth_store.rb', line 135 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.
121 122 123 124 125 |
# File 'lib/spidr/auth_store.rb', line 121 def for_url(url) if (auth = self[url]) Base64.encode64("#{auth.username}:#{auth.password}") end end |
#inspect ⇒ String
Inspects the auth store.
162 163 164 |
# File 'lib/spidr/auth_store.rb', line 162 def inspect "#<#{self.class}: #{@credentials.inspect}>" end |
#size ⇒ Integer
Size of the current auth store (number of URL paths stored).
148 149 150 151 152 153 154 |
# File 'lib/spidr/auth_store.rb', line 148 def size total = 0 @credentials.each_value { |paths| total += paths.length } return total end |