yaks-html 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 +4 -4
- data/README.md +41 -0
- data/Rakefile +1 -1
- data/lib/yaks-html/rspec.rb +73 -0
- data/lib/yaks/format/html.rb +16 -6
- data/spec/smoke_test_spec.rb +32 -0
- data/spec/spec_helper.rb +5 -6
- data/spec/support/test_app.rb +44 -0
- data/yaks-html.gemspec +5 -0
- metadata +66 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af536b85c5b8ec153e5c803ddb0051a84bb0b284
|
4
|
+
data.tar.gz: e9de84047d31aecab97ed462262e972d10da57b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 610b56a2df54cf21201f8c6627a05a9ccecd9439515512f85146753267ddf2003d91712f304b3c080bcdcfea80abe4e51058a9c8aaec1ae535983bcc02547cb3
|
7
|
+
data.tar.gz: fdad369fdf7116bf6da5551643c7d97f1fb08e1f42b0bb181d716021c23ad76311a4c71f8939cd72546d6ceb23000d6aa7e6f2fc27be947c63b390a215062d65
|
data/README.md
CHANGED
@@ -1,3 +1,44 @@
|
|
1
1
|
A HTML output format for Yaks.
|
2
2
|
|
3
3
|
Browse your hypermedia API like a good old fashioned web site.
|
4
|
+
|
5
|
+
You can see an example output at
|
6
|
+
[https://myticketsireland.ticketsolve.com/api/](https://myticketsireland.ticketsolve.com/api/)
|
7
|
+
(visit the [front page](https://myticketsireland.ticketsolve.com/)
|
8
|
+
first to make sure you have some necessary cookies).
|
9
|
+
|
10
|
+
For APIs that make good use of links and forms this provides a great
|
11
|
+
help during development. It also makes it possible to write
|
12
|
+
integration tests against the HTML output using Capybara.
|
13
|
+
|
14
|
+
We currently provide a small DSL and RSpec integration to make this easy. For example:
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'yaks-html/rspec'
|
18
|
+
|
19
|
+
RSpec.describe Yaks::Format::HTML, type: :yaks_integration do
|
20
|
+
before do
|
21
|
+
Capybara.app = YourRackApp
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:rel_prefix) { 'http://myapi.example.com/rel/' }
|
25
|
+
|
26
|
+
it 'should allow browsing the api' do
|
27
|
+
visit '/'
|
28
|
+
|
29
|
+
click_rel('friends') # => clicks http://myapi.example.com/rel/friends
|
30
|
+
|
31
|
+
expect(current_path).to eql '/friends'
|
32
|
+
|
33
|
+
expect(page).to have_content 'Matt'
|
34
|
+
|
35
|
+
submit_form(:poke) do
|
36
|
+
fill_in :message, with: 'Would you fancy some tea?'
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(current_path).to eql '/poke/Matt'
|
40
|
+
|
41
|
+
expect(page).to have_content 'You poked Matt: Would you fancy some tea?'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
data/Rakefile
CHANGED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'yaks-html'
|
2
|
+
require 'capybara/rspec'
|
3
|
+
|
4
|
+
module YaksHTML
|
5
|
+
# This is a bit of a hack. The only way to add custom rack env
|
6
|
+
# entries to Capybara/Rack::Test is through the driver options (see
|
7
|
+
# Capybara.register_driver below). However this is only executed
|
8
|
+
# once, so throughout the whole test suite the same instance of the
|
9
|
+
# hash we pass will be used. To make it a bit easier to set/reset
|
10
|
+
# this we add this layer of indirection. Now what we pass the driver
|
11
|
+
# looks like a hash, but we can point it to a new hash with
|
12
|
+
# RACK_ENV.__setobj__({})
|
13
|
+
|
14
|
+
RACK_ENV = SimpleDelegator.new({})
|
15
|
+
|
16
|
+
module CapybaraDSL
|
17
|
+
def self.included(base)
|
18
|
+
base.class_eval do
|
19
|
+
let(:rel_prefix) { '' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def click_rel(rel)
|
24
|
+
rel = [rel_prefix, rel].join unless rel.is_a? Symbol
|
25
|
+
find("a[rel=\"#{rel}\"]").click
|
26
|
+
end
|
27
|
+
|
28
|
+
def click_first_rel(rel)
|
29
|
+
rel = [rel_prefix, rel].join unless rel.is_a? Symbol
|
30
|
+
all("a[rel=\"#{rel}\"]").first.click
|
31
|
+
end
|
32
|
+
|
33
|
+
def submit!
|
34
|
+
find('input[type="submit"]').click
|
35
|
+
end
|
36
|
+
|
37
|
+
def within_form(name, &block)
|
38
|
+
within(all("form[@name=\"#{name}\"]").first, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def submit_form(name, &block)
|
42
|
+
within(all("form[@name=\"#{name}\"]").first) do
|
43
|
+
yield block
|
44
|
+
submit!
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def refresh
|
49
|
+
visit current_path
|
50
|
+
end
|
51
|
+
|
52
|
+
def env
|
53
|
+
YaksHTML::RACK_ENV
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
RSpec.configure do |config|
|
59
|
+
select = {type: :yaks_integration}
|
60
|
+
|
61
|
+
config.include Capybara::DSL, select
|
62
|
+
config.include Capybara::RSpecMatchers, select
|
63
|
+
|
64
|
+
config.include YaksHTML::CapybaraDSL, select
|
65
|
+
|
66
|
+
config.before(select) do
|
67
|
+
YaksHTML::RACK_ENV.__setobj__('HTTP_ACCEPT' => 'text/html')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
Capybara.register_driver :rack_test do |app|
|
72
|
+
Capybara::RackTest::Driver.new(app, headers: YaksHTML::RACK_ENV)
|
73
|
+
end
|
data/lib/yaks/format/html.rb
CHANGED
@@ -12,7 +12,7 @@ module Yaks
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def section(name)
|
15
|
-
template.select(".#{name}").first
|
15
|
+
template.select(".#{name}").first
|
16
16
|
end
|
17
17
|
|
18
18
|
def serialize_resource(resource)
|
@@ -120,22 +120,32 @@ module Yaks
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def render_field(field)
|
123
|
-
|
123
|
+
attrs = field.to_h_compact
|
124
|
+
|
125
|
+
if attrs.key? :checked
|
126
|
+
if attrs[:checked]
|
127
|
+
attrs[:checked] = 'checked'
|
128
|
+
else
|
129
|
+
attrs.delete(:checked)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
extra_info = reject_keys(attrs, :type, :name, :value, :label, :options)
|
124
134
|
H[:tr,
|
125
135
|
H[:td,
|
126
136
|
H[:label, {for: field.name}, [field.label || field.name.to_s, field.required ? '*' : ''].join]],
|
127
137
|
H[:td,
|
128
138
|
case field.type
|
129
139
|
when /select/
|
130
|
-
H[:select, reject_keys(
|
140
|
+
H[:select, reject_keys(attrs, :options), render_select_options(field.options)]
|
131
141
|
when /textarea/
|
132
|
-
H[:textarea, reject_keys(
|
142
|
+
H[:textarea, reject_keys(attrs, :value), field.value || '']
|
133
143
|
when /hidden/
|
134
144
|
[ field.value.inspect,
|
135
|
-
H[:input,
|
145
|
+
H[:input, attrs]
|
136
146
|
]
|
137
147
|
else
|
138
|
-
H[:input,
|
148
|
+
H[:input, attrs]
|
139
149
|
end],
|
140
150
|
H[:td, extra_info.empty? ? '' : extra_info.inspect]
|
141
151
|
]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'support/test_app'
|
3
|
+
|
4
|
+
RSpec.describe Yaks::Format::HTML, type: :yaks_integration do
|
5
|
+
before do
|
6
|
+
Capybara.app = TestApp
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:rel_prefix) { 'http://myapi.example.com/rel/' }
|
10
|
+
|
11
|
+
it 'should allow browsing the api' do
|
12
|
+
visit '/'
|
13
|
+
|
14
|
+
expect(page).to have_content 'GET /'
|
15
|
+
expect(page).to have_content 'generated with Yaks'
|
16
|
+
|
17
|
+
click_rel('friends')
|
18
|
+
|
19
|
+
expect(current_path).to eql '/friends'
|
20
|
+
|
21
|
+
expect(page).to have_content 'GET /friends'
|
22
|
+
expect(page).to have_content 'Matt'
|
23
|
+
|
24
|
+
submit_form(:poke) do
|
25
|
+
fill_in :message, with: 'Free the means of production'
|
26
|
+
end
|
27
|
+
|
28
|
+
expect(current_path).to eql '/poke/Matt'
|
29
|
+
|
30
|
+
expect(page).to have_content 'You poked Matt: Free the means of production'
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
1
|
+
require 'yaks'
|
2
|
+
require 'yaks-html/rspec'
|
3
|
+
require 'yaks-sinatra'
|
4
|
+
|
5
|
+
require_relative '../../shared/rspec_config'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class TestApp < Sinatra::Base
|
2
|
+
register Yaks::Sinatra
|
3
|
+
|
4
|
+
class HomeMapper < Yaks::Mapper
|
5
|
+
link 'http://myapi.example.com/rel/friends', '/friends'
|
6
|
+
end
|
7
|
+
|
8
|
+
class FriendMapper < Yaks::Mapper
|
9
|
+
attribute :name
|
10
|
+
|
11
|
+
def name
|
12
|
+
object[:name]
|
13
|
+
end
|
14
|
+
|
15
|
+
form :poke do
|
16
|
+
action '/poke/{name}'
|
17
|
+
method 'POST'
|
18
|
+
text :message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class MessageMapper < Yaks::Mapper
|
23
|
+
attribute :message do
|
24
|
+
object[:message]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
configure_yaks do
|
29
|
+
mapper_for :home, HomeMapper
|
30
|
+
rel_template 'http://myapi.example.com/rel/{rel}'
|
31
|
+
end
|
32
|
+
|
33
|
+
get '/' do
|
34
|
+
yaks :home
|
35
|
+
end
|
36
|
+
|
37
|
+
get '/friends' do
|
38
|
+
yaks [{name: 'Matt'}, {name: 'Yohan'}, {name: 'Janko'}], item_mapper: FriendMapper
|
39
|
+
end
|
40
|
+
|
41
|
+
post '/poke/:name' do
|
42
|
+
yaks({message: "You poked #{params[:name]}: #{params[:message]}"}, mapper: MessageMapper)
|
43
|
+
end
|
44
|
+
end
|
data/yaks-html.gemspec
CHANGED
@@ -22,4 +22,9 @@ Gem::Specification.new do |gem|
|
|
22
22
|
|
23
23
|
gem.add_runtime_dependency 'yaks', Yaks::VERSION
|
24
24
|
gem.add_runtime_dependency 'hexp', '>= 0.4'
|
25
|
+
|
26
|
+
gem.add_development_dependency 'yaks-sinatra'
|
27
|
+
gem.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
gem.add_development_dependency 'rack-test'
|
29
|
+
gem.add_development_dependency 'capybara'
|
25
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaks-html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 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-
|
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.
|
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.
|
26
|
+
version: 0.11.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: hexp
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +38,62 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yaks-sinatra
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: capybara
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
41
97
|
description: HTML output format for Yaks
|
42
98
|
email:
|
43
99
|
- arne@arnebrasseur.net
|
@@ -49,9 +105,12 @@ files:
|
|
49
105
|
- README.md
|
50
106
|
- Rakefile
|
51
107
|
- lib/yaks-html.rb
|
108
|
+
- lib/yaks-html/rspec.rb
|
52
109
|
- lib/yaks/format/html.rb
|
53
110
|
- lib/yaks/format/template.html
|
111
|
+
- spec/smoke_test_spec.rb
|
54
112
|
- spec/spec_helper.rb
|
113
|
+
- spec/support/test_app.rb
|
55
114
|
- yaks-html.gemspec
|
56
115
|
homepage: https://github.com/plexus/yaks
|
57
116
|
licenses:
|
@@ -73,9 +132,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
132
|
version: '0'
|
74
133
|
requirements: []
|
75
134
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.2.3
|
77
136
|
signing_key:
|
78
137
|
specification_version: 4
|
79
138
|
summary: HTML output format for Yaks
|
80
139
|
test_files:
|
140
|
+
- spec/smoke_test_spec.rb
|
81
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/support/test_app.rb
|