rester 0.5.0 → 0.5.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: e0d4b971390eaa9fe9c3aa6abdfb754f141bd8bb
4
- data.tar.gz: 23c86151fc635c434a83249f85b0a66f66bf93ce
3
+ metadata.gz: a17ecdff2d900cdad60ccd5404afca94cfda269f
4
+ data.tar.gz: efa02cdbfef4743e9c0538896d769cb33c979a51
5
5
  SHA512:
6
- metadata.gz: 3782bdebc01e6c081ab57224f025b9c8abc0f5002b83f3cf120b67626232f42855910714885e0d0f49572eb011b8e3e537f175fa0aa13e4d0db3e5d7c9e9839c
7
- data.tar.gz: 6fe88686d1afa14ee878b4868ee3e67e201ca4ede8dfdaf0a9c4ab4369b81812cf51542aad5cd6cbec05042c0e624547af3457aabc4d5bd030d0111092406792
6
+ metadata.gz: 38c3311be4b40526035536d31cbbd1e682911e51cd54dcd53e1789aa356c44ef4ef38ff4bdf9b7012735c0d79d844a324eeedd7418706985ddabb756b7546bb5
7
+ data.tar.gz: 60f377dc74da842bf80696998657ce61549db1a7f7cb86f0d38967cd35a4311ae6d9d5d1d8c565956b56dcf0a82305cbac9d64044aabf9910569621cc42225df
@@ -0,0 +1,51 @@
1
+ require 'new_relic/agent'
2
+ require 'active_support/inflector'
3
+
4
+ NewRelic::Agent.manual_start unless defined?(Rails)
5
+
6
+ module Rester
7
+ module Middleware
8
+ class NewRelic < Base
9
+ include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
10
+
11
+ def call(env)
12
+ request = Service::Request.new(env)
13
+ name = identify_method(request)
14
+ ::NewRelic::Agent.set_transaction_name(name, category: :controller)
15
+ super
16
+ end
17
+
18
+ def identify_method(request)
19
+ object_chain = request.object_chain
20
+
21
+ if object_chain.length.odd?
22
+ resource_name = object_chain.last
23
+ else
24
+ resource_name = object_chain[-2]
25
+ end
26
+
27
+ method = _determine_method(request)
28
+
29
+ "#{service.class.name}::#{request.version.to_s.upcase}::"\
30
+ "#{resource_name.singularize.camelcase}/#{method}"
31
+ end
32
+
33
+ private
34
+
35
+ def _determine_method(request)
36
+ if request.object_chain.length.odd?
37
+ case request.request_method
38
+ when 'GET' then 'search'
39
+ when 'POST' then 'create'
40
+ end
41
+ else
42
+ case request.request_method
43
+ when 'GET' then 'get'
44
+ when 'PUT' then 'update'
45
+ when 'DELETE' then 'delete'
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -3,5 +3,6 @@ module Rester
3
3
  autoload(:Base, 'rester/middleware/base')
4
4
  autoload(:ErrorHandling, 'rester/middleware/error_handling')
5
5
  autoload(:Ping, 'rester/middleware/ping')
6
+ autoload(:NewRelic, 'rester/middleware/new_relic')
6
7
  end
7
8
  end
data/lib/rester/rspec.rb CHANGED
@@ -37,6 +37,16 @@ RSpec.configure do |config|
37
37
  end
38
38
 
39
39
  config.before :each, rester: // do |ex|
40
+ _setup_example(ex) unless ex.pending?
41
+ end
42
+
43
+ config.after :each, rester: // do |ex|
44
+ if defined?(service_response_code)
45
+ expect(service_response_code).to eq stub_response_code
46
+ end
47
+ end
48
+
49
+ def _setup_example(ex)
40
50
  # Gather the request args from the spec descriptions
41
51
  #
42
52
  # For example:
@@ -96,10 +106,6 @@ RSpec.configure do |config|
96
106
  ex.example_group.let(:subject) { service_response }
97
107
  end
98
108
 
99
- config.after :each, rester: // do |ex|
100
- expect(service_response_code).to eq stub_response_code
101
- end
102
-
103
109
  ##
104
110
  # Check to see if each stub example has a corresponding test written for it
105
111
  def _validate_test_coverage(ex)
@@ -115,8 +121,7 @@ RSpec.configure do |config|
115
121
  verb_group = _find_or_create_child(path_group, missing_verb)
116
122
 
117
123
  missing_contexts.each { |missing_context, _|
118
- context_group = _find_or_create_child(verb_group, missing_context)
119
- context_group.it { is_expected.to include_stub_response }
124
+ _find_or_create_child(verb_group, missing_context).pending
120
125
  }
121
126
  }
122
127
  }
@@ -15,6 +15,16 @@ module Rester
15
15
  path.length < 256 && %r{\A/v\d+/(\w+/?)+\z}.match(path)
16
16
  end
17
17
 
18
+ def each_resource
19
+ return unless (chain = object_chain)
20
+
21
+ loop do
22
+ name, id, *chain = chain
23
+ yield name, id
24
+ break if chain.empty?
25
+ end
26
+ end
27
+
18
28
  private
19
29
 
20
30
  def _parse_path
@@ -192,6 +192,9 @@ module Rester
192
192
 
193
193
  def _validate_str(key, value, klass, opts)
194
194
  fail unless value.is_a?(String) # assert
195
+ if [Array, Hash].include?(klass)
196
+ _error!("expected #{key} to be of type #{klass}")
197
+ end
195
198
 
196
199
  _validate_match(key, value, opts[:match]) if opts[:match]
197
200
  _parse_with_class(klass, value).tap do |obj|
@@ -201,7 +204,7 @@ module Rester
201
204
  end
202
205
 
203
206
  def _validate_array(key, value, klass, opts)
204
- _error!("unexpected array for #{key}") unless klass == Array
207
+ _error!("unexpected Array for #{key}") unless klass == Array
205
208
  type = (opts = opts.dup).delete(:type) || String
206
209
 
207
210
  value.each_with_index
@@ -209,7 +212,7 @@ module Rester
209
212
  end
210
213
 
211
214
  def _validate_hash(key, value, klass, opts)
212
- _error!("unexpected hash for #{key}") unless klass == Hash
215
+ _error!("unexpected Hash for #{key}") unless klass == Hash
213
216
  (validator = opts[:use]) && validator.validate(value)
214
217
  end
215
218
 
@@ -143,38 +143,39 @@ module Rester
143
143
  def _call_method(request)
144
144
  params = request.params
145
145
  retval = nil
146
-
147
- name, id, *object_chain = request.object_chain
148
- obj = _load_resource(request, name)
149
-
150
- loop {
151
- params.merge!(obj.id_param => id) if id
152
-
153
- if object_chain.empty?
154
- retval = obj.process(request.request_method, !!id, params)
155
- break
146
+ resource_obj = nil
147
+ resource_id = nil
148
+
149
+ request.each_resource do |name, id|
150
+ unless resource_obj
151
+ (resource_obj = _load_resource(request.version, name)) or
152
+ _error!(Errors::NotFoundError)
153
+ else
154
+ mounted_resource = resource_obj.mounts[name] or
155
+ _error!(Errors::NotFoundError)
156
+ resource_obj = mounted_resource.new
156
157
  end
157
158
 
158
- name, id, *object_chain = object_chain
159
- obj = obj.mounts[name].new or fail Errors::NotFoundError
160
- }
159
+ params.merge!(resource_obj.id_param => id) if id
160
+ resource_id = id
161
+ end
161
162
 
162
- retval
163
+ resource_obj.process(request.request_method, !!resource_id, params)
163
164
  end
164
165
 
165
166
  ##
166
167
  # Loads the appropriate Service::Resource for the request. This will return
167
168
  # the class, not an instance.
168
- def _load_resource(request, name)
169
- _version_module(request).const_get(name.camelcase.singularize).new
169
+ def _load_resource(version, name)
170
+ _version_module(version).const_get(name.camelcase.singularize).new
170
171
  rescue NameError
171
- _error!(Errors::NotFoundError)
172
+ nil
172
173
  end
173
174
 
174
175
  ##
175
176
  # Returns the module specified by the version in the request.
176
- def _version_module(request)
177
- self.class.version_module(request.version)
177
+ def _version_module(version)
178
+ self.class.version_module(version)
178
179
  end
179
180
 
180
181
  ##
@@ -1,3 +1,3 @@
1
1
  module Rester
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-10 00:00:00.000000000 Z
12
+ date: 2016-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -115,6 +115,20 @@ dependencies:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: newrelic_rpm
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
118
132
  description: A framework for creating simple RESTful interfaces between services.
119
133
  email:
120
134
  - robert@payout.com
@@ -137,6 +151,7 @@ files:
137
151
  - lib/rester/middleware.rb
138
152
  - lib/rester/middleware/base.rb
139
153
  - lib/rester/middleware/error_handling.rb
154
+ - lib/rester/middleware/new_relic.rb
140
155
  - lib/rester/middleware/ping.rb
141
156
  - lib/rester/railtie.rb
142
157
  - lib/rester/rspec.rb
@@ -169,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
184
  version: '0'
170
185
  requirements: []
171
186
  rubyforge_project:
172
- rubygems_version: 2.4.8
187
+ rubygems_version: 2.4.6
173
188
  signing_key:
174
189
  specification_version: 4
175
190
  summary: A framework for creating simple RESTful interfaces between services.