http-double 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 588746bf46fcf2a95cdc80f2dd92a4b0967bcf51
4
- data.tar.gz: da0b030d79ac9cff01c565a5e7d72548bab8bc81
3
+ metadata.gz: 1658a9ed204156a7f61cd7799d5c12394bbb3d44
4
+ data.tar.gz: 25a71de1ce9365e1a90440e89eedbb9798b7ec19
5
5
  SHA512:
6
- metadata.gz: f2b4f2e91049966762fa4680c30c98b03f8406dc2d2a7de70fc2a506f5681e7d0d841d53b535521690bab2472f4589336588a7c20a19446ea253b38767336456
7
- data.tar.gz: 996a293170fd7b7e9484412026161fbf5a12bf47c3355d9c9836ab5cdccc5f969a4f35b3f4d47a76b529560ab0a0f8a9026a52803dd808411b1cd93d3e594548
6
+ metadata.gz: 3ee2b0dac09422840ff6502cb61f723a132ce92d561ac272596a441fc900d1dd1448703cb6d758a4b07036610a2969a72b732386a06c9c7edb2592c2d1da4fec
7
+ data.tar.gz: fdb056755c01ff438db6439c7c1587327a727295703c5d8120882113b23b9c4d9c35d99e613a29acd47356426a31f4c92b781698dc02b11914e32a83efa66744
@@ -1,6 +1,7 @@
1
1
  require 'rack/utils'
2
2
  require 'ostruct'
3
3
  require 'active_support/hash_with_indifferent_access'
4
+ require 'forwardable'
4
5
 
5
6
  class HttpDouble::RequestLogger
6
7
 
@@ -24,12 +25,16 @@ class HttpDouble::RequestLogger
24
25
  end
25
26
 
26
27
  class Request
28
+ extend Forwardable
27
29
 
28
- attr_reader :env, :body
30
+ attr_reader :env
29
31
 
30
32
  def initialize(env)
31
33
  @env = env
32
- @body = env['rack.input'].read
34
+ end
35
+
36
+ def body
37
+ @body ||= rack_input.rewind && rack_input.read
33
38
  end
34
39
 
35
40
  def verb
@@ -41,22 +46,35 @@ class HttpDouble::RequestLogger
41
46
  end
42
47
 
43
48
  def [](field, index = nil)
44
- case env['CONTENT_TYPE']
45
- when 'application/x-www-form-urlencoded'
46
- result = form_fields[field]
47
- result = result[index] if result and index
48
- result
49
- else
50
- raise "The content type '#{env['CONTENT_TYPE']}' doesn't support indexed access"
51
- end
49
+ result = parsed_input[field]
50
+ result = result[index] if result and index
51
+ result
52
52
  end
53
53
 
54
+ delegate %i[first last map each size count length] => :parsed_input
55
+
54
56
  private
55
57
 
58
+ def parsed_input
59
+ @parsed_input ||= case env['CONTENT_TYPE']
60
+ when 'application/x-www-form-urlencoded' then form_fields
61
+ when 'application/json', 'application/x-json' then json_input
62
+ else raise "The content type '#{env['CONTENT_TYPE']}' doesn't support indexed access"
63
+ end
64
+ end
65
+
66
+ def rack_input
67
+ @rack_input ||= env['rack.input']
68
+ end
69
+
56
70
  def form_fields
57
71
  @form_fields ||= IHash.new(Rack::Utils.parse_query(body).map { |key, value| [key.to_sym, value.is_a?(Array) ? value : [value]] }.to_h)
58
72
  end
59
73
 
74
+ def json_input
75
+ @json_input ||= JSON.parse body, quirks_mode: true
76
+ end
77
+
60
78
  end
61
79
 
62
80
  end
@@ -1,3 +1,3 @@
1
1
  module HttpDouble
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-double
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson