raccept 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sam Schenkman-Moore
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.
@@ -0,0 +1,33 @@
1
+ = Raccept (Rack Accepts Helper)
2
+
3
+ Converts Ruby objects to JSON or XML based on accepts headers.
4
+
5
+ Ignores legal Rack responses like "string" and ["string","string"]
6
+
7
+ == Use
8
+
9
+ # in config.ru
10
+ use Raccept
11
+ run lambda {|env| [200,{"Content-Type" => "text/plain"}, {:this => 'is a Hash'} ] }
12
+
13
+ # Will respond with ...
14
+ # Accepts application/xml or text/xml
15
+ <?xml version="1.0" encoding="UTF-8"?>
16
+ <hash>
17
+ <this>is a Hash</this>
18
+ </hash>
19
+
20
+ # Accepts application/json or text/json
21
+ {"this":"is a Hash"}
22
+
23
+
24
+ == Todo, maybe
25
+
26
+ * Make into gem?
27
+ * Get something leaner than ActiveSupport for to_xml?
28
+ * Support custom objects somehow?
29
+ * Add tests for it.
30
+
31
+ == Copyright
32
+
33
+ Copyright (c) 2009 Sam Schenkman-Moore. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "raccept"
8
+ gem.summary = %Q{Transforms Ruby objects into xml or json based on accepts headers.}
9
+ gem.description = %Q{This middleware turns Ruby objects that are not acceptable Rack bodies into xml or json depending upon the accepts header.}
10
+ gem.email = "samsm@samsm.com"
11
+ gem.homepage = "http://github.com/samsm/raccept"
12
+ gem.authors = ["Sam Schenkman-Moore"]
13
+ # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "raccept #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+
4
+ require 'lib/raccept'
5
+
6
+ use Raccept
7
+ run lambda {|env| [200,{"Content-Type" => "text/plain"}, {:this => 'is a Hash'} ] }
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+
3
+ # This will require active-support and enhance it so more objects can tbe to_xml'd
4
+ # xml_serialization was found here: http://github.com/alpinegizmo/xml_serialization
5
+ require File.dirname(__FILE__) + '/../vendor/xml_serialization/lib/xml_serialization'
6
+
7
+ class Raccept
8
+ attr_reader :app
9
+ def initialize(app, options = {})
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ result = app.call(env)
15
+ body = result[2]
16
+
17
+ # If body is already a string or array with string(s) pass it along.
18
+ return result if legal_rack_body?(body)
19
+
20
+ accepts_headers = env['HTTP_ACCEPT'].sub(/\;.+/,'').split(',')
21
+ accepts_headers.each do |accepts|
22
+ if header_list[accepts]
23
+ return [result[0],
24
+ result[1],
25
+ convert(header_list[accepts], body)
26
+ ]
27
+ end
28
+ end
29
+ result # apparently nothing matched, so return untouched result
30
+ end
31
+
32
+ def legal_rack_body?(body)
33
+ if body.kind_of?(String)
34
+ true
35
+ elsif body.respond_to?(:each)
36
+ body.collect {|element| element.class }.uniq == [String]
37
+ end
38
+ end
39
+
40
+ def convert(format, body)
41
+ case format
42
+ when :json
43
+ body.to_json
44
+ when :xml
45
+ body.to_xml
46
+ end
47
+ end
48
+
49
+ def header_list
50
+ {
51
+ 'application/json' => :json,
52
+ 'text/json' => :json,
53
+ 'application/xml' => :xml,
54
+ 'text/xml' => :xml
55
+ }
56
+ end
57
+
58
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'raccept'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRaccept < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Mirai España, S.L., (c) 2009 FolkLogic SARL
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.
@@ -0,0 +1,23 @@
1
+ ## What is this?
2
+
3
+ ActiveSupport provides a to_xml method that produces appealing XML for ActiveRecord objects and arrays and hashes containing ActiveRecord objects. The xml_serialization gem extends that XML serialization support to allow for arrays and hashes containing strings, symbols, and integers.
4
+
5
+ The [xslt_render](http://github.com/alpinegizmo/xslt_render/tree/) plugin for rails depends on this gem.
6
+
7
+ For more information, see [Rails on XML, part 7](http://alpinegizmo.com/2009/03/01/rails-on-xml-part-7-xml-serialization.html).
8
+
9
+ ## Installation
10
+
11
+ If you haven't already done so:
12
+
13
+ gem sources -a http://gems.github.com
14
+
15
+ And then:
16
+
17
+ sudo gem install xml_serialization
18
+
19
+ ## Authors
20
+
21
+ This code was originally written by Larry Baltz and David Anderson of [FolkLogic](http://folklogic.com) for [Mirai España](http://miraiespana.com/).
22
+
23
+ Copyright (c) 2008 Mirai España, S.L., (c) 2009 FolkLogic SARL. See MIT-LICENSE for details.
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "xml_serialization"
8
+ gem.summary = %Q{Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.}
9
+ gem.email = "david@folklogic.com"
10
+ gem.homepage = "http://github.com/alpinegizmo/xml_serialization"
11
+ gem.description = "Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers."
12
+ gem.authors = ["Larry Baltz", "David Anderson"]
13
+ gem.add_dependency 'builder'
14
+ gem.add_dependency 'activesupport'
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ desc 'Test the xml_serialization gem.'
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = true
25
+ end
26
+
27
+ desc 'Default: run unit tests.'
28
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,70 @@
1
+ # Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.
2
+
3
+ require 'active_support'
4
+ require 'builder'
5
+
6
+ module SimpleSerializer
7
+ def to_xml(options={})
8
+ builder = options[:builder] || Builder::XmlMarkup.new(:indent => options[:indent])
9
+ tag = options[:root] || self.class.name.downcase
10
+ dasherize = ! options.has_key?(:dasherize) || options[:dasherize]
11
+ tag = dasherize ? tag.to_s.dasherize : tag
12
+ builder.tag!(tag, self.to_s)
13
+ end
14
+ end
15
+
16
+ class String
17
+ include SimpleSerializer
18
+
19
+ XML_HEAD_PATTERN = /\A\s*<\?xml[^>]*>\s*/m
20
+ def strip_xml_head!
21
+ sub!(XML_HEAD_PATTERN, '')
22
+ end
23
+
24
+ def strip_xml_head
25
+ sub(XML_HEAD_PATTERN, '')
26
+ end
27
+ end
28
+
29
+ class Symbol
30
+ include SimpleSerializer
31
+ end
32
+
33
+ class Fixnum
34
+ include SimpleSerializer
35
+ end
36
+
37
+ class Builder::XmlMarkup
38
+ def raw_tag!(tag, string)
39
+ _start_tag(tag, {})
40
+ _text string
41
+ _end_tag(tag)
42
+ end
43
+
44
+ def no_tag!(string)
45
+ _text string
46
+ end
47
+ end
48
+
49
+ # create a string class that doesn't get escaped when dumped into XML stream
50
+ class RawXML < String
51
+ def initialize(string, options={})
52
+ @options = options
53
+ super string
54
+ end
55
+
56
+ def to_xml(options_arg={})
57
+ options = @options.merge options_arg
58
+ builder = options[:builder] ||
59
+ Builder::XmlMarkup.new(:indent => options[:indent])
60
+
61
+ add_tag = options.has_key?(:no_tag) ? !options[:no_tag] : options[:root]
62
+ return builder.no_tag!(self.strip_xml_head) unless add_tag
63
+
64
+ tag = options[:root] || self.class.name.downcase
65
+ dasherize = ! options.has_key?(:dasherize) || options[:dasherize]
66
+ tag = dasherize ? tag.to_s.dasherize : tag
67
+
68
+ builder.raw_tag!(tag, self.strip_xml_head)
69
+ end
70
+ end
@@ -0,0 +1,63 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'test/unit'
3
+ require 'xml_serialization'
4
+
5
+ class TestXMLSerialization < Test::Unit::TestCase
6
+
7
+ def test_rawxml_should_pass_thru
8
+ rawxml = RawXML.new '<tag>content</tag>'
9
+ assert_equal rawxml, rawxml.to_xml
10
+ end
11
+
12
+ def test_fixnum
13
+ assert_equal '<fixnum>1</fixnum>', 1.to_xml
14
+ end
15
+
16
+ def test_string
17
+ assert_equal '<string>abc</string>', 'abc'.to_xml
18
+ end
19
+
20
+ def test_symbol
21
+ assert_equal '<symbol>abc</symbol>', :abc.to_xml
22
+ end
23
+
24
+ def test_array_of_strings
25
+ expected = <<EXPECTED
26
+ <?xml version="1.0" encoding="UTF-8"?>
27
+ <strings type="array">
28
+ <string>a</string>
29
+ <string>b</string>
30
+ <string>c</string>
31
+ </strings>
32
+ EXPECTED
33
+ assert_equal expected, ['a', 'b', 'c'].to_xml
34
+ end
35
+
36
+ def test_array_of_fixnums_with_root_tag_specified
37
+ expected = <<EXPECTED
38
+ <?xml version="1.0" encoding="UTF-8"?>
39
+ <numbers type="array">
40
+ <number>1</number>
41
+ <number>2</number>
42
+ <number>3</number>
43
+ </numbers>
44
+ EXPECTED
45
+ assert_equal expected, [1, 2, 3].to_xml(:root => 'numbers')
46
+ end
47
+
48
+ # this case is used by xslt_render to serialize the hash of controller instance variable values
49
+ def test_hash_with_array
50
+ expected = <<EXPECTED
51
+ <?xml version="1.0" encoding="UTF-8"?>
52
+ <hash>
53
+ <numbers type="array">
54
+ <number>1</number>
55
+ <number>2</number>
56
+ </numbers>
57
+ </hash>
58
+ EXPECTED
59
+ assert_equal expected, {'numbers' => [1, 2]}.to_xml
60
+ assert_equal expected, {'numbers' => ['1', '2']}.to_xml
61
+ end
62
+
63
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{xml_serialization}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Larry Baltz", "David Anderson"]
9
+ s.date = %q{2009-02-22}
10
+ s.description = %q{Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.}
11
+ s.email = %q{david@folklogic.com}
12
+ s.files = ["VERSION.yml", "lib/xml_serialization.rb", "test/xml_serialization_test.rb"]
13
+ s.has_rdoc = true
14
+ s.homepage = %q{http://github.com/alpinegizmo/xml_serialization}
15
+ s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
16
+ s.require_paths = ["lib"]
17
+ s.rubygems_version = %q{1.3.1}
18
+ s.summary = %q{Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ s.add_runtime_dependency(%q<builder>, [">= 0"])
26
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<builder>, [">= 0"])
29
+ s.add_dependency(%q<activesupport>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<builder>, [">= 0"])
33
+ s.add_dependency(%q<activesupport>, [">= 0"])
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raccept
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Schenkman-Moore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-19 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This middleware turns Ruby objects that are not acceptable Rack bodies into xml or json depending upon the accepts header.
17
+ email: samsm@samsm.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - config.ru
33
+ - lib/raccept.rb
34
+ - test/helper.rb
35
+ - test/test_raccept.rb
36
+ - vendor/xml_serialization/MIT-LICENSE
37
+ - vendor/xml_serialization/README.markdown
38
+ - vendor/xml_serialization/Rakefile
39
+ - vendor/xml_serialization/VERSION.yml
40
+ - vendor/xml_serialization/lib/xml_serialization.rb
41
+ - vendor/xml_serialization/test/xml_serialization_test.rb
42
+ - vendor/xml_serialization/xml_serialization.gemspec
43
+ has_rdoc: true
44
+ homepage: http://github.com/samsm/raccept
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.5
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Transforms Ruby objects into xml or json based on accepts headers.
71
+ test_files:
72
+ - test/helper.rb
73
+ - test/test_raccept.rb