Module: RaptorIO::Protocol::HTTP::Request::Manipulators

Extended by:
Enumerable
Defined in:
lib/raptor-io/protocol/http/request/manipulators.rb,
lib/raptor-io/protocol/http/request/manipulators/authenticator.rb,
lib/raptor-io/protocol/http/request/manipulators/redirect_follower.rb,
lib/raptor-io/protocol/http/request/manipulators/authenticators/ntlm.rb,
lib/raptor-io/protocol/http/request/manipulators/authenticators/basic.rb,
lib/raptor-io/protocol/http/request/manipulators/authenticators/digest.rb,
lib/raptor-io/protocol/http/request/manipulators/authenticators/negotiate.rb

Overview

Namespace holding all Request manipulators and providing some helper methods for management.

Author:

Defined Under Namespace

Modules: Authenticators Classes: Authenticator, RedirectFollower

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.libraryString

Returns Directory of the manipulators’ repository.

Returns:

  • (String)

    Directory of the manipulators’ repository.



18
19
20
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 18

def library
  @library
end

Class Method Details

.availableArray<Symbol>

Returns Names of all manipulators.

Returns:

  • (Array<Symbol>)

    Names of all manipulators.



81
82
83
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 81

def available
  paths.map { |path| path_to_name path }
end

.class_to_name(klass) ⇒ String?

Returns Manipulator shortname, ‘nil` if the manipulator isn’t loaded.

Parameters:

  • klass (Class)

    Manipulator class.

Returns:

  • (String, nil)

    Manipulator shortname, ‘nil` if the manipulator isn’t loaded.



194
195
196
197
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 194

def class_to_name( klass )
  @manipulators.select { |name, k| return name if k == klass }
  nil
end

.each(&block) ⇒ Enumerator, Manipulators

Returns ‘Enumerator` if no `block` is given, `self` otherwise.

Parameters:

  • block (Block)

    Block to be passed each manipulator name=>class.

Returns:

  • (Enumerator, Manipulators)

    ‘Enumerator` if no `block` is given, `self` otherwise.



146
147
148
149
150
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 146

def each( &block )
  return enum_for( __method__ ) if !block_given?
  @manipulators.each( &block )
  self
end

.exist?(name) ⇒ Bool

Returns ‘true` if the given manipulator exists, `false` otherwise.

Parameters:

  • name (String)

    Manipulator name.

Returns:

  • (Bool)

    ‘true` if the given manipulator exists, `false` otherwise.



181
182
183
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 181

def exist?( name )
  File.exist? name_to_path( name )
end

.load(manipulator) ⇒ Class

Returns Loaded manipulator.

Parameters:

  • manipulator (Symbol)

    Loads a manipulator by name.

Returns:

  • (Class)

    Loaded manipulator.



89
90
91
92
93
94
95
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 89

def load( manipulator )
  manipulator = normalize_name( manipulator )
  return @manipulators[manipulator] if @manipulators.include? manipulator

  Kernel.load name_to_path( manipulator )
  @manipulators[manipulator]
end

.load_allHash

Loads all manipulators.

Returns:

  • (Hash)

    All manipulators.



100
101
102
103
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 100

def load_all
  paths.each { |path| load path_to_name( path ) }
  loaded
end

.loadedHash

Returns All manipulators as a frozen hash.

Returns:

  • (Hash)

    All manipulators as a frozen hash.



153
154
155
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 153

def loaded
  @manipulators.dup.freeze
end

.name_to_path(name) ⇒ String

Returns Manipulator FS path.

Parameters:

  • name (String)

    Manipulator shortname.

Returns:

  • (String)

    Manipulator FS path.



201
202
203
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 201

def name_to_path( name )
  File.expand_path "#{library}/#{name}.rb"
end

.normalize_name(name) ⇒ String

Returns Manipulator name.

Parameters:

  • name (String, Symbol)

    Manipulator name.

Returns:

  • (String)

    Manipulator name.



207
208
209
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 207

def normalize_name( name )
  name.to_s
end

.path_to_name(path) ⇒ String

Returns Manipulator shortname.

Parameters:

  • path (String)

    FS path to a manipulator.

Returns:

  • (String)

    Manipulator shortname.



187
188
189
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 187

def path_to_name( path )
  normalize_name path.gsub( library, '' ).gsub( /(.+)\.rb$/, '\1' )
end

.pathsArray<String>

Returns Paths of all manipulators.

Returns:

  • (Array<String>)

    Paths of all manipulators.



76
77
78
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 76

def paths
  Dir.glob( "#{library}**/*.rb" )
end

.process(manipulator, client, request, options = {}) ⇒ Object

Parameters:

  • manipulator (String)

    Manipulator to run – will be loaded if needed.

  • client (HTTP::Client)

    Applicable client.

  • request (HTTP::Request)

    Request to process.

  • options (Hash) (defaults to: {})

    Manipulator options.



28
29
30
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 28

def process( manipulator, client, request, options = {} )
  load( manipulator ).new( client, request, options ).run
end

.register(name, klass) ⇒ Manipulator

Registers a manipulator.

Parameters:

  • name (Symbol)
  • klass (Base)

Returns:



165
166
167
168
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 165

def register( name, klass )
  @manipulators[normalize_name( name )] = klass
  self
end

.resetObject

Resets the manipulators by unloading all and settings the #library to its default setting.



172
173
174
175
176
177
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 172

def reset
  unload_all if @manipulators

  @library      = File.expand_path( File.dirname( __FILE__ ) + '/manipulators' ) + '/'
  @manipulators = {}
end

.unload(manipulator) ⇒ Bool

Returns ‘true` if the manipulator was unloaded successfully, `false` if no matching one was found.

Parameters:

  • manipulator (Symbol)

    Unloads a manipulator by name.

Returns:

  • (Bool)

    ‘true` if the manipulator was unloaded successfully, `false` if no matching one was found.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 111

def unload( manipulator )
  klass = @manipulators.delete( normalize_name( manipulator ) )
  return false if !klass

  container = self
  klass.to_s.gsub( "#{self}::", '' ).split( '::' )[0...-1].each do |c|
    container = container.const_get( c.to_sym )
  end

  container.instance_eval do
    remove_const klass.to_s.split( ':' ).last.to_sym
  end

  # Remove the container namespaces themselves if they're now empty.
  container = self
  klass.to_s.gsub( "#{self}::", '' ).split( '::' )[0...-1].each do |c|
    container = container.const_get( c.to_sym )
    if container != self && container.constants.empty?
      remove_const container.to_s.split( ':' ).last.to_sym
    end
  end

  true
end

.unload_allObject

Unloads all manipulators.



137
138
139
140
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 137

def unload_all
  @manipulators.keys.each { |manipulator| unload manipulator }
  nil
end

.validate_batch_options(manipulators, client) ⇒ Hash{String=>Hash}

Performs batch validation of manipulator options.

Parameters:

  • manipulators (Hash{String=>Hash})

    Manipulators for keys and their options as values.

  • client (HTTP::Client)

    Applicable client.

Returns:

  • (Hash{String=>Hash})

    Manipulators for keys and error hashes as values.



42
43
44
45
46
47
48
49
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 42

def validate_batch_options( manipulators, client )
  errors = {}
  manipulators.each do |manipulator, options|
    errors[manipulator] =
        validate_options( manipulator, options, client )
  end
  errors.reject { |_, errs| errs.empty? }
end

.validate_batch_options!(manipulators, client) ⇒ Object

Same as validate_batch_options but raises exception on errors.



52
53
54
55
56
57
58
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 52

def validate_batch_options!( manipulators, client )
  errors = validate_batch_options( manipulators, client )
  if errors.any?
    fail Request::Manipulator::Error::InvalidOptions, errors.to_s
  end
  nil
end

.validate_options(manipulator, options, client) ⇒ Hash{Symbol=>Array<String>}

Returns Option names keys for and error messages for values.

Parameters:

Returns:

  • (Hash{Symbol=>Array<String>})

    Option names keys for and error messages for values.



66
67
68
# File 'lib/raptor-io/protocol/http/request/manipulators.rb', line 66

def validate_options( manipulator, options, client )
  load( manipulator ).validate_options!( options, client )
end