rack_format_response 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +94 -0
  3. data/Rakefile +51 -0
  4. data/lib/rack/format_response.rb +92 -0
  5. metadata +66 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [maiha@wota.jp]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,94 @@
1
+ Rack::FormatResponse
2
+ ====================
3
+
4
+ A Rack middleware for automatically formatting response body
5
+
6
+
7
+ Install
8
+ =======
9
+
10
+ gem install rack_format_response
11
+
12
+
13
+ Usage
14
+ =====
15
+
16
+ use Rack::FormatResponse
17
+
18
+ When non-string value is given in response body,
19
+ it will be passed to Rack::FormatResponse#format_<OBJECT_CLASS>.
20
+
21
+
22
+ Example
23
+ =======
24
+
25
+ Let's consider about generating json response.
26
+
27
+ get "/ping" do
28
+ {"message"=>"pong"}
29
+ end
30
+
31
+ This code generates "messagepong" response due to response#to_s.
32
+ So we must write like following with concern about Content-Type.
33
+
34
+ get "/ping" do
35
+ content_type :json
36
+ hash = {"message"=>"pong"}
37
+ JSON.dump(hash)
38
+ end
39
+
40
+ This is a pain and not fun!
41
+
42
+ Well, with this module, we can enjoy rack with various objects.
43
+
44
+ # use Rack::FormatResponse
45
+ get "/ping" do
46
+ {"message"=>"pong"}
47
+ end
48
+ ->
49
+ Content-Type: application/json;charset=utf-8
50
+ {"message":"pong"}
51
+
52
+
53
+ Customize
54
+ =========
55
+
56
+ When you want automatic formatter from Hash to XML,
57
+ It would be like this.
58
+
59
+ require "rack/format_response"
60
+ require "active_suport"
61
+
62
+ class Rack::FormatResponse
63
+ def format_hash(hash)
64
+ hash.to_xml
65
+ end
66
+ end
67
+ Rack::FormatResponse::MAPPINGS["Hash"] = "format_hash"
68
+
69
+ or just write code in block.
70
+
71
+ Rack::FormatResponse::MAPPINGS["Hash"] = lambda{|hash| hash.to_xml}
72
+
73
+
74
+ NOTE
75
+ ====
76
+
77
+ In default, this module converts hash to json with Yajl gem.
78
+ Install it first for json use.
79
+
80
+ gem install yajl
81
+
82
+ # Yajl is a fastest json library where I know
83
+
84
+
85
+ Homepage
86
+ ========
87
+
88
+ http://github.com/maiha/rack_format_response
89
+
90
+
91
+ Author
92
+ ======
93
+ Maiha <maiha@wota.jp>
94
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "rack_format_response"
5
+ AUTHOR = "maiha"
6
+ EMAIL = "maiha@wota.jp"
7
+ HOMEPAGE = "http://github.com/maiha/rack_format_response"
8
+ SUMMARY = "A Rack middleware for automatically formatting response body"
9
+ GEM_VERSION = "0.0.1"
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'asakusarb'
13
+ s.executables = []
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.require_path = 'lib'
25
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
31
+
32
+ desc "Install the gem"
33
+ task :install do
34
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
35
+ end
36
+
37
+ desc "Uninstall the gem"
38
+ task :uninstall do
39
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
40
+ end
41
+
42
+ desc "Create a gemspec file"
43
+ task :gemspec do
44
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
45
+ file.puts spec.to_ruby
46
+ end
47
+ end
48
+
49
+ require 'spec/rake/spectask'
50
+ desc 'Default: run spec examples'
51
+ task :default => 'spec'
@@ -0,0 +1,92 @@
1
+ module Rack
2
+ #
3
+ # A Rack middleware for automatically formatting response body
4
+ #
5
+ # e.g.
6
+ # # in usual case
7
+ # get "/ping" do
8
+ # {"message"=>"pong"}
9
+ # end
10
+ # ->
11
+ # # forced to to_s
12
+ # messagepong
13
+ #
14
+ #
15
+ # # use Rack::FormatResponse # with string response
16
+ # get "/ping" do
17
+ # "pong"
18
+ # end
19
+ # ->
20
+ # # calls Rack::FormatResponse#format_string that effects nothing
21
+ # # in short, nothing special
22
+ # Content-Type: text/html
23
+ # "pong"
24
+ #
25
+ # # use Rack::FormatResponse # with hash response
26
+ # get "/ping" do
27
+ # {"message"=>"pong"}
28
+ # end
29
+ # ->
30
+ # # calls Rack::FormatResponse#format_hash_to_json
31
+ # # due to Rack::FormatResponse::MAPPINGS
32
+ # Content-Type: application/json;charset=utf-8
33
+ # {"message":"pong"}
34
+ #
35
+
36
+ class FormatResponse
37
+ MAPPINGS = {
38
+ "Hash" => :format_hash_to_json,
39
+ }
40
+
41
+ class UnknownFormat < RuntimeError; end
42
+ class InvalidFormat < RuntimeError; end
43
+
44
+ def initialize(app, opts = {})
45
+ @app = app
46
+ @opts = opts
47
+ end
48
+
49
+ def call(env)
50
+ @status, @headers, body = @app.call(env)
51
+ body = [apply(body)].flatten
52
+ [@status, @headers, @body || body]
53
+ end
54
+
55
+ private
56
+ def lookup(body)
57
+ body = body[0] if body.class == Array and body.size == 1
58
+ MAPPINGS[body.class.name] or
59
+ "format_" + body.class.name.gsub(/::/,'_').downcase
60
+ end
61
+
62
+ def apply(data)
63
+ func = lookup(data)
64
+ case func
65
+ when Proc
66
+ return func.call(data)
67
+ when Symbol, String
68
+ return __send__(func, data) if respond_to?(func, true)
69
+ raise UnknownFormat, "no formatters named `#{func}'"
70
+ else
71
+ raise UnknownFormat, "no formatters typed `#{func.class}'"
72
+ end
73
+ end
74
+
75
+ ######################################################################
76
+ ### Formatters
77
+
78
+ def format_array(ary)
79
+ ary
80
+ end
81
+
82
+ def format_string(str)
83
+ str
84
+ end
85
+
86
+ def format_hash_to_json(hash)
87
+ require 'yajl'
88
+ @headers['Content-Type'] = "application/json;charset=utf-8"
89
+ Yajl.dump(hash)
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_format_response
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - maiha
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-08 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A Rack middleware for automatically formatting response body
22
+ email: maiha@wota.jp
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - MIT-LICENSE
30
+ files:
31
+ - MIT-LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/rack/format_response.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/maiha/rack_format_response
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project: asakusarb
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Rack middleware for automatically formatting response body
65
+ test_files: []
66
+