Class: Spidr::AuthStore

Inherits:
Object
  • Object
show all
Defined in:
lib/spidr/auth_store.rb

Instance Method Summary

Constructor Details

- (AuthStore) initialize

Creates a new auth store.

Since:

  • 0.2.2


15
16
17
# File 'lib/spidr/auth_store.rb', line 15

def initialize
  @credentials = {}
end

Instance Method Details

- (AuthCredential?) [](url)

Given a URL, return the most specific matching auth credential.

Parameters:

  • (URI) url — A fully qualified url includig optional path.

Returns:

Since:

  • 0.2.2


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spidr/auth_store.rb', line 31

def [](url)
  # normalize the url
  url = URI(url) 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.expand_path(url.path).split('/')

  ordered_paths.each do |path|
    return paths[path] if path_dirs[0,path.length] == path
  end

  return nil
end

- (AuthCredential) []=(url, auth)

Add an auth credential to the store for supplied base URL.

Parameters:

  • (URI) url_base — A URL pattern to associate with a set of auth credentials.
  • (AuthCredential) The — auth credential for this URL pattern.

Returns:

Since:

  • 0.2.2


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spidr/auth_store.rb', line 67

def []=(url, auth)
  # normalize the url
  url = URI(url) unless url.kind_of?(URI)

  # normalize the URL path
  path = URI.expand_path(url.path)

  key = [url.scheme, url.host, url.port]

  @credentials[key] ||= {}
  @credentials[key][path.split('/')] = auth
  return auth
end

- (AuthCredential) add(url, username, password)

Convenience method to add username and password credentials for a named URL.

Parameters:

  • (URI) url — The base URL that requires authorization.
  • (String) username — The username required to access the URL.
  • (String) password — The password required to access the URL.

Returns:

Since:

  • 0.2.2


99
100
101
# File 'lib/spidr/auth_store.rb', line 99

def add(url, username, password)
  self[url] = AuthCredential.new(username, password)
end

- (AuthStore) clear!

Clear the contents of the auth store.

Returns:

Since:

  • 0.2.2


129
130
131
132
# File 'lib/spidr/auth_store.rb', line 129

def clear!
  @credentials.clear
  return self
end

- (String?) for_url(url)

Returns the base64 encoded authorization string for the URL or nil if no authorization exists.

Parameters:

  • (URI) url — The url.

Returns:

  • (String, nil) — The base64 encoded authorizatio string or nil.

Since:

  • 0.2.2


115
116
117
118
119
# File 'lib/spidr/auth_store.rb', line 115

def for_url(url)
  if (auth = self[url])
    return Base64.encode64("#{auth.username}:#{auth.password}")
  end
end

- (String) inspect

Inspects the auth store.

Returns:

  • (String) — The inspected version of the auth store.


152
153
154
# File 'lib/spidr/auth_store.rb', line 152

def inspect
  "#<#{self.class}: #{@credentials.inspect}>"
end

- (Integer) size

Size of the current auth store (number of URL paths stored).

Returns:

  • (Integer) — The size of the auth store.

Since:

  • 0.2.2


142
143
144
# File 'lib/spidr/auth_store.rb', line 142

def size
  @credentials.inject(0) { |res, arr| res + arr[1].length }
end