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.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.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_base (URI)

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

  • The (AuthCredential)

    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.to_s) 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

#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])
    return Base64.encode64("#{auth.username}:#{auth.password}")
  end
end

#inspectString

Inspects the auth store.

Returns:

  • (String)

    The inspected version of the auth store.



156
157
158
# File 'lib/spidr/auth_store.rb', line 156

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
# File 'lib/spidr/auth_store.rb', line 146

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