Skip to content

Commit 085cf16

Browse files
authored
Removed registrations, defaults, added suffix management (#77)
* Removed registrations, defaults, added suffix management Fixes #72
1 parent aa1b685 commit 085cf16

23 files changed

+67
-704
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 2.0.0
2+
- Removed ability to set default suffix. All suffixes now default to `+json`.
3+
- Suffixes are now set for a given view and version instead of as a block.
4+
- Added `suffix :yoursuffix` command to override the default `:json` suffix.
5+
- Removed defaults block.
6+
- Removed registrations block.
7+
18
# 1.0.0
29
- Added the ability to do inline tests when defining validations using `assert_pass '<json>'` and `assert_fail '<json>'`.
310
- `media_type` has been replaced with `use_name`.

Gemfile.lock

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
PATH
22
remote: .
33
specs:
4-
media_types (1.0.0)
4+
media_types (2.0.0)
55

66
GEM
77
remote: https://rubygems.org/
88
specs:
9-
actionpack (5.2.4)
10-
actionview (= 5.2.4)
11-
activesupport (= 5.2.4)
12-
rack (~> 2.0)
9+
actionpack (5.2.4.1)
10+
actionview (= 5.2.4.1)
11+
activesupport (= 5.2.4.1)
12+
rack (~> 2.0, >= 2.0.8)
1313
rack-test (>= 0.6.3)
1414
rails-dom-testing (~> 2.0)
1515
rails-html-sanitizer (~> 1.0, >= 1.0.2)
16-
actionview (5.2.4)
17-
activesupport (= 5.2.4)
16+
actionview (5.2.4.1)
17+
activesupport (= 5.2.4.1)
1818
builder (~> 3.1)
1919
erubi (~> 1.4)
2020
rails-dom-testing (~> 2.0)
2121
rails-html-sanitizer (~> 1.0, >= 1.0.3)
22-
activesupport (5.2.4)
22+
activesupport (5.2.4.1)
2323
concurrent-ruby (~> 1.0, >= 1.0.2)
2424
i18n (>= 0.7, < 2)
2525
minitest (~> 5.1)
@@ -28,36 +28,30 @@ GEM
2828
public_suffix (>= 2.0.2, < 5.0)
2929
ansi (1.5.0)
3030
awesome_print (1.8.0)
31-
builder (3.2.3)
31+
builder (3.2.4)
3232
concurrent-ruby (1.1.5)
33-
crass (1.0.5)
33+
crass (1.0.6)
3434
docile (1.3.2)
3535
domain_name (0.5.20190701)
3636
unf (>= 0.0.5, < 1.0.0)
3737
erubi (1.9.0)
38-
ffi (1.11.1)
39-
ffi (1.11.1-x64-mingw32)
40-
ffi-compiler (1.0.1)
41-
ffi (>= 1.0.0)
42-
rake
43-
http (4.2.0)
38+
http (4.1.1)
4439
addressable (~> 2.3)
4540
http-cookie (~> 1.0)
4641
http-form_data (~> 2.0)
47-
http-parser (~> 1.2.0)
42+
http_parser.rb (~> 0.6.0)
4843
http-cookie (1.0.3)
4944
domain_name (~> 0.5)
5045
http-form_data (2.1.1)
51-
http-parser (1.2.1)
52-
ffi-compiler (>= 1.0, < 2.0)
53-
i18n (1.7.0)
46+
http_parser.rb (0.6.0)
47+
i18n (1.8.2)
5448
concurrent-ruby (~> 1.0)
55-
json (2.2.0)
49+
json (2.3.0)
5650
loofah (2.4.0)
5751
crass (~> 1.0.2)
5852
nokogiri (>= 1.5.9)
5953
mini_portile2 (2.4.0)
60-
minitest (5.13.0)
54+
minitest (5.14.0)
6155
minitest-ci (3.4.0)
6256
minitest (>= 5.0.6)
6357
minitest-reporters (1.4.2)
@@ -67,11 +61,9 @@ GEM
6761
ruby-progressbar
6862
nokogiri (1.10.7)
6963
mini_portile2 (~> 2.4.0)
70-
nokogiri (1.10.7-x64-mingw32)
71-
mini_portile2 (~> 2.4.0)
72-
oj (3.10.0)
64+
oj (3.10.1)
7365
public_suffix (4.0.1)
74-
rack (2.0.7)
66+
rack (2.1.1)
7567
rack-test (1.1.0)
7668
rack (>= 1.0, < 3)
7769
rails-dom-testing (2.0.3)
@@ -87,20 +79,19 @@ GEM
8779
simplecov-html (~> 0.10.0)
8880
simplecov-html (0.10.2)
8981
thread_safe (0.3.6)
90-
tzinfo (1.2.5)
82+
tzinfo (1.2.6)
9183
thread_safe (~> 0.1)
9284
unf (0.1.4)
9385
unf_ext
9486
unf_ext (0.0.7.6)
9587

9688
PLATFORMS
9789
ruby
98-
x64-mingw32
9990

10091
DEPENDENCIES
10192
actionpack
10293
awesome_print
103-
bundler (~> 2)
94+
bundler
10495
http
10596
media_types!
10697
minitest (~> 5.0)
@@ -111,4 +102,4 @@ DEPENDENCIES
111102
simplecov
112103

113104
BUNDLED WITH
114-
2.0.1
105+
1.17.3

README.md

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Venue
6464
'mydomain'
6565
end
6666

67-
media_type 'venue', defaults: { suffix: :json }
67+
media_type 'venue'
6868

6969
validations do
7070
version 2 do
@@ -103,17 +103,6 @@ class Venue
103103
end
104104
end
105105
end
106-
107-
registrations :venue_json do
108-
view 'create', :create_venue
109-
view 'index', :venue_urls
110-
view 'collection', :venue_collection
111-
112-
versions [1,2]
113-
114-
suffix :json
115-
suffix :xml
116-
end
117106
end
118107
```
119108

@@ -346,9 +335,6 @@ Venue.mime_type.identifier
346335
Venue.mime_type.version(1).identifier
347336
# => "application/vnd.mydomain.venue.v1+json"
348337
349-
Venue.mime_type.version(1).suffix(:xml).identifier
350-
# => "application/vnd.mydomain.venue.v1+xml"
351-
352338
Venue.mime_type.to_s(0.2)
353339
# => "application/vnd.mydomain.venue.v2+json; q=0.2"
354340
@@ -359,43 +345,6 @@ Venue.mime_type.view('active').identifier
359345
# => "application/vnd.mydomain.venue.v2.active+json"
360346
```
361347

362-
## Integrations
363-
The integrations are not loaded by default, so you need to require them:
364-
```ruby
365-
# For Rails / ActionPack
366-
require 'media_types/integrations/actionpack'
367-
368-
# For HTTP.rb
369-
require 'media_types/integrations/http'
370-
```
371-
372-
Define a `registrations` block on your media type, indicating the symbol for the base type (`registrations :symbol do`) and inside use the registrations dsl to define which media types to register. `versions array_of_numbers` determines which versions, `suffix name` adds a suffix, `type_alias name` adds an alias and `view name, symbol` adds a view.
373-
374-
```Ruby
375-
Venue.register
376-
```
377-
378-
### Rails
379-
Load the `actionpack` integration and call `.register` on all the media types you want to be available in Rails. You can do this in the `mime_types` initializer, or anywhere before your controllers are instantiated. Yes, the symbol (by default `<type>_v<version>_<suffix>`) can now be used in your `format` blocks, or as extension in the url.
380-
381-
Rails only has a default serializer for `application/json`, and content with your `+json` media types (or different once) will not be deserialized by default. A way to overcome this is to set the JSON parameter parser for all new symbols. `.register` gives you back an array of `Registerable` objects that responds to `#to_sym` to get that symbol.
382-
383-
```ruby
384-
symbols = Venue.register.map(&:to_sym)
385-
386-
original_parsers = ActionDispatch::Request.parameter_parsers
387-
new_parser = original_parsers[Mime[:json].symbol]
388-
new_parsers = original_parsers.merge(Hash[*symbols.map { |s| [s, new_parser] }])
389-
ActionDispatch::Request.parameter_parsers = new_parsers
390-
```
391-
392-
If you want to validate the content-type and not have your errors be `Rack::Error` but be handled by your controllers, leave this out and add a `before_action` to your controller that deserializes + validates for you.
393-
394-
### HTTP.rb
395-
Load the `http` integration and call `.register` on all media types you want to be able to serialize and deserialize. The media type validations will run both before serialization and after deserialization.
396-
397-
Currently uses `oj` under the hood and this can not be changed.
398-
399348
## API
400349

401350
A defined schema has the following functions available:

lib/media_types/.dsl.rb.swp

-20 KB
Binary file not shown.

lib/media_types/constructable.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ def view(view = NO_ARG)
2828
with(view: view)
2929
end
3030

31-
def suffix(suffix = NO_ARG)
32-
return opts[:suffix] if suffix == NO_ARG
33-
with(suffix: suffix)
34-
end
35-
3631
def collection
3732
view(COLLECTION_VIEW)
3833
end
@@ -74,21 +69,26 @@ def split(pattern = nil, *limit)
7469
end
7570

7671
def as_key
77-
[type, view, version, suffix]
72+
[type, view, version]
7873
end
7974

8075
def hash
8176
as_key.hash
8277
end
8378

79+
def suffix
80+
schema = schema_for(self)
81+
schema.type_attributes.fetch(:suffix, 'json')
82+
end
83+
8484
def to_str(qualifier = nil)
8585
qualified(
8686
qualifier,
8787
__getobj__.media_type_name_for.call(
8888
type: opts[:type],
8989
view: opts[:view],
9090
version: opts[:version],
91-
suffix: opts[:suffix],
91+
suffix: suffix
9292
)
9393
)
9494
end

lib/media_types/defaults.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/media_types/dsl.rb

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'media_types/constructable'
4-
require 'media_types/defaults'
5-
require 'media_types/registrar'
64
require 'media_types/validations'
75

86
module MediaTypes
@@ -65,10 +63,7 @@ def view(v)
6563
def version(v)
6664
to_constructable.version(v)
6765
end
68-
def suffix(s)
69-
to_constructable.suffix(s)
70-
end
71-
66+
7267
def identifier_format
7368
self.media_type_name_for = Proc.new do |type:, view:, version:, suffix:|
7469
yield(type: type, view: view, version: version, suffix: suffix)
@@ -81,14 +76,18 @@ def identifier
8176

8277
def available_validations
8378
self.media_type_combinations.map do |a|
84-
_, view, version, suffix = a
85-
view(view).version(version).suffix(suffix)
79+
_, view, version = a
80+
view(view).version(version)
8681
end
8782
end
8883

84+
def schema_for(constructable)
85+
validations.find(constructable)
86+
end
87+
8988
private
9089

91-
def use_name(name, defaults: {})
90+
def use_name(name)
9291
if self.media_type_name_for.nil?
9392
self.media_type_name_for = Proc.new do |type:, view:, version:, suffix:|
9493
resolved_org = nil
@@ -109,21 +108,7 @@ def use_name(name, defaults: {})
109108
result
110109
end
111110
end
112-
self.media_type_constructable = Constructable.new(self, type: name).suffix(defaults.fetch(:suffix) { nil })
113-
end
114-
115-
def defaults(&block)
116-
return media_type_constructable unless block_given?
117-
self.media_type_constructable = Defaults.new(to_constructable, &block).to_constructable
118-
119-
self
120-
end
121-
122-
def registrations(symbol = nil, &block)
123-
return media_type_registrar unless block_given?
124-
self.media_type_registrar = Registrar.new(self, symbol: symbol, &block)
125-
126-
self
111+
self.media_type_constructable = Constructable.new(self, type: name)
127112
end
128113

129114
def validations(&block)

lib/media_types/integrations.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)