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



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.

Parameters:

  • url (URI)

    A fully qualified url including optional path.

Returns:

Since:

  • 0.2.2



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



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



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.

Returns:

Since:

  • 0.2.2



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.

Parameters:

  • url (URI)

    The URL.

Returns:

  • (String, nil)

    The base64 encoded authorizatio string or nil.

Since:

  • 0.2.2



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

#inspectString

Inspects the auth store.

Returns:

  • (String)

    The inspected version of the auth store.



162
163
164
# File 'lib/spidr/auth_store.rb', line 162

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



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