Class: Rack::TrafficLogger::OptionInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/traffic_logger/option_interpreter.rb,
lib/rack/traffic_logger/option_interpreter/shorthand.rb

Defined Under Namespace

Classes: Exclude, Include, OnlyCode, OnlyVerb, OptionProxy, Rule, Shorthand

Constant Summary collapse

VERBS =
%i[get post put patch delete head options trace]
TYPES =
%i[request_headers response_headers request_bodies response_bodies]

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ OptionInterpreter

Returns a new instance of OptionInterpreter.



10
11
12
13
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 10

def initialize(*options)
  @tests = {}
  add_rules options
end

Instance Method Details

#add_rule_hash(hash, **filter) ⇒ Object



89
90
91
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 89

def add_rule_hash(hash, **filter)
  hash.each { |k, v| add_rule_pair k, v, **filter }
end

#add_rule_pair(name, value, **filter) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 93

def add_rule_pair(name, value, **filter)
  case name
    when *VERBS then add_rules value, **filter.merge(verb: name)
    when Integer, Range then add_rules value, **filter.merge(code: name)
    when Array then name.each { |n| add_rule_pair n, value, **filter }
    else raise "Invalid token of type #{name.class.name} : #{name}"
  end
end

#add_rules(input, **filter) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 55

def add_rules(input, **filter)
  input = [input] unless Array === input
  input = Shorthand.new(input.shift).transform + input while String === input.first
  verb = filter[:verb]
  code = filter[:code]
  input.each do |token|
    case token
      when *VERBS
        raise "Verb on verb (#{token} on #{verb})" if verb
        rules << OnlyVerb.new(token, filter)
      when Integer, Range
        raise "Code on code (#{token} on #{code})" if code
        rules << OnlyCode.new(token, filter)
      when *TYPES
        rules << Include.new(token, filter)
      when FalseClass
        rules << Exclude.new(nil, filter)
      when :headers then add_rules [:request_headers, :response_headers], **filter
      when :bodies then add_rules [:request_bodies, :response_bodies], **filter
      when :all then add_rules [:headers, :bodies], **filter
      when Hash
        if token.keys == [:only]
          inner_hash = token.values.first
          raise 'You can only use :only => {} with a Hash' unless Hash === inner_hash
          add_rules inner_hash.keys, **filter
          add_rule_hash inner_hash, **filter
        else
          add_rule_hash token, **filter
        end
      else raise "Invalid token of type #{token.class.name} : #{token}"
    end
  end
end

#for(verb, code) ⇒ Object



51
52
53
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 51

def for(verb, code)
  OptionProxy.new self, verb, code
end

#test(*args) ⇒ TrueClass|FalseClass Also known as: basic?

Test whether a given verb, status code, and log type should be logged

Parameters:

  • verb (Symbol)

    One of the Rack::TrafficLogger::OptionInterpreter.selfself::VERBS symbols

  • code (Integer)

    The HTTP status code

  • type (Symbol|NilClass)

    One of the Rack::TrafficLogger::OptionInterpreter.selfself::TYPES symbols, or ‘nil` for basic request/response details

Returns:

  • (TrueClass|FalseClass)

    Whether the type should be logged



107
108
109
110
111
112
113
# File 'lib/rack/traffic_logger/option_interpreter.rb', line 107

def test(*args)
  if @tests.key? args
    @tests[args]
  else
    @tests[args] = _test *args
  end
end