toast 1.0.2 → 1.0.3

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: 5a7b7339a3fd66ef55e9261818f11458c36c7164
4
- data.tar.gz: c3e72c819be4ebcc9769d77411bbabb088db2abf
3
+ metadata.gz: 277d6133963ef4bab7c92dec2cfcf4e57c59f7e3
4
+ data.tar.gz: f592710bc8498778fd12760cd8439eea36bab6f5
5
5
  SHA512:
6
- metadata.gz: efbebf7fd608c4cde8666531e5b55531b41cd81b41e1c55e50acf636498d0e031ed822c378bcba2811aad76cafbc700fb0ef4f6057a1a6faf3e1c51e804732db
7
- data.tar.gz: 37626365865ea3b1cd05a552b53821ba7d2bc02572c444071fe6acfde7b1940233540d39902d122f284c0400ffb35e6d99876cba014115a5875c87a3155317d7
6
+ metadata.gz: 95033345cb023b2ffcdf47b2ea000b6c2147a9daab5268de4e4bc565ae811fc6d6e3d5fd06a2d431b16d79c21b00872bc44d44f6913f70b4601d60e23b43a0f4
7
+ data.tar.gz: 7db559b1ae70778c937c1bb59c4888fb508e29e30f83cfa0c5cf65d7197d010583c479b31ebb8b2aae5194fcff97a8fdd8808012a66c981e89467bedcc71961f
data/README.md CHANGED
@@ -33,16 +33,6 @@ Works with Rails 5 and 4.2.9+. This version will be tested with upcoming new Rai
33
33
 
34
34
  Works with Rails 3 and 4. It has a much different and smaller DSL, which is not compatible with v1. This version will not receive any updates or fixes anymore.
35
35
 
36
- Status
37
- ======
38
-
39
- Toast v1 for Rails 5 is a complete rewrite of v0.9, which was first published and used in production since 2012.
40
- It comes now with secure defaults: Nothing is exposed unless declared, all endpoints have a default authorization hook responding with 401.
41
-
42
- v1 is not compatible with v0.9, and it is not tested with Rails < v5. All configurations must be ported to the new DSL.
43
-
44
- From my point of view it is production ready. I am in the process of porting a large API from v0.9 to v1 that uses all features and it looks very good so far. Of course minor issues will appear, please help to report and fix them.
45
-
46
36
  Installation
47
37
  ============
48
38
 
data/lib/toast.rb CHANGED
@@ -12,7 +12,10 @@ module Toast
12
12
  # collects all configs of one expose block (DSL methods write to it)
13
13
  @@current_expose = nil
14
14
 
15
- cattr_accessor :expositions, :settings
15
+ # path tree to resolve the requested model
16
+ @@path_tree = {}
17
+
18
+ cattr_accessor :expositions, :settings, :path_tree
16
19
 
17
20
  class ConfigError < StandardError
18
21
  end
@@ -23,6 +26,7 @@ module Toast
23
26
  # clean up
24
27
  Toast.expositions.clear
25
28
  Toast.settings = nil
29
+ Toast.path_tree = {}
26
30
 
27
31
  settings = ''
28
32
  # read global settings
@@ -20,6 +20,21 @@ class Toast::ConfigDSL::Base
20
20
  raise_config_error 'Block expected.'
21
21
  end
22
22
 
23
+ # register base path with 'under' prefix
24
+ to_path_tree = lambda do |path|
25
+ if path.empty?
26
+ { model_class.to_s.underscore.pluralize => model_class }
27
+ else
28
+ { path.first => to_path_tree.call(path[1..-1]) }
29
+ end
30
+ end
31
+
32
+ path = under.split('/').delete_if(&:blank?)
33
+ Toast.path_tree.deep_merge!(to_path_tree.call(path)) do |key,v1,v2|
34
+ raise_config_error "multiple definitions of endpoint URI segment `.../#{key}/...'"
35
+ end
36
+
37
+ # base config object
23
38
  config_data = OpenStruct.new
24
39
 
25
40
  config_data.instance_eval do
@@ -27,8 +42,8 @@ class Toast::ConfigDSL::Base
27
42
 
28
43
  self.model_class = model_class
29
44
  self.media_type = as
30
- self.url_path_prefix = under.split('/').delete_if(&:blank?)
31
-
45
+ self.prefix_path = path
46
+
32
47
  # defaults
33
48
  self.readables = []
34
49
  self.writables = []
@@ -34,32 +34,20 @@ class Toast::RackApp
34
34
  end
35
35
 
36
36
  path = request.path_parameters[:toast_path].split('/')
37
+
38
+ # look up requested model
39
+ model_class = resolve_model(path, Toast.path_tree)
37
40
 
38
- # strip path prefixes
39
- # get model class
40
- begin
41
- if path.first.singularize == path.first # not in plural form?
42
- raise NameError
43
- end
44
- model_class = path.first.singularize.classify.constantize
45
- raise NameError unless is_active_record?(model_class)
46
- rescue NameError
41
+ if model_class.nil?
47
42
  return response :not_found,
48
- msg: "resource at /#{path.join('/')} not found"
43
+ msg: "no API configuration found for endpoint /#{path.join('/')}"
49
44
  end
50
45
 
51
46
  # select base configuration
47
+ base_config = get_config(model_class)
52
48
 
53
- ## This is unused. Do we need it at all?
54
- # preferred_type = Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT']).prefered || "application/json"
55
-
56
- # the base_config is the configuration for the model corresponding to the first part of the URI path
57
- begin
58
- base_config = get_config(model_class)
59
- rescue Toast::Errors::ConfigNotFound => error
60
- return response :not_found,
61
- msg: "no API configuration found for model `#{model_class.name}'"
62
- end
49
+ # remove path prefix
50
+ path = path[(base_config.prefix_path.length)..-1]
63
51
 
64
52
  toast_request =
65
53
  case path.length
@@ -130,4 +118,17 @@ class Toast::RackApp
130
118
  toast_request.respond
131
119
 
132
120
  end
121
+
122
+ private
123
+ # gets the model class from the
124
+ # it's similar to Hash#dig but stops at a non string
125
+ def resolve_model path, path_tree
126
+ if path_tree[ path.first ].is_a?(Hash)
127
+ # dig deeper
128
+ resolve_model(path[1..-1], path_tree[ path.first ])
129
+ else
130
+ path_tree[ path.first ]
131
+ end
132
+ end
133
133
  end
134
+
@@ -23,7 +23,8 @@ module Toast::RequestHelpers
23
23
  result[attr.to_s] = record.send(attr) if attr_selected?(attr)
24
24
  end
25
25
 
26
- model_uri = "#{@base_uri}/#{record.class.name.underscore.pluralize}"
26
+ model_uri = [@base_uri, config.prefix_path, record.class.name.underscore.pluralize].delete_if(&:blank?).join('/')
27
+
27
28
  result['self'] = "#{model_uri}/#{record.id}" if attr_selected?('self')
28
29
 
29
30
  # add associations, collections and singles
data/lib/toast/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Toast
2
- VERSION = '1.0.2'
3
- end
2
+ VERSION = '1.0.3'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toast
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - robokopp (Robert Annies)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-12 00:00:00.000000000 Z
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.9
27
- - !ruby/object:Gem::Dependency
28
- name: rack-accept-media-types
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '0.9'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: '0.9'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: link_header
43
29
  requirement: !ruby/object:Gem::Requirement