Skip to content

Commit

Permalink
Removed registrations, defaults, added suffix management (#77)
Browse files Browse the repository at this point in the history
* Removed registrations, defaults, added suffix management

Fixes #72
  • Loading branch information
thexa4 authored Feb 26, 2020
1 parent aa1b685 commit 085cf16
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 704 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 2.0.0
- Removed ability to set default suffix. All suffixes now default to `+json`.
- Suffixes are now set for a given view and version instead of as a block.
- Added `suffix :yoursuffix` command to override the default `:json` suffix.
- Removed defaults block.
- Removed registrations block.

# 1.0.0
- Added the ability to do inline tests when defining validations using `assert_pass '<json>'` and `assert_fail '<json>'`.
- `media_type` has been replaced with `use_name`.
Expand Down
51 changes: 21 additions & 30 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
PATH
remote: .
specs:
media_types (1.0.0)
media_types (2.0.0)

GEM
remote: https://rubygems.org/
specs:
actionpack (5.2.4)
actionview (= 5.2.4)
activesupport (= 5.2.4)
rack (~> 2.0)
actionpack (5.2.4.1)
actionview (= 5.2.4.1)
activesupport (= 5.2.4.1)
rack (~> 2.0, >= 2.0.8)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.4)
activesupport (= 5.2.4)
actionview (5.2.4.1)
activesupport (= 5.2.4.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activesupport (5.2.4)
activesupport (5.2.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -28,36 +28,30 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ansi (1.5.0)
awesome_print (1.8.0)
builder (3.2.3)
builder (3.2.4)
concurrent-ruby (1.1.5)
crass (1.0.5)
crass (1.0.6)
docile (1.3.2)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
erubi (1.9.0)
ffi (1.11.1)
ffi (1.11.1-x64-mingw32)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
http (4.2.0)
http (4.1.1)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 2.0)
http-parser (~> 1.2.0)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (2.1.1)
http-parser (1.2.1)
ffi-compiler (>= 1.0, < 2.0)
i18n (1.7.0)
http_parser.rb (0.6.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
json (2.2.0)
json (2.3.0)
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mini_portile2 (2.4.0)
minitest (5.13.0)
minitest (5.14.0)
minitest-ci (3.4.0)
minitest (>= 5.0.6)
minitest-reporters (1.4.2)
Expand All @@ -67,11 +61,9 @@ GEM
ruby-progressbar
nokogiri (1.10.7)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.7-x64-mingw32)
mini_portile2 (~> 2.4.0)
oj (3.10.0)
oj (3.10.1)
public_suffix (4.0.1)
rack (2.0.7)
rack (2.1.1)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
Expand All @@ -87,20 +79,19 @@ GEM
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
thread_safe (0.3.6)
tzinfo (1.2.5)
tzinfo (1.2.6)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.6)

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
actionpack
awesome_print
bundler (~> 2)
bundler
http
media_types!
minitest (~> 5.0)
Expand All @@ -111,4 +102,4 @@ DEPENDENCIES
simplecov

BUNDLED WITH
2.0.1
1.17.3
53 changes: 1 addition & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Venue
'mydomain'
end

media_type 'venue', defaults: { suffix: :json }
media_type 'venue'

validations do
version 2 do
Expand Down Expand Up @@ -103,17 +103,6 @@ class Venue
end
end
end

registrations :venue_json do
view 'create', :create_venue
view 'index', :venue_urls
view 'collection', :venue_collection

versions [1,2]

suffix :json
suffix :xml
end
end
```

Expand Down Expand Up @@ -346,9 +335,6 @@ Venue.mime_type.identifier
Venue.mime_type.version(1).identifier
# => "application/vnd.mydomain.venue.v1+json"
Venue.mime_type.version(1).suffix(:xml).identifier
# => "application/vnd.mydomain.venue.v1+xml"
Venue.mime_type.to_s(0.2)
# => "application/vnd.mydomain.venue.v2+json; q=0.2"
Expand All @@ -359,43 +345,6 @@ Venue.mime_type.view('active').identifier
# => "application/vnd.mydomain.venue.v2.active+json"
```

## Integrations
The integrations are not loaded by default, so you need to require them:
```ruby
# For Rails / ActionPack
require 'media_types/integrations/actionpack'
# For HTTP.rb
require 'media_types/integrations/http'
```

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.

```Ruby
Venue.register
```

### Rails
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.

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.

```ruby
symbols = Venue.register.map(&:to_sym)
original_parsers = ActionDispatch::Request.parameter_parsers
new_parser = original_parsers[Mime[:json].symbol]
new_parsers = original_parsers.merge(Hash[*symbols.map { |s| [s, new_parser] }])
ActionDispatch::Request.parameter_parsers = new_parsers
```

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.

### HTTP.rb
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.

Currently uses `oj` under the hood and this can not be changed.

## API

A defined schema has the following functions available:
Expand Down
Binary file removed lib/media_types/.dsl.rb.swp
Binary file not shown.
14 changes: 7 additions & 7 deletions lib/media_types/constructable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def view(view = NO_ARG)
with(view: view)
end

def suffix(suffix = NO_ARG)
return opts[:suffix] if suffix == NO_ARG
with(suffix: suffix)
end

def collection
view(COLLECTION_VIEW)
end
Expand Down Expand Up @@ -74,21 +69,26 @@ def split(pattern = nil, *limit)
end

def as_key
[type, view, version, suffix]
[type, view, version]
end

def hash
as_key.hash
end

def suffix
schema = schema_for(self)
schema.type_attributes.fetch(:suffix, 'json')
end

def to_str(qualifier = nil)
qualified(
qualifier,
__getobj__.media_type_name_for.call(
type: opts[:type],
view: opts[:view],
version: opts[:version],
suffix: opts[:suffix],
suffix: suffix
)
)
end
Expand Down
31 changes: 0 additions & 31 deletions lib/media_types/defaults.rb

This file was deleted.

33 changes: 9 additions & 24 deletions lib/media_types/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require 'media_types/constructable'
require 'media_types/defaults'
require 'media_types/registrar'
require 'media_types/validations'

module MediaTypes
Expand Down Expand Up @@ -65,10 +63,7 @@ def view(v)
def version(v)
to_constructable.version(v)
end
def suffix(s)
to_constructable.suffix(s)
end


def identifier_format
self.media_type_name_for = Proc.new do |type:, view:, version:, suffix:|
yield(type: type, view: view, version: version, suffix: suffix)
Expand All @@ -81,14 +76,18 @@ def identifier

def available_validations
self.media_type_combinations.map do |a|
_, view, version, suffix = a
view(view).version(version).suffix(suffix)
_, view, version = a
view(view).version(version)
end
end

def schema_for(constructable)
validations.find(constructable)
end

private

def use_name(name, defaults: {})
def use_name(name)
if self.media_type_name_for.nil?
self.media_type_name_for = Proc.new do |type:, view:, version:, suffix:|
resolved_org = nil
Expand All @@ -109,21 +108,7 @@ def use_name(name, defaults: {})
result
end
end
self.media_type_constructable = Constructable.new(self, type: name).suffix(defaults.fetch(:suffix) { nil })
end

def defaults(&block)
return media_type_constructable unless block_given?
self.media_type_constructable = Defaults.new(to_constructable, &block).to_constructable

self
end

def registrations(symbol = nil, &block)
return media_type_registrar unless block_given?
self.media_type_registrar = Registrar.new(self, symbol: symbol, &block)

self
self.media_type_constructable = Constructable.new(self, type: name)
end

def validations(&block)
Expand Down
32 changes: 0 additions & 32 deletions lib/media_types/integrations.rb

This file was deleted.

Loading

0 comments on commit 085cf16

Please sign in to comment.