sinatra-rest-base 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57d73eb788f53e9e785801ac3bc484fa3f64ddca
4
- data.tar.gz: df2f1dc92f930e864c202809ac7015f9bf6dafca
3
+ metadata.gz: 5b8c04a79fba6e26cc1309e58107b3e06d950364
4
+ data.tar.gz: fce5fc71f80bbbbb0a51c3b023fdd1eb220e01a9
5
5
  SHA512:
6
- metadata.gz: ebaf5fdcef53ed2b83f9d10e2709ddbefb43c9ad2d7a72890f3c5a353badcb8bac117eb77eaedc2495494d1549b1f929a18af8fab0be13e985c13cd27c315978
7
- data.tar.gz: e25c7baf2ae4b255d08976a2b12d663b814821dfb5c8408601706c2d7898f86faa4ba8bf96003004eadf433e6017091c37ea010fc952bfc1bf6a0b0261e065c7
6
+ metadata.gz: 0077f646ba6a2b2056b4aa2a34e41c013133d60773b282b9a81e6e83172d71daa7e3c2384615ebf1c17d2d88d07b110966117bef00a3e807a3410cb04c46ac3d
7
+ data.tar.gz: 8f78662d0f61227f4265f4cd6747ee76598a3439eae312298b40aa8f565a7aab7a1b7516ab5f965587527e724ba409ed2df2895336b95acbd04d1011e087b92d
@@ -9,6 +9,7 @@ module RestBase
9
9
  attr_reader :request_body
10
10
  attr_reader :request_version
11
11
  attr_reader :errored
12
+ attr_reader :forensics
12
13
 
13
14
  configure do
14
15
  disable :raise_errors
@@ -29,6 +30,7 @@ module RestBase
29
30
 
30
31
  set_api_version if settings.header_versioning
31
32
  set_request_body
33
+ setup_forensics
32
34
  error 401 unless authenticated?
33
35
  end
34
36
 
@@ -36,6 +36,14 @@ module RestBase
36
36
  end
37
37
  end
38
38
 
39
+ def setup_forensics
40
+ @forensics = []
41
+ end
42
+
43
+ def add_forensics_to_response?
44
+ !@env['Forensics'].nil? && @env['Forensics'].downcase == 'true'
45
+ end
46
+
39
47
  def create_response()
40
48
  content_type :json
41
49
  content_type :xml if !request.accept?('*/*') && request.accept?('text/xml')
@@ -47,10 +55,10 @@ module RestBase
47
55
  response.body = nil
48
56
  end
49
57
 
50
- response_hash = {
51
- :errors => error_hash,
52
- :results => response.body
53
- }
58
+ response_hash = { }
59
+ response_hash[:errors] = error_hash unless error_hash.nil? || error_hash.empty?
60
+ response_hash[:result] = response.body unless response.body.nil?
61
+ response_hash[:forensics] = @forensics if add_forensics_to_response? && !(@forensics.nil? || @forensics.empty?)
54
62
 
55
63
  return '' if empty_body_status_code? && (response.body.empty?)
56
64
  return response_hash.to_xml(:root => :response) if response.headers['Content-Type'].start_with? 'application/xml;'
@@ -1,3 +1,3 @@
1
1
  module RestBase
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -0,0 +1,14 @@
1
+ module RestBase
2
+ module Test
3
+ class TestApplication
4
+ get '/forensics', :version => 1 do
5
+ @forensics << "Added Forensics"
6
+ "Forensics Test"
7
+ end
8
+
9
+ get '/forensics_empty', :version => 1 do
10
+ "Forensics Test"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -23,7 +23,7 @@ describe RestBase::Application do
23
23
 
24
24
  expect(last_request.env["HTTP_ACCEPT"].include? expected_version).to be_truthy
25
25
  expect(last_response.headers["Content-Type"].include? expected_version).to be_truthy
26
- expect(last_response.body).to eq({:errors => nil, :results => { :gotten => "success" }}.to_json)
26
+ expect(last_response.body).to eq({:result => { :gotten => "success" }}.to_json)
27
27
  end
28
28
 
29
29
  it "should get the version 2 version" do
@@ -34,7 +34,7 @@ describe RestBase::Application do
34
34
 
35
35
  expect(last_request.env["HTTP_ACCEPT"].include? expected_version).to be_truthy
36
36
  expect(last_response.headers["Content-Type"].include? expected_version).to be_truthy
37
- expect(last_response.body).to eq({:errors => nil, :results => { :gotten => "success 2" }}.to_json)
37
+ expect(last_response.body).to eq({:result => { :gotten => "success 2" }}.to_json)
38
38
  end
39
39
  end
40
40
 
@@ -56,7 +56,7 @@ describe RestBase::Application do
56
56
  it "should return an empty string response" do
57
57
  get '/empty', {}, @headers
58
58
 
59
- expect(last_response.body).to eq({:errors => nil, :results => nil}.to_json)
59
+ expect(last_response.body).to eq({}.to_json)
60
60
  end
61
61
  end
62
62
 
@@ -79,7 +79,7 @@ describe RestBase::Application do
79
79
  get '/created_with_body', {}, @headers
80
80
 
81
81
  expect(last_response.status).to eq(201)
82
- expect(last_response.body).to eq({:errors => nil, :results => {:created => true}}.to_json)
82
+ expect(last_response.body).to eq({:result => {:created => true}}.to_json)
83
83
  end
84
84
 
85
85
  it "should return a not modified response with empty body" do
@@ -96,16 +96,44 @@ describe RestBase::Application do
96
96
  expect(last_response.body).to be_empty
97
97
  end
98
98
  end
99
-
99
+
100
+ describe :forensics do
101
+ before(:each) do
102
+ @expected_hash = {:result => ["Forensics Test"], :forensics => ["Added Forensics"]}
103
+ @headers['Forensics'] = 'TRUE'
104
+ end
105
+
106
+ it "should add forensics to body" do
107
+ get '/forensics', {}, @headers
108
+
109
+ expect(last_response.body).to eq(@expected_hash.to_json)
110
+ end
111
+
112
+ it "should not add forensics to body" do
113
+ @headers.delete('Forensics')
114
+ get '/forensics', {}, @headers
115
+
116
+ @expected_hash.delete(:forensics)
117
+ expect(last_response.body).to eq(@expected_hash.to_json)
118
+ end
119
+
120
+ it "should not add nil/empty forensics to body" do
121
+ get '/forensics_empty', {}, @headers
122
+
123
+ @expected_hash.delete(:forensics)
124
+ expect(last_response.body).to eq(@expected_hash.to_json)
125
+ end
126
+ end
127
+
100
128
  describe :json do
101
129
  before(:each) do
102
130
  @posted_body = { :success => true }
103
131
 
104
- @expected_hash = {:errors => nil, :results => { :posted => @posted_body }}
132
+ @expected_hash = {:result => { :posted => @posted_body }}
105
133
  end
106
134
 
107
135
  it "should get json" do
108
- @expected_hash = {:errors => nil, :results => { :gotten => "success" }}
136
+ @expected_hash = {:result => { :gotten => "success" }}
109
137
 
110
138
  get '/', {}, @headers
111
139
 
@@ -130,7 +158,7 @@ describe RestBase::Application do
130
158
  it "should post empty json and return json" do
131
159
  post '/', "", @headers
132
160
 
133
- @expected_hash[:results][:posted] = {}
161
+ @expected_hash[:result][:posted] = {}
134
162
  expect(last_response.body).to eq(@expected_hash.to_json)
135
163
  end
136
164
 
@@ -145,12 +173,12 @@ describe RestBase::Application do
145
173
  before(:each) do
146
174
  @posted_body = { :success => true }
147
175
 
148
- @expected_hash = {:errors => nil, :results => { :posted => @posted_body }}
176
+ @expected_hash = {:result => { :posted => @posted_body }}
149
177
  end
150
178
 
151
179
  it "should get xml" do
152
180
  @headers['HTTP_ACCEPT'] = 'text/xml'
153
- @expected_hash = { :errors => nil, :results => { :gotten => 'success'} }
181
+ @expected_hash = {:result => { :gotten => 'success'} }
154
182
 
155
183
  get '/', {}, @headers
156
184
 
@@ -12,8 +12,7 @@ describe RestBase::Application do
12
12
  "Invalid Request Format",
13
13
  "param1 is not valid: Must be a String",
14
14
  "param2 is not valid: Must be an Integer"
15
- ],
16
- :results => nil
15
+ ]
17
16
  }
18
17
 
19
18
  get '/invalid_request', {}, @headers
@@ -3,8 +3,7 @@ require 'spec_helper'
3
3
  describe RestBase::Application do
4
4
  before(:each) do
5
5
  @expected_body = {
6
- :errors => [],
7
- :results => nil
6
+ :errors => []
8
7
  }
9
8
 
10
9
  @headers = { 'HTTP_AUTHORIZATION' => 'key1' }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rest-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - bmills
@@ -177,6 +177,7 @@ files:
177
177
  - spec/fixtures/test_application.rb
178
178
  - spec/fixtures/test_end_points.rb
179
179
  - spec/fixtures/test_errors_end_points.rb
180
+ - spec/fixtures/test_forensics.rb
180
181
  - spec/lib/rest_base/application_spec.rb
181
182
  - spec/lib/rest_base/authentication/authentication_base_spec.rb
182
183
  - spec/lib/rest_base/authentication/basic_authentication_spec.rb
@@ -211,6 +212,7 @@ test_files:
211
212
  - spec/fixtures/test_application.rb
212
213
  - spec/fixtures/test_end_points.rb
213
214
  - spec/fixtures/test_errors_end_points.rb
215
+ - spec/fixtures/test_forensics.rb
214
216
  - spec/lib/rest_base/application_spec.rb
215
217
  - spec/lib/rest_base/authentication/authentication_base_spec.rb
216
218
  - spec/lib/rest_base/authentication/basic_authentication_spec.rb