response 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +16 -0
  4. data/Changelog.md +3 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.devtools +60 -0
  7. data/Guardfile +18 -0
  8. data/LICENSE +20 -0
  9. data/README.md +39 -0
  10. data/Rakefile +2 -0
  11. data/TODO +2 -0
  12. data/config/devtools.yml +2 -0
  13. data/config/flay.yml +3 -0
  14. data/config/flog.yml +2 -0
  15. data/config/mutant.yml +3 -0
  16. data/config/reek.yml +92 -0
  17. data/config/roodi.yml +18 -0
  18. data/config/yardstick.yml +2 -0
  19. data/lib/response/html.rb +36 -0
  20. data/lib/response/json.rb +36 -0
  21. data/lib/response/redirect.rb +52 -0
  22. data/lib/response/status.rb +28 -0
  23. data/lib/response/text.rb +37 -0
  24. data/lib/response/xml.rb +36 -0
  25. data/lib/response.rb +271 -0
  26. data/response.gemspec +22 -0
  27. data/spec/rcov.opts +7 -0
  28. data/spec/shared/functional_command_method_behavior.rb +11 -0
  29. data/spec/spec_helper.rb +8 -0
  30. data/spec/unit/response/body_spec.rb +22 -0
  31. data/spec/unit/response/cache_control_spec.rb +25 -0
  32. data/spec/unit/response/class_methods/build_spec.rb +31 -0
  33. data/spec/unit/response/content_type_spec.rb +26 -0
  34. data/spec/unit/response/headers_spec.rb +23 -0
  35. data/spec/unit/response/html/class_methods/build_spec.rb +18 -0
  36. data/spec/unit/response/json/class_methods/build_spec.rb +18 -0
  37. data/spec/unit/response/last_modified_spec.rb +25 -0
  38. data/spec/unit/response/merge_headers_spec.rb +20 -0
  39. data/spec/unit/response/rack_array_spec.rb +15 -0
  40. data/spec/unit/response/redirect/class_methods/build_spec.rb +28 -0
  41. data/spec/unit/response/status_spec.rb +22 -0
  42. data/spec/unit/response/text/class_methods/build_spec.rb +18 -0
  43. data/spec/unit/response/to_rack_response_spec.rb +26 -0
  44. data/spec/unit/response/valid_predicate_spec.rb +37 -0
  45. data/spec/unit/response/with_body_spec.rb +17 -0
  46. data/spec/unit/response/with_headers_spec.rb +17 -0
  47. data/spec/unit/response/with_status_spec.rb +17 -0
  48. data/spec/unit/response/xml/class_methods/build_spec.rb +18 -0
  49. data/tasks/metrics/ci.rake +7 -0
  50. data/tasks/metrics/flay.rake +47 -0
  51. data/tasks/metrics/flog.rake +43 -0
  52. data/tasks/metrics/heckle.rake +208 -0
  53. data/tasks/metrics/metric_fu.rake +29 -0
  54. data/tasks/metrics/reek.rake +15 -0
  55. data/tasks/metrics/roodi.rake +15 -0
  56. data/tasks/metrics/yardstick.rake +23 -0
  57. data/tasks/spec.rake +45 -0
  58. data/tasks/yard.rake +9 -0
  59. metadata +187 -0
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Response, '#with_body' do
4
+ subject { object.with_status(status).with_headers(headers).with_body(body) }
5
+
6
+ let(:object) { described_class.build }
7
+
8
+ let(:status) { Response::Status::OK }
9
+ let(:body) { mock('Body') }
10
+ let(:headers) { mock('Headers') }
11
+
12
+ its(:status) { should be(status) }
13
+ its(:body) { should be(body) }
14
+ its(:headers) { should be(headers) }
15
+
16
+ it_should_behave_like 'a functional command method'
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Response, '#with_headers' do
4
+ subject { object.with_status(status).with_body(body).with_headers(headers) }
5
+
6
+ let(:object) { described_class.build }
7
+
8
+ let(:status) { Response::Status::OK }
9
+ let(:body) { mock('Body') }
10
+ let(:headers) { mock('Headers') }
11
+
12
+ its(:status) { should be(status) }
13
+ its(:body) { should be(body) }
14
+ its(:headers) { should be(headers) }
15
+
16
+ it_should_behave_like 'a functional command method'
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Response, '#with_status' do
4
+ subject { object.with_headers(headers).with_body(body).with_status(status) }
5
+
6
+ let(:object) { described_class.build }
7
+ let(:status) { Response::Status::OK }
8
+
9
+ let(:body) { mock('Body') }
10
+ let(:headers) { mock('Headers') }
11
+
12
+ its(:status) { should be(status) }
13
+ its(:body) { should be(body) }
14
+ its(:headers) { should be(headers) }
15
+
16
+ it_should_behave_like 'a functional command method'
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Response::XML, '.build' do
4
+ subject { object.build(body) }
5
+
6
+ let(:body) { mock('Body') }
7
+ let(:object) { described_class }
8
+
9
+ its(:status) { should be(Response::Status::OK) }
10
+ its(:body) { should be(body) }
11
+ its(:headers) { should eql('Content-Type' => 'application/xml; charset=UTF-8') }
12
+
13
+ it 'allows to modify response' do
14
+ object.build(body) do |response|
15
+ response.with_status(404)
16
+ end.status.should be(404)
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ desc 'Run metrics with Heckle'
2
+ task :ci => %w[ ci:metrics heckle ]
3
+
4
+ namespace :ci do
5
+ desc 'Run metrics'
6
+ task :metrics => %w[ verify_measurements flog flay reek roodi metrics:all ]
7
+ end
@@ -0,0 +1,47 @@
1
+ begin
2
+ if RUBY_VERSION == '1.8.7'
3
+ require 'flay'
4
+ require 'yaml'
5
+
6
+ config = YAML.load_file(File.expand_path('../../../config/flay.yml', __FILE__)).freeze
7
+ threshold = config.fetch('threshold').to_i
8
+ total_score = config.fetch('total_score').to_f
9
+ files = Flay.expand_dirs_to_files(config.fetch('path', 'lib'))
10
+
11
+ # original code by Marty Andrews:
12
+ # http://blog.martyandrews.net/2009/05/enforcing-ruby-code-quality.html
13
+ desc 'Analyze for code duplication'
14
+ task :flay do
15
+ # run flay once without a threshold to ensure the max mass matches the threshold
16
+ flay = Flay.new(:fuzzy => false, :verbose => false, :mass => 0)
17
+ flay.process(*files)
18
+
19
+ max = flay.masses.map { |hash, mass| mass.to_f / flay.hashes[hash].size }.max
20
+ unless max >= threshold
21
+ raise "Adjust flay threshold down to #{max}"
22
+ end
23
+
24
+ total = flay.masses.reduce(0.0) { |total, (hash, mass)| total + (mass.to_f / flay.hashes[hash].size) }
25
+ unless total == total_score
26
+ raise "Flay total is now #{total}, but expected #{total_score}"
27
+ end
28
+
29
+ # run flay a second time with the threshold set
30
+ flay = Flay.new(:fuzzy => false, :verbose => false, :mass => threshold.succ)
31
+ flay.process(*files)
32
+
33
+ if flay.masses.any?
34
+ flay.report
35
+ raise "#{flay.masses.size} chunks of code have a duplicate mass > #{threshold}"
36
+ end
37
+ end
38
+ else
39
+ task :flay do
40
+ $stderr.puts 'Flay has inconsistend results accros ruby implementations. It is only enabled on 1.8.7, fix and remove guard'
41
+ end
42
+ end
43
+ rescue LoadError
44
+ task :flay do
45
+ abort 'Flay is not available. In order to run flay, you must: gem install flay'
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ begin
2
+ require 'flog'
3
+ require 'yaml'
4
+
5
+ class Float
6
+ def round_to(n)
7
+ (self * 10**n).round.to_f * 10**-n
8
+ end
9
+ end
10
+
11
+ config = YAML.load_file(File.expand_path('../../../config/flog.yml', __FILE__)).freeze
12
+ threshold = config.fetch('threshold').to_f.round_to(1)
13
+
14
+ # original code by Marty Andrews:
15
+ # http://blog.martyandrews.net/2009/05/enforcing-ruby-code-quality.html
16
+ desc 'Analyze for code complexity'
17
+ task :flog do
18
+ flog = Flog.new
19
+ flog.flog Array(config.fetch('path', 'lib'))
20
+
21
+ totals = flog.totals.select { |name, score| name[-5, 5] != '#none' }.
22
+ map { |name, score| [ name, score.round_to(1) ] }.
23
+ sort_by { |name, score| score }
24
+
25
+ max = totals.last[1]
26
+ unless max >= threshold
27
+ raise "Adjust flog score down to #{max}"
28
+ end
29
+
30
+ bad_methods = totals.select { |name, score| score > threshold }
31
+ if bad_methods.any?
32
+ bad_methods.reverse_each do |name, score|
33
+ puts '%8.1f: %s' % [ score, name ]
34
+ end
35
+
36
+ raise "#{bad_methods.size} methods have a flog complexity > #{threshold}"
37
+ end
38
+ end
39
+ rescue LoadError
40
+ task :flog do
41
+ abort 'Flog is not available. In order to run flog, you must: gem install flog'
42
+ end
43
+ end
@@ -0,0 +1,208 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
2
+
3
+ # original code by Ashley Moran:
4
+ # http://aviewfromafar.net/2007/11/1/rake-task-for-heckling-your-specs
5
+
6
+ begin
7
+ require 'pathname'
8
+ require 'backports'
9
+ require 'active_support/inflector'
10
+ require 'heckle'
11
+ require 'mspec'
12
+ require 'mspec/utils/name_map'
13
+
14
+ SKIP_METHODS = %w[ blank_slate_method_added ].freeze
15
+
16
+ class NameMap
17
+ def file_name(method, constant)
18
+ map = MAP[method]
19
+ name = if map
20
+ map[constant] || map[:default]
21
+ else
22
+ method.
23
+ gsub('?','_ques').
24
+ gsub('!','_bang').
25
+ gsub('=','_assign')
26
+ end
27
+ "#{name}_spec.rb"
28
+ end
29
+ end
30
+
31
+ desc 'Heckle each module and class'
32
+ task :heckle => :rcov do
33
+ unless Ruby2Ruby::VERSION == '1.2.2'
34
+ raise "ruby2ruby version #{Ruby2Ruby::VERSION} may not work properly, 1.2.2 *only* is recommended for use with heckle"
35
+ end
36
+
37
+ require 'response'
38
+
39
+ root_module_regexp = Regexp.union('Response')
40
+
41
+ spec_dir = Pathname('spec/unit')
42
+
43
+ NameMap::MAP.each do |op, method|
44
+ next if method.kind_of?(Hash)
45
+ NameMap::MAP[op] = { :default => method }
46
+ end
47
+
48
+ aliases = Hash.new { |h,mod| h[mod] = Hash.new { |h,method| h[method] = method } }
49
+ map = NameMap.new
50
+
51
+ heckle_caught_modules = Hash.new { |hash, key| hash[key] = [] }
52
+ unhandled_mutations = 0
53
+
54
+ ObjectSpace.each_object(Module) do |mod|
55
+ next unless mod.name =~ /\A#{root_module_regexp}(?::|\z)/
56
+
57
+ spec_prefix = spec_dir.join(mod.name.underscore)
58
+
59
+ specs = []
60
+
61
+ # get the public class methods
62
+ metaclass = class << mod; self end
63
+ ancestors = metaclass.ancestors
64
+
65
+ spec_class_methods = mod.singleton_methods(false)
66
+
67
+ spec_class_methods.reject! do |method|
68
+ %w[ yaml_new yaml_tag_subclasses? included nesting constants ].include?(method.to_s)
69
+ end
70
+
71
+ if mod.ancestors.include?(Singleton)
72
+ spec_class_methods.reject! { |method| method.to_s == 'instance' }
73
+ end
74
+
75
+ # get the protected and private class methods
76
+ other_class_methods = metaclass.protected_instance_methods(false) |
77
+ metaclass.private_instance_methods(false)
78
+
79
+ ancestors.each do |ancestor|
80
+ other_class_methods -= ancestor.protected_instance_methods(false) |
81
+ ancestor.private_instance_methods(false)
82
+ end
83
+
84
+ other_class_methods.reject! do |method|
85
+ method.to_s == 'allocate' || SKIP_METHODS.include?(method.to_s)
86
+ end
87
+
88
+ other_class_methods.reject! do |method|
89
+ next unless spec_class_methods.any? { |specced| specced.to_s == $1 }
90
+
91
+ spec_class_methods << method
92
+ end
93
+
94
+ # get the instances methods
95
+ spec_methods = mod.public_instance_methods(false)
96
+
97
+ other_methods = mod.protected_instance_methods(false) |
98
+ mod.private_instance_methods(false)
99
+
100
+ other_methods.reject! do |method|
101
+ next unless spec_methods.any? { |specced| specced.to_s == $1 }
102
+
103
+ spec_methods << method
104
+ end
105
+
106
+ # map the class methods to spec files
107
+ spec_class_methods.each do |method|
108
+ method = aliases[mod.name][method]
109
+ next if SKIP_METHODS.include?(method.to_s)
110
+
111
+ spec_file = spec_prefix.join('class_methods').join(map.file_name(method, mod.name))
112
+
113
+ unless spec_file.file?
114
+ raise "No spec file #{spec_file} for #{mod}.#{method}"
115
+ next
116
+ end
117
+
118
+ specs << [ ".#{method}", [ spec_file ] ]
119
+ end
120
+
121
+ # map the instance methods to spec files
122
+ spec_methods.each do |method|
123
+ method = aliases[mod.name][method]
124
+ next if SKIP_METHODS.include?(method.to_s)
125
+
126
+ spec_file = spec_prefix.join(map.file_name(method, mod.name))
127
+
128
+ unless spec_file.file?
129
+ raise "No spec file #{spec_file} for #{mod}##{method}"
130
+ next
131
+ end
132
+
133
+ specs << [ "##{method}", [ spec_file ] ]
134
+ end
135
+
136
+ # non-public methods are considered covered if they can be mutated
137
+ # and any spec fails for the current or descendant modules
138
+ other_methods.each do |method|
139
+ descedant_specs = []
140
+
141
+ ObjectSpace.each_object(Module) do |descedant|
142
+ next unless descedant.name =~ /\A#{root_module_regexp}(?::|\z)/ && mod >= descedant
143
+ descedant_spec_prefix = spec_dir.join(descedant.name.underscore)
144
+ descedant_specs << descedant_spec_prefix
145
+
146
+ if method.to_s == 'initialize'
147
+ descedant_specs.concat(Pathname.glob(descedant_spec_prefix.join('class_methods/new_spec.rb')))
148
+ end
149
+ end
150
+
151
+ specs << [ "##{method}", descedant_specs ]
152
+ end
153
+
154
+ other_class_methods.each do |method|
155
+ descedant_specs = []
156
+
157
+ ObjectSpace.each_object(Module) do |descedant|
158
+ next unless descedant.name =~ /\A#{root_module_regexp}(?::|\z)/ && mod >= descedant
159
+ descedant_specs << spec_dir.join(descedant.name.underscore).join('class_methods')
160
+ end
161
+
162
+ specs << [ ".#{method}", descedant_specs ]
163
+ end
164
+
165
+ specs.sort.each do |(method, spec_files)|
166
+ puts "Heckling #{mod}#{method}"
167
+ IO.popen("spec #{spec_files.join(' ')} --heckle '#{mod}#{method}'") do |pipe|
168
+ while line = pipe.gets
169
+ case line = line.chomp
170
+ when "The following mutations didn't cause test failures:"
171
+ heckle_caught_modules[mod.name] << method
172
+ when '+++ mutation'
173
+ unhandled_mutations += 1
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ if unhandled_mutations > 0
181
+ error_message_lines = [ "*************\n" ]
182
+
183
+ error_message_lines << "Heckle found #{unhandled_mutations} " \
184
+ "mutation#{"s" unless unhandled_mutations == 1} " \
185
+ "that didn't cause spec violations\n"
186
+
187
+ heckle_caught_modules.each do |mod, methods|
188
+ error_message_lines << "#{mod} contains the following " \
189
+ 'poorly-specified methods:'
190
+ methods.each do |method|
191
+ error_message_lines << " - #{method}"
192
+ end
193
+ error_message_lines << ''
194
+ end
195
+
196
+ error_message_lines << 'Get your act together and come back ' \
197
+ 'when your specs are doing their job!'
198
+
199
+ raise error_message_lines.join("\n")
200
+ else
201
+ puts 'Well done! Your code withstood a heckling.'
202
+ end
203
+ end
204
+ rescue LoadError
205
+ task :heckle => :spec do
206
+ $stderr.puts 'Heckle or mspec is not available. In order to run heckle, you must: gem install heckle mspec'
207
+ end
208
+ end
@@ -0,0 +1,29 @@
1
+ begin
2
+ require 'metric_fu'
3
+ require 'json'
4
+
5
+ # XXX: temporary hack until metric_fu is fixed
6
+ MetricFu::Saikuro.class_eval { include FileUtils }
7
+
8
+ MetricFu::Configuration.run do |config|
9
+ config.rcov = {
10
+ :environment => 'test',
11
+ :test_files => %w[ spec/**/*_spec.rb ],
12
+ :rcov_opts => %w[
13
+ --sort coverage
14
+ --no-html
15
+ --text-coverage
16
+ --no-color
17
+ --profile
18
+ --exclude spec/,^/
19
+ --include lib:spec
20
+ ],
21
+ }
22
+ end
23
+ rescue LoadError
24
+ namespace :metrics do
25
+ task :all do
26
+ $stderr.puts 'metric_fu is not available. In order to run metrics:all, you must: gem install metric_fu'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'reek/rake/task'
3
+
4
+ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'rbx'
5
+ task :reek do
6
+ $stderr.puts 'Reek fails under rubinius, fix rubinius and remove guard'
7
+ end
8
+ else
9
+ Reek::Rake::Task.new
10
+ end
11
+ rescue LoadError
12
+ task :reek do
13
+ $stderr.puts 'Reek is not available. In order to run reek, you must: gem install reek'
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'roodi'
3
+ require 'rake/tasklib'
4
+ require 'roodi_task'
5
+
6
+ RoodiTask.new do |t|
7
+ t.verbose = false
8
+ t.config = File.expand_path('../../../config/roodi.yml', __FILE__)
9
+ t.patterns = %w[ lib/**/*.rb ]
10
+ end
11
+ rescue LoadError
12
+ task :roodi do
13
+ abort 'Roodi is not available. In order to run roodi, you must: gem install roodi'
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick'
4
+ require 'yardstick/rake/measurement'
5
+ require 'yardstick/rake/verify'
6
+ require 'yaml'
7
+
8
+ config = YAML.load_file(File.expand_path('../../../config/yardstick.yml', __FILE__))
9
+
10
+ # yardstick_measure task
11
+ Yardstick::Rake::Measurement.new
12
+
13
+ # verify_measurements task
14
+ Yardstick::Rake::Verify.new do |verify|
15
+ verify.threshold = config.fetch('threshold')
16
+ end
17
+ rescue LoadError
18
+ %w[ yardstick_measure verify_measurements ].each do |name|
19
+ task name.to_s do
20
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
21
+ end
22
+ end
23
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,45 @@
1
+ begin
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ rescue LoadError
6
+ require 'spec/rake/spectask'
7
+
8
+ module RSpec
9
+ module Core
10
+ RakeTask = Spec::Rake::SpecTask
11
+ end
12
+ end
13
+ end
14
+
15
+ desc 'run all specs'
16
+ task :spec => %w[ spec:unit spec:integration ]
17
+
18
+ namespace :spec do
19
+ RSpec::Core::RakeTask.new(:integration) do |t|
20
+ t.pattern = 'spec/integration/**/*_spec.rb'
21
+ end
22
+
23
+ RSpec::Core::RakeTask.new(:unit) do |t|
24
+ t.pattern = 'spec/unit/**/*_spec.rb'
25
+ end
26
+ end
27
+ rescue LoadError
28
+ task :spec do
29
+ abort 'rspec is not available. In order to run spec, you must: gem install rspec'
30
+ end
31
+ end
32
+
33
+ begin
34
+ desc "Generate code coverage"
35
+ RSpec::Core::RakeTask.new(:rcov) do |t|
36
+ t.rcov = true
37
+ t.rcov_opts = File.read('spec/rcov.opts').split(/\s+/)
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort 'rcov is not available. In order to run rcov, you must: gem install rcov'
42
+ end
43
+ end
44
+
45
+ task :test => 'spec'
data/tasks/yard.rake ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end