sinatra-jsonapi 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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kyle Drake
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,67 @@
1
+ # Sinatra::JSONAPI
2
+
3
+ This is an extension for Sinatra that makes the Base class return JSON for 404 and 500 errors, and gives you api\_response and api\_error for quick JSON responses.
4
+
5
+ ## Caveats
6
+
7
+ Does not automatically try to convert response. You need to use api\_response or api\_error, or halt with proper JSON like such:
8
+
9
+ get '/ping' do
10
+ halt {response: 'pong'}.to_json
11
+ end
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'sinatra-jsonapi', require: 'sinatra/jsonapi'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install sinatra-jsonapi
26
+
27
+ ## Usage
28
+
29
+ For classy apps:
30
+
31
+ require 'sinatra'
32
+ require 'sinatra/jsonapi'
33
+
34
+ class MyCoolAPI < Sinatra::Base
35
+ register Sinatra::JSONAPI
36
+
37
+ get '/hello' do
38
+ api_response response: 'world!'
39
+ end
40
+
41
+ # Returns:
42
+ # {"response":"world!"}
43
+
44
+ get '/break' do
45
+ api_error :your_error_type, 'human readable version of error message'
46
+ end
47
+
48
+ # Returns:
49
+ # {"error":{"error_type":"your_error_type", "message":"human readable version of your error message"}}
50
+ end
51
+
52
+ For classic style apps (no class), registration of the extension happens automatically:
53
+
54
+ require 'sinatra'
55
+ require 'sinatra/jsonapi'
56
+
57
+ get '/hello' do
58
+ api_response response: 'world!'
59
+ end
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ # To run tests in the order of a seed: bundle exec rake test TESTOPTS="--seed=987"
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'test/*_spec.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,61 @@
1
+ require 'sinatra'
2
+ require 'json'
3
+ require './lib/sinatra/jsonapi/version.rb'
4
+
5
+ module Sinatra
6
+ module JSONAPI
7
+ def self.registered(app)
8
+ app.disable :show_exceptions
9
+ app.disable :raise_errors
10
+ app.disable :protection
11
+ app.enable :raise_errors if test?
12
+
13
+ app.before do
14
+ content_type :json
15
+ end
16
+
17
+ app.error do
18
+ api_error 'unexpected_error', 'An unexpected error has occured, please try your request again later'
19
+ end
20
+
21
+ # Disables Sinatra's internal HTML error message for development mode.
22
+ app.error Sinatra::NotFound do
23
+ not_found
24
+ end
25
+
26
+ app.not_found do
27
+ # If 404 body is not the default "Not Found" message, we can return it without processing.
28
+ return response if response.body.first != "<h1>Not Found</h1>"
29
+
30
+ api_error 'not_found', "The requested method was not found: #{request.path}"
31
+ end
32
+
33
+ app.helpers do
34
+ def api_error(type, message)
35
+ payload = {error: {type: type, message: message}}
36
+ halt process_payload(payload)
37
+ end
38
+
39
+ def api_response(payload)
40
+ halt process_payload(payload)
41
+ end
42
+
43
+ def _jsonapi_to_json(input)
44
+ JSON.unparse input
45
+ end
46
+
47
+ def process_payload(payload)
48
+ if params[:callback]
49
+ response_body = "#{params[:callback]}({\n"+
50
+ " #{_jsonapi_to_json payload}\n"+
51
+ "})"
52
+ else
53
+ response_body = _jsonapi_to_json payload
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ register JSONAPI
61
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module JSONAPI
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/jsonapi/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sinatra-jsonapi"
8
+ gem.version = Sinatra::JSONAPI::VERSION
9
+ gem.authors = ["Kyle Drake"]
10
+ gem.email = ["kyledrake@gmail.com"]
11
+ gem.description = %q{Turns a Sinatra Base class into a JSON api}
12
+ gem.summary = %q{This gem makes it easy to make a JSON api by converting some HTML-esque things in Sinatra into JSON}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'json'
21
+ gem.add_dependency 'sinatra'
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'minitest'
24
+ gem.add_development_dependency 'rack-test'
25
+ gem.add_development_dependency 'pry'
26
+ end
@@ -0,0 +1,95 @@
1
+ require 'bundler/setup'
2
+ ENV['RACK_ENV'] = 'test'
3
+ require 'minitest/autorun'
4
+ require 'rack/test'
5
+ require File.join(File.join(File.expand_path(File.dirname(__FILE__))), '..', 'lib', 'sinatra', 'jsonapi.rb')
6
+
7
+ include Rack::Test::Methods
8
+
9
+ class TestApp < Sinatra::Base
10
+ register Sinatra::JSONAPI
11
+ end
12
+
13
+ def mock_app(&block)
14
+ @app = Sinatra.new TestApp, &block
15
+ end
16
+
17
+ def app
18
+ @app
19
+ end
20
+
21
+ def as_json(hash)
22
+ hash.to_json
23
+ end
24
+
25
+ describe Sinatra::JSONAPI do
26
+ it 'should return 404 JSON on unknown request' do
27
+ mock_app {
28
+ register Sinatra::JSONAPI
29
+ }
30
+ get '/notfound'
31
+ last_response.body.wont_equal '<h1>Not Found</h1>'
32
+ last_response.body.must_equal(as_json(error: {type: 'not_found', message: 'The requested method was not found: /notfound'}))
33
+ end
34
+
35
+ it 'should return generic 500 JSON on uncaught exception' do
36
+ mock_app {
37
+ disable :raise_errors
38
+ get '/error' do
39
+ raise 'Hell!'
40
+ end
41
+ }
42
+
43
+ get '/error'
44
+
45
+ error_msg = {error: {type: 'unexpected_error', message: 'An unexpected error has occured, please try your request again later'}}
46
+
47
+ last_response.body.must_equal error_msg.to_json
48
+ end
49
+
50
+ it 'should return custom 500 JSON error' do
51
+ mock_app {
52
+ get '/custom' do
53
+ api_error 'wrong_argument', 'no arguments'
54
+ end
55
+ }
56
+
57
+ get '/custom'
58
+ last_response.body.must_equal(as_json(error: {type: 'wrong_argument', message: 'no arguments'}))
59
+ end
60
+
61
+ it 'should return hello world json' do
62
+
63
+ mock_app {
64
+ get '/hello' do
65
+ api_response response: 'hello'
66
+ end
67
+ }
68
+
69
+ get '/hello'
70
+ last_response.body.must_equal(as_json(response: 'hello'))
71
+ end
72
+
73
+ it 'should return jsonp when requested' do
74
+ mock_app {
75
+ get '/' do
76
+ api_response response: 'hello'
77
+ end
78
+ }
79
+
80
+ get '/?callback=abc123'
81
+
82
+ expected = "abc123({\n"+
83
+ " #{as_json(response: 'hello')}\n"+
84
+ "})"
85
+
86
+ last_response.body.must_equal expected
87
+ end
88
+
89
+ it 'should return error with jsonp' do
90
+ mock_app
91
+ get '/sdfsdfsd?callback=abcd123'
92
+ last_response.body.must_match /abcd123\(\{/
93
+ last_response.body.must_match /not found/
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-jsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Drake
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sinatra
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pry
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Turns a Sinatra Base class into a JSON api
111
+ email:
112
+ - kyledrake@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/sinatra/jsonapi.rb
123
+ - lib/sinatra/jsonapi/version.rb
124
+ - sinatra-jsonapi.gemspec
125
+ - test/jsonapi_spec.rb
126
+ homepage: ''
127
+ licenses: []
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.24
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: This gem makes it easy to make a JSON api by converting some HTML-esque things
150
+ in Sinatra into JSON
151
+ test_files:
152
+ - test/jsonapi_spec.rb
153
+ has_rdoc: