Class: Fitting::Doc::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/doc/action.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, prefix, method, path, responses) ⇒ Action

Returns a new instance of Action.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fitting/doc/action.rb', line 11

def initialize(host, prefix, method, path, responses)
  @host = host
  @prefix = prefix
  @method = method
  @path = path
  @path.slice!(prefix)
  @responses = []
  responses.group_by { |response| response['status'] }.each do |code, value|
    @responses.push(Code.new(code, value))
  end
  @key = "#{method} #{host}#{prefix}#{path}"

  @host_cover = 0
  @prefix_cover = 0
  @path_cover = 0
  @method_cover = 0
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/fitting/doc/action.rb', line 7

def host
  @host
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/fitting/doc/action.rb', line 7

def method
  @method
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/fitting/doc/action.rb', line 7

def path
  @path
end

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'lib/fitting/doc/action.rb', line 7

def prefix
  @prefix
end

#responsesObject

Returns the value of attribute responses.



7
8
9
# File 'lib/fitting/doc/action.rb', line 7

def responses
  @responses
end

Instance Method Details

#cover!(log) ⇒ Object



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
88
89
90
91
92
93
94
# File 'lib/fitting/doc/action.rb', line 63

def cover!(log)
  unless log.host == host
    return
  end
  @host_cover += 1

  unless prefix.size == 0 || log.path[0..prefix.size - 1] == prefix
    return
  end
  @prefix_cover += 1

  unless path_match(log.path)
    return
  end
  @path_cover += 1

  unless log.method == method
    return
  end
  @method_cover += 1

  @responses.each { |response| response.cover!(log) }

  true
rescue Fitting::Doc::Code::NotFound => e
  raise NotFound.new "\n\ntype: #{@type}\n"\
    "host: #{@host}\n" \
    "prefix: #{@prefix}\n" \
    "method: #{@method}\n" \
    "path: #{@path}\n" \
    "#{e.message}"
end

#debug(debug) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fitting/doc/action.rb', line 96

def debug(debug)
  unless debug.host == host
    return nil
  end

  unless prefix.size == 0 || debug.path[0..prefix.size - 1] == prefix
    return nil
  end

  unless path_match(debug.path)
    return nil
  end

  unless debug.method == method
    return nil
  end

  @responses.each do |response|
    res = response.debug(debug)
    return res if res
  end
  nil
end

#nocover!Object



120
121
122
123
124
125
# File 'lib/fitting/doc/action.rb', line 120

def nocover!
  @host_cover = nil
  @prefix_cover = nil
  @path_cover = nil
  @method_cover = nil
end

#path_match(find_path) ⇒ Object



127
128
129
# File 'lib/fitting/doc/action.rb', line 127

def path_match(find_path)
  regexp =~ find_path
end

#regexpObject



131
132
133
134
135
136
137
138
# File 'lib/fitting/doc/action.rb', line 131

def regexp
  return @regexp if @regexp

  str = Regexp.escape(@prefix + path)
  str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
  str = "\\A#{str}\\z"
  @regexp = Regexp.new(str)
end

#to_hashObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fitting/doc/action.rb', line 44

def to_hash
  res = [
    @host_cover,
    @prefix_cover,
    @path_cover,
    @method_cover
  ]
  (to_hash_lock[@key].size - 4).times { res.push(nil) }

  if @method_cover != nil && @method_cover != 0
    code_index = 4
    @responses.each do |code|
      res, code_index = code.report(res, code_index)
    end
  end

  { @key => res }
end

#to_hash_lockObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fitting/doc/action.rb', line 29

def to_hash_lock
  res = YAML.dump(
    {
      host => {
        prefix => {
          path => {
            method => @responses.inject({}) { |sum, response| sum.merge!(response) }
          }
        }
      }
    }
  ).split("\n")
  { @key => res[1..-1] }
end