api_problem 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -37
- data/api_problem.gemspec +2 -0
- data/lib/api_problem/version.rb +1 -1
- data/lib/generators/api_problem/USAGE +8 -0
- data/lib/generators/api_problem/api_problem_generator.rb +69 -0
- data/lib/generators/api_problem/templates/api_problem_layout.jbuilder.erb +5 -0
- data/lib/generators/api_problem/templates/api_problem_layout.xml.erb +18 -0
- data/lib/generators/api_problem/templates/errors_controller.erb +2 -0
- data/lib/generators/api_problem/templates/errors_view.html.erb +2 -0
- data/test/api_problem_generator.rb +20 -0
- metadata +26 -2
data/README.md
CHANGED
@@ -5,11 +5,15 @@
|
|
5
5
|
Status](https://coveralls.io/repos/guillec/api-problem/badge.png)](https://coveralls.io/r/guillec/api_problem)
|
6
6
|
|
7
7
|
With this gem you can return api errors that follow the api-problem draft specs
|
8
|
-
|
8
|
+
=======
|
9
|
+
With this gem you can return api errors that follow the http api problem draft
|
10
|
+
- http://tools.ietf.org/html/draft-nottingham-http-problem-06
|
9
11
|
|
10
12
|
Here is a explanation behind the draft:
|
11
13
|
- http://www.mnot.net/blog/2013/05/15/http_problem
|
12
14
|
|
15
|
+
## VERSION 0.0.2 almost ready this is what you will be able to do....
|
16
|
+
|
13
17
|
## Installation
|
14
18
|
|
15
19
|
Add this line to your application's Gemfile:
|
@@ -24,47 +28,42 @@ Or install it yourself as:
|
|
24
28
|
|
25
29
|
$ gem install api_problem
|
26
30
|
|
27
|
-
## Basics on Gem
|
28
|
-
|
29
|
-
This gem installs the following method:
|
30
|
-
|
31
|
-
api_problem( problem_type, title, { optional stuff } )
|
32
|
-
|
33
|
-
The method params:
|
34
|
-
|
35
|
-
problem_type = required
|
36
|
-
title = required
|
37
|
-
args = optional values
|
38
|
-
|
39
31
|
## Usage in Rails
|
40
32
|
ApiProblem provides a Railtie that registers the proper MIME types with Rails:
|
41
33
|
- application/api-problem+json
|
42
34
|
- application/api-problem+xml
|
43
35
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
36
|
+
This gem installs a generator for your project
|
37
|
+
|
38
|
+
rail g api_problem bad_token_error
|
39
|
+
|
40
|
+
You can specify the problem detail object members
|
41
|
+
|
42
|
+
rail g api_problem bad_token_error type:"http://example.com/probs/out-of-credit" status:403 title:"You do not have enough credit." detail:"Your current balance is 30, but that costs 50."
|
43
|
+
|
44
|
+
You can also namespace it with the ns key
|
45
|
+
|
46
|
+
rail g api_problem bad_token_error ns:api
|
47
|
+
|
48
|
+
About the type, if you DON'T want a type you need to specify with false
|
49
|
+
|
50
|
+
rail g api_problem bad_token_error type:false
|
51
|
+
|
52
|
+
About the type, if you dont set a type, it will default to the error name
|
53
|
+
|
54
|
+
# This will create a bad_token_error type for you.
|
55
|
+
rail g api_problem bad_token_error
|
56
|
+
|
57
|
+
Files that get created for you
|
58
|
+
|
59
|
+
create app/views/api_problems/bad_token_error.jbuilder
|
60
|
+
create app/views/errors/bad_token_error.html.erb
|
61
|
+
create app/controllers/errors_controller.rb
|
62
|
+
route match '/bad_token_error' => 'errors#bad_token_error', :via => :get, :as => :bad_token_error
|
63
|
+
|
64
|
+
## TODO
|
65
|
+
- The XML views
|
66
|
+
- Tests for the genrator
|
68
67
|
|
69
68
|
## Contributing
|
70
69
|
|
data/api_problem.gemspec
CHANGED
data/lib/api_problem/version.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
class ApiProblemGenerator < Rails::Generators::NamedBase
|
2
|
+
source_root File.expand_path('../templates', __FILE__)
|
3
|
+
argument :one, :type => :hash, :required => false, :default => {}
|
4
|
+
|
5
|
+
def build_http_error
|
6
|
+
template "api_problem_layout.jbuilder.erb", "app/views#{ns_path}api_problems/#{name}.jbuilder"
|
7
|
+
template "api_problem_layout.xml.erb", "app/views#{ns_path}api_problems/#{name}.xml.erb"
|
8
|
+
end
|
9
|
+
|
10
|
+
def build_error_html
|
11
|
+
template "errors_view.html.erb", "app/views#{ns_path}errors/#{name}.html.erb"
|
12
|
+
template "errors_controller.erb", "app/controllers#{ns_path}errors_controller.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_route
|
16
|
+
route "match '#{ns_path}#{name}' => '#{ns_class_name}errors##{name}', :via => :get, :as => :#{problem}"
|
17
|
+
inject_into_class "app/controllers/#{ns_path}errors_controller.rb", "#{ns_class_name}ErrorsController".classify, "def #{name}\nend\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
# User must specify that they don't a type by setting type:false
|
22
|
+
def type
|
23
|
+
if one["type"].nil?
|
24
|
+
problem_url
|
25
|
+
elsif one["type"] && one["type"] != "false"
|
26
|
+
"'#{one["type"]}'"
|
27
|
+
elsif one["type"] && one["type"] == "false"
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def title
|
33
|
+
one["title"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def status
|
37
|
+
one["status"] ? Integer(one["status"]) : nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def detail
|
41
|
+
one["detail"]
|
42
|
+
end
|
43
|
+
|
44
|
+
def instance
|
45
|
+
one["instance"]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Namespace
|
49
|
+
def ns
|
50
|
+
one["ns"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def ns_path
|
54
|
+
ns ? "/#{ns}/" : "/"
|
55
|
+
end
|
56
|
+
|
57
|
+
def ns_class_name
|
58
|
+
ns ? "#{ns.classify}::" : ""
|
59
|
+
end
|
60
|
+
|
61
|
+
def problem
|
62
|
+
ns ? "#{ns}_#{name}" : "#{name}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def problem_url
|
66
|
+
"#{problem}_url"
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
<% if type %>json.set! :type, <%= type %><% end %>
|
2
|
+
<% if title %>json.set! :title, "<%= title %>"<% end %>
|
3
|
+
<% if status %>json.set! :status, <%= status %><% end %>
|
4
|
+
<% if detail %>json.set! :detail, "<%= detail %>"<% end %>
|
5
|
+
<% if instance %>json.set! :instance, "<%= instance %>"<% end %>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<problem xmlns="urn:ietf:rfc:XXXX">
|
3
|
+
<% if type %>
|
4
|
+
<type><%%= type %></type>
|
5
|
+
<% end %>
|
6
|
+
<% if status %>
|
7
|
+
<statu><%= status %></status>
|
8
|
+
<% end %>
|
9
|
+
<% if title %>
|
10
|
+
<title><%= title %></title>
|
11
|
+
<% end %>
|
12
|
+
<% if detail %>
|
13
|
+
<detail><%= detail %></detail>
|
14
|
+
<% end %>
|
15
|
+
<% if instance %>
|
16
|
+
<instance><%= instance %></instance>
|
17
|
+
<% end %>
|
18
|
+
</problem>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'api_problem'
|
3
|
+
|
4
|
+
class CellGeneratorTest < Rails::Generators::TestCase
|
5
|
+
p "asflkasflksadjflaksdjfl;k"
|
6
|
+
destination File.join(Rails.root, "tmp")
|
7
|
+
setup :prepare_destination
|
8
|
+
tests ::Cells::Generators::CellGenerator
|
9
|
+
|
10
|
+
test "create the standard assets" do
|
11
|
+
run_generator %w(Blog post latest)
|
12
|
+
|
13
|
+
assert_file "app/cells/blog_cell.rb", /class BlogCell < Cell::Rails/
|
14
|
+
assert_file "app/cells/blog_cell.rb", /def post/
|
15
|
+
assert_file "app/cells/blog_cell.rb", /def latest/
|
16
|
+
assert_file "app/cells/blog/post.html.erb", %r(app/cells/blog/post.html.erb)
|
17
|
+
assert_file "app/cells/blog/latest.html.erb", %r(app/cells/blog/latest.html.erb)
|
18
|
+
assert_file "test/cells/blog_cell_test.rb"
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_problem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jbuilder
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: An implementation of draft-nottingham-http-problem.
|
47
63
|
email:
|
48
64
|
- guille@bitpop.in
|
@@ -59,7 +75,14 @@ files:
|
|
59
75
|
- lib/api_problem.rb
|
60
76
|
- lib/api_problem/railtie.rb
|
61
77
|
- lib/api_problem/version.rb
|
78
|
+
- lib/generators/api_problem/USAGE
|
79
|
+
- lib/generators/api_problem/api_problem_generator.rb
|
80
|
+
- lib/generators/api_problem/templates/api_problem_layout.jbuilder.erb
|
81
|
+
- lib/generators/api_problem/templates/api_problem_layout.xml.erb
|
82
|
+
- lib/generators/api_problem/templates/errors_controller.erb
|
83
|
+
- lib/generators/api_problem/templates/errors_view.html.erb
|
62
84
|
- test/api_problem_draft_spec_test.rb
|
85
|
+
- test/api_problem_generator.rb
|
63
86
|
- test/api_problem_test.rb
|
64
87
|
- test/test_helper.rb
|
65
88
|
homepage: https://github.com/guillec/api_problem
|
@@ -89,6 +112,7 @@ specification_version: 3
|
|
89
112
|
summary: An implementation of draft-nottingham-http-problem.
|
90
113
|
test_files:
|
91
114
|
- test/api_problem_draft_spec_test.rb
|
115
|
+
- test/api_problem_generator.rb
|
92
116
|
- test/api_problem_test.rb
|
93
117
|
- test/test_helper.rb
|
94
118
|
has_rdoc:
|