api_problem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api-problem.gemspec
4
+ gem 'minitest'
5
+ gem 'coveralls', require: false
6
+
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Guille Carlos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # ApiProblem for Rails
2
+ [![CodeClimate](https://codeclimate.com/github/guillec/http_problem.png)](https://codeclimate.com/github/guillec/api_problem)
3
+ [![Build Status](https://travis-ci.org/guillec/api-problem.png)](https://travis-ci.org/guillec/api_problem)
4
+ [![Coverage
5
+ Status](https://coveralls.io/repos/guillec/api-problem/badge.png)](https://coveralls.io/r/guillec/api_problem)
6
+
7
+ With this gem you can return api errors that follow the api-problem draft specs
8
+ - http://tools.ietf.org/html/draft-nottingham-http-problem-04
9
+
10
+ Here is a explanation behind the draft:
11
+ - http://www.mnot.net/blog/2013/05/15/http_problem
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'api_problem'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install api_problem
26
+
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
+ ## Usage in Rails
40
+ ApiProblem provides a Railtie that registers the proper MIME types with Rails:
41
+ - application/api-problem+json
42
+ - application/api-problem+xml
43
+
44
+
45
+ To use create your api problems controller:
46
+
47
+ rails g controller api_problems first_error_name second_error_name
48
+
49
+ The urls to these views is what you will pass to the api_problem method as the problem_type.
50
+
51
+ Here is an example of how to return a api_error:
52
+
53
+ format.api_problem_json do
54
+ render json: api_problem first_errors_name_url, "You are out of credits", { }
55
+ end
56
+
57
+ This will respond to the client with something like:
58
+
59
+ {
60
+ "problemType": "http://example.com/problem_details/first_error_name",
61
+ "title": "You are out of credits",
62
+ ...
63
+ }
64
+
65
+ Sample curl call
66
+
67
+ curl -i "http://localhost:3000/emails" -H 'ACCEPT: application/api-problem+json'
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_problem/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api_problem"
8
+ spec.version = ApiProblem::VERSION
9
+ spec.authors = ["Guille Carlos"]
10
+ spec.email = ["guille@bitpop.in"]
11
+ spec.description = %q{An implementation of draft-nottingham-http-problem.}
12
+ spec.summary = %q{An implementation of draft-nottingham-http-problem.}
13
+ spec.homepage = "https://github.com/guillec/api_problem"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails'
2
+
3
+ module ApiProblem
4
+ class Railtie < ::Rails::Railtie
5
+ config.to_prepare do
6
+ ApplicationController.send(:include, ApiProblem)
7
+ end
8
+ initializer 'api-problem-json' do
9
+ Mime::Type.register 'application/api-problem+json', :api_problem_json
10
+ end
11
+ initializer 'api-problem-xml' do
12
+ Mime::Type.register 'application/api-problem+xml', :api_problem_xml
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module ApiProblem
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require "api_problem/version"
2
+ require "api_problem/railtie" if defined? Rails
3
+
4
+ module ApiProblem
5
+ MissingTitle = Class.new(StandardError)
6
+ MissingProblemType = Class.new(StandardError)
7
+
8
+ def self.build(problem_type, title, options={})
9
+ response ={}
10
+ raise ApiProblem::MissingProblemType if problem_type.nil?
11
+ raise ApiProblem::MissingTitle if title.nil?
12
+ new_options = build_options(options)
13
+ unless new_options.empty?
14
+ { problemType: problem_type, title: title }.merge(new_options)
15
+ else
16
+ { problemType: problem_type, title: title }
17
+ end
18
+ end
19
+
20
+ def api_problem(problem_type, title, options={})
21
+ ApiProblem.build problem_type, title, options
22
+ end
23
+
24
+ private
25
+ def self.build_options(options)
26
+ new_options ={}
27
+ options.each do |key, value|
28
+ key = convert_key(key)
29
+ value = convert_value(key, value)
30
+ new_options[key] = value
31
+ end
32
+ new_options
33
+ end
34
+
35
+ def self.convert_key(key)
36
+ if key == :http_status || key == "http_status"
37
+ key = "httpStatus"
38
+ elsif key == :problem_instance || key == "problem_instance"
39
+ key = "problemInstance"
40
+ end
41
+ key.to_s
42
+ end
43
+
44
+ def self.convert_value(key, value)
45
+ if key == "httpStatus"
46
+ value = value.to_i
47
+ end
48
+ value
49
+ end
50
+ end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+ require 'api_problem'
3
+
4
+ describe "API Problem Draft Specs" do
5
+
6
+ it "should return a http problem hash" do
7
+ response = { problemType: "http://example.com/probs/out-of-credit", title: "Your do not have enough credit."}
8
+ http_problem = ApiProblem.build("http://example.com/probs/out-of-credit", "Your do not have enough credit.")
9
+ assert_equal response, http_problem
10
+ end
11
+
12
+ end
13
+
14
+ describe "Section 3.1: Required Members" do
15
+
16
+ describe "A problem details object MUST have the following" do
17
+
18
+ it "should have a problemType" do
19
+ assert_raises ApiProblem::MissingProblemType do
20
+ http_problem = ApiProblem.build(nil, "You do not have enough credit.")
21
+ end
22
+ end
23
+
24
+ it "should have a title" do
25
+ assert_raises ApiProblem::MissingTitle do
26
+ http_problem = ApiProblem.build("http://example.com/probs/out-of-credit", nil)
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ describe "Section 3.2: Optional Members" do
34
+
35
+ describe "A problem details object MAY have the following members" do
36
+
37
+ it "may have a httpStatus" do
38
+ http_problem = ApiProblem.build("http://example.com", "You do not have enough credit.", http_status: 200 )
39
+ assert_equal 200, http_problem["httpStatus"]
40
+ end
41
+
42
+ it "may have a problemInstance" do
43
+ http_problem = ApiProblem.build("http://example.com", "You do not have enough credit.", problem_instance: "http://example.net/account/12345/msgs/abc" )
44
+ assert_equal "http://example.net/account/12345/msgs/abc", http_problem["problemInstance"]
45
+ end
46
+
47
+ end
48
+
49
+ describe "httpStatus" do
50
+
51
+ it "should be a number" do
52
+ http_problem = ApiProblem.build("http://example.com", "You do not have enough credit.", http_status: "200" )
53
+ assert_equal 200, http_problem["httpStatus"]
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ describe "3.3: Extension Members" do
61
+
62
+ describe "Problem type definitions MAY extend the problem details object with additional members." do
63
+
64
+ it "will accepst options values" do
65
+ http_problem = ApiProblem.build("http://example.com", "You do not have enough credit.", hammer: "Time", array: [1, 2, 3] )
66
+ assert_equal "Time", http_problem["hammer"]
67
+ assert_equal [1,2,3], http_problem["array"]
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
@@ -0,0 +1,14 @@
1
+ require "test_helper.rb"
2
+ require "api_problem"
3
+
4
+ describe "ApiProblem module" do
5
+ it "should return a hash" do
6
+
7
+ fake_class = class Class
8
+ include ApiProblem
9
+ end
10
+
11
+ http_problem = { problemType: "http://example.com", title: "This is a test" }
12
+ assert_equal http_problem, fake_class.new.api_problem("http://example.com", "This is a test")
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter do |source_file|
4
+ source_file.filename =~ /test/
5
+ end
6
+ end
7
+
8
+ require 'coveralls'
9
+ Coveralls.wear!
10
+
11
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_problem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guille Carlos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: An implementation of draft-nottingham-http-problem.
47
+ email:
48
+ - guille@bitpop.in
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - api_problem.gemspec
59
+ - lib/api_problem.rb
60
+ - lib/api_problem/railtie.rb
61
+ - lib/api_problem/version.rb
62
+ - test/api_problem_draft_spec_test.rb
63
+ - test/api_problem_test.rb
64
+ - test/test_helper.rb
65
+ homepage: https://github.com/guillec/api_problem
66
+ licenses:
67
+ - MIT
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.25
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: An implementation of draft-nottingham-http-problem.
90
+ test_files:
91
+ - test/api_problem_draft_spec_test.rb
92
+ - test/api_problem_test.rb
93
+ - test/test_helper.rb
94
+ has_rdoc: