Class: Spidr::AuthStore

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeAuthStore

Creates a new auth store.

Since:

  • 0.2.2


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.

Parameters:

  • url (URI)

    A fully qualified url including optional path.

Returns:

Since:

  • 0.2.2


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)

  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.expand_path(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.

Parameters:

  • url (URI)

    A URL pattern to associate with a set of auth credentials.

  • auth (AuthCredential)

    The auth credential for this URL pattern.

Returns:

Since:

  • 0.2.2


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)

  # 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

#add(url, username, password) ⇒ AuthCredential

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

Parameters:

  • url (URI)

    The base URL that requires authorization.

  • username (String)

    The username required to access the URL.

  • password (String)

    The password required to access the URL.

Returns:

Since:

  • 0.2.2


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.

Returns:

Since:

  • 0.2.2


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.

Parameters:

  • url (URI)

    The url.

Returns:

  • (String, nil)

    The base64 encoded authorizatio string or nil.

Since:

  • 0.2.2


119
120
121
122
123
# File 'lib/spidr/auth_store.rb', line 119

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

#inspectString

Inspects the auth store.

Returns:

  • (String)

    The inspected version of the auth store.


160
161
162
# File 'lib/spidr/auth_store.rb', line 160

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

#sizeInteger

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

Returns:

  • (Integer)

    The size of the auth store.

Since:

  • 0.2.2


146
147
148
149
150
151
152
# File 'lib/spidr/auth_store.rb', line 146

def size
  total = 0

  @credentials.each_value { |paths| total += paths.length }

  return total
end