yaks-sinatra 0.10.0 → 0.11.0

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: 5b8d0cab9f9d94ee17327cd7820ba568a47c8b0f
4
- data.tar.gz: 77136f4984461ec883d62667579fd80a75e43b1d
3
+ metadata.gz: 4dc7bd3869510736d87c46e96be148a9ca257968
4
+ data.tar.gz: ea5bf1fd71db1dbfaa0332b4154087b0b4e606ac
5
5
  SHA512:
6
- metadata.gz: 8e25bd3e7beca062da86c6422c66fe4fd128b6542e94063050304b4a9fe086747735c12ca78b8af7bead0c280944df630e774c4a78da64d3c90f777b02324714
7
- data.tar.gz: 6dea67ec3c24dd0a3bbcf019d8cb6f1935419de50d1ecb80511ff59dca213edbad7fc192dc6f448d0a2fad90ef973dcbb36c4d5f0a680b76f564433a05dfcb09
6
+ metadata.gz: 752a4c4f33aaf9117c1fd08f6724584f74100fd09e0d1fe1ad0a8507ed65cfb532f50e32e963ad6cf13107df56be4c4813bcc4cd8b7dc1050f9d9c8db3fd1869
7
+ data.tar.gz: 1fce79637a67ac4d2006c663bbcc2b7b2ed9e4547bca7052fd4edf5490320ccd83c3c55880728a4238675f3ef130f469f312e1d6412034d26ff20e0c9739451e
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -r spec_helper
data/README.md CHANGED
@@ -3,7 +3,10 @@ Provide basic integration for using Yaks in sinatra. It gives you a top level `c
3
3
  This will register all media types known to Yaks, make sure the right one is picked based on the `Accept` header, and it will put the correct `Content-Type` header on the response.
4
4
 
5
5
 
6
- ``` ruby
6
+ In a classic Sinatra app, you use it like so:
7
+
8
+ ```ruby
9
+ require 'sinatra'
7
10
  require 'yaks-sinatra'
8
11
 
9
12
  configure_yaks do
@@ -28,3 +31,20 @@ get '/posts' do
28
31
  yaks Post.all
29
32
  end
30
33
  ```
34
+
35
+ In a modular Sinatra app, you need to register the extension explicitly:
36
+
37
+ ```ruby
38
+ require 'sinatra/base'
39
+ require 'yaks-sinatra'
40
+
41
+ class MyApp < Sinatra::Base
42
+ register Yaks::Sinatra
43
+
44
+ configure_yaks do
45
+ # ...
46
+ end
47
+
48
+ # ...
49
+ end
50
+ ```
data/Rakefile CHANGED
@@ -1,3 +1,3 @@
1
- load '../rakelib/shared.rake'
1
+ load '../shared/rake_tasks.rb'
2
2
 
3
3
  gem_tasks(:"yaks-sinatra")
data/lib/yaks-sinatra.rb CHANGED
@@ -1,14 +1,22 @@
1
1
  require 'sinatra/base'
2
2
  require 'yaks'
3
3
 
4
- module Sinatra
5
- module Yaks
4
+ module Yaks
5
+ module Sinatra
6
6
  class << self
7
7
  attr_accessor :yaks_config
8
8
  end
9
9
 
10
+ module Helpers
11
+ def yaks(object, opts = {})
12
+ runner = Yaks::Sinatra.yaks_config.runner(object, {env: env}.merge(opts))
13
+ content_type runner.format_name
14
+ runner.call
15
+ end
16
+ end
17
+
10
18
  def configure_yaks(&block)
11
- Yaks.yaks_config = ::Yaks.new(&block)
19
+ Yaks::Sinatra.yaks_config = ::Yaks.new(&block)
12
20
 
13
21
  configure do
14
22
  ::Yaks::Format.all.each do |format|
@@ -16,16 +24,20 @@ module Sinatra
16
24
  end
17
25
  end
18
26
  end
19
- end
20
27
 
21
- module YaksHelpers
22
- def yaks(object, opts = {})
23
- runner = Yaks.yaks_config.runner(object, {env: env}.merge(opts))
24
- content_type runner.format_name
25
- runner.call
28
+ def self.registered(app)
29
+ app.helpers(Yaks::Sinatra::Helpers)
30
+
31
+ ::Yaks::Format.all.each do |format|
32
+ app.settings.add_charset << format.media_type
33
+ end
34
+
35
+ # app.configure_yaks
26
36
  end
27
37
  end
38
+ end
28
39
 
29
- register Yaks
30
- helpers YaksHelpers
40
+ # For classic apps
41
+ module Sinatra
42
+ register Yaks::Sinatra
31
43
  end
@@ -0,0 +1,16 @@
1
+ require 'sinatra'
2
+ require 'yaks-sinatra'
3
+
4
+ Root = Class.new(Struct.new(:name))
5
+
6
+ class RootMapper < Yaks::Mapper
7
+ link :self, '/'
8
+ end
9
+
10
+ set :default_charset, 'utf-8'
11
+
12
+ configure_yaks
13
+
14
+ get '/' do
15
+ yaks Root.new('root'), mapper: RootMapper
16
+ end
@@ -0,0 +1,25 @@
1
+ require 'integration_helper'
2
+ require_relative 'classic_app.rb'
3
+
4
+ RSpec.describe 'Sinatra Classic app integration', type: :integration do
5
+ include Yaks::Sinatra::Test::ClassicApp::Helpers
6
+
7
+ ::Yaks::Format.all.each do |format|
8
+ context "For #{format.format_name}" do
9
+ it "returns 200" do
10
+ make_req(format.media_type)
11
+ expect(last_response).to be_ok
12
+ end
13
+
14
+ it "respects the Accept header" do
15
+ make_req(format.media_type)
16
+ expect(last_content_type.type).to eq(format.media_type)
17
+ end
18
+
19
+ it "returns an explicit charset" do
20
+ make_req(format.media_type)
21
+ expect(last_content_type.charset).to eq('utf-8')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require 'integration_helper'
2
+
3
+ RSpec.describe 'Sinatra Modular app integration', type: :integration do
4
+ include Yaks::Sinatra::Test::ModularApp::Helpers
5
+
6
+ ::Yaks::Format.all.each do |format|
7
+ context "For #{format.format_name}" do
8
+ it "returns 200" do
9
+ make_req(format.media_type)
10
+ expect(last_response).to be_ok
11
+ end
12
+
13
+ it "respects the Accept header" do
14
+ make_req(format.media_type)
15
+ expect(last_content_type.type).to eq(format.media_type)
16
+ end
17
+
18
+ it "returns an explicit charset" do
19
+ make_req(format.media_type)
20
+ expect(last_content_type.charset).to eq('utf-8')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'yaks-sinatra'
5
+ require 'rack/test'
6
+
7
+ ENV['RACK_ENV'] = 'test'
8
+
9
+ class MediaType
10
+ QUOTED_STRING = '[\w!#$%&\'()*+,-./:;<=>?@\[\]\\\\^`{|}~ \t\r\n]+'
11
+ TOKEN = '[\w!\#$%&\'*+-.^`|~]+'
12
+ TYPE_AND_SUBTYPE = "(?<maintype>#{TOKEN})/(?<subtype>#{TOKEN})"
13
+ PARAMETER_VALUE = "(?:(?<value>#{TOKEN})|\"(?<q_value>#{QUOTED_STRING})\")"
14
+ PARAMETER_RE = /\s*;\s*(?<name>#{TOKEN})\s*=\s*#{PARAMETER_VALUE}/m
15
+ MEDIA_TYPE_RE = /^#{TYPE_AND_SUBTYPE}/
16
+
17
+ def initialize(header_string)
18
+ @header_string = header_string
19
+ end
20
+
21
+ def type_and_rest
22
+ @type_and_rest ||= begin
23
+ type_match = MEDIA_TYPE_RE.match(@header_string)
24
+ return ["", ""] if type_match.nil?
25
+ @main_type = type_match['maintype']
26
+ @sub_type = type_match['subtype']
27
+ type = type_match.to_s
28
+ rest = @header_string[type.length..-1]
29
+ [type, rest]
30
+ end
31
+ end
32
+
33
+ def parameters
34
+ @parameters ||= begin
35
+ _, rest = type_and_rest
36
+ parameters = {}
37
+ rest.scan(PARAMETER_RE) do |name, value, q_value|
38
+ parameters[name] = value || q_value
39
+ end
40
+ parameters
41
+ end
42
+ end
43
+
44
+ def type
45
+ @type ||= type_and_rest[0]
46
+ end
47
+
48
+ def main_type
49
+ @main_type || type_and_rest && @main_type
50
+ end
51
+
52
+ def sub_type
53
+ @sub_type || type_and_rest && @sub_type
54
+ end
55
+
56
+ def charset
57
+ parameters['charset']
58
+ end
59
+ end
60
+
61
+ module Yaks
62
+ module Sinatra
63
+ module Test
64
+ module Helpers
65
+ def make_req(mime_type = 'application/hal+json')
66
+ header 'Accept', mime_type
67
+ get '/'
68
+ end
69
+
70
+ def last_content_type
71
+ MediaType.new(last_response.content_type)
72
+ end
73
+ end
74
+
75
+ class ModularApp < ::Sinatra::Base
76
+ module Helpers
77
+ include Yaks::Sinatra::Test::Helpers
78
+
79
+ def app
80
+ Yaks::Sinatra::Test::ModularApp
81
+ end
82
+ end
83
+
84
+ Root = Class.new(Struct.new(:name))
85
+
86
+ class RootMapper < Yaks::Mapper
87
+ link :self, '/'
88
+ end
89
+
90
+ register Yaks::Sinatra
91
+
92
+ set :default_charset, 'utf-8'
93
+
94
+ configure_yaks
95
+
96
+ get '/' do
97
+ yaks Root.new('root'), mapper: RootMapper
98
+ end
99
+ end
100
+
101
+ module ClassicApp
102
+ module Helpers
103
+ include Yaks::Sinatra::Test::Helpers
104
+
105
+ def app
106
+ ::Sinatra::Application
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ RSpec.configure do |config|
115
+ config.include Rack::Test::Methods, type: :integration
116
+ end
@@ -0,0 +1 @@
1
+ require_relative '../../shared/rspec_config'
data/yaks-sinatra.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
22
22
 
23
23
  gem.add_runtime_dependency 'yaks', Yaks::VERSION
24
24
  gem.add_runtime_dependency 'sinatra', '~> 1.4'
25
+ gem.add_development_dependency 'rack-test', '~> 0.6'
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaks-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arne Brasseur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaks
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.10.0
19
+ version: 0.11.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.10.0
26
+ version: 0.11.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sinatra
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
41
55
  description: Sinatra integration for Yaks
42
56
  email:
43
57
  - arne@arnebrasseur.net
@@ -46,9 +60,15 @@ extensions: []
46
60
  extra_rdoc_files:
47
61
  - README.md
48
62
  files:
63
+ - ".rspec"
49
64
  - README.md
50
65
  - Rakefile
51
66
  - lib/yaks-sinatra.rb
67
+ - spec/integration/classic_app.rb
68
+ - spec/integration/classic_spec.rb
69
+ - spec/integration/modular_spec.rb
70
+ - spec/integration_helper.rb
71
+ - spec/spec_helper.rb
52
72
  - yaks-sinatra.gemspec
53
73
  homepage: https://github.com/plexus/yaks
54
74
  licenses:
@@ -70,8 +90,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
90
  version: '0'
71
91
  requirements: []
72
92
  rubyforge_project:
73
- rubygems_version: 2.4.5
93
+ rubygems_version: 2.2.3
74
94
  signing_key:
75
95
  specification_version: 4
76
96
  summary: Sinatra integration for Yaks
77
- test_files: []
97
+ test_files:
98
+ - spec/integration/classic_app.rb
99
+ - spec/integration/classic_spec.rb
100
+ - spec/integration/modular_spec.rb
101
+ - spec/integration_helper.rb
102
+ - spec/spec_helper.rb