jsonp 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,23 @@
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
22
+ *.gem
23
+ *.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jakub Kuźma
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.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = jsonp
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
10
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
11
+ * Send me a pull request. Bonus points for topic branches.
12
+
13
+ == Copyright
14
+
15
+ Copyright (c) 2009 Jakub Kuźma. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "jsonp"
10
+ gem.summary = %Q{JSONP helpers}
11
+ gem.description = %Q{JSONP helpers}
12
+ gem.email = "qoobaa+github@gmail.com"
13
+ gem.homepage = "http://github.com/qoobaa/jsonp"
14
+ gem.authors = ["Jakub Kuźma"]
15
+ gem.add_development_dependency "test-unit", ">= 2.0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "jsonp #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,24 @@
1
+ module Jsonp
2
+
3
+ # Wraps JSONP response with appropriate callback.
4
+ class CallbackWrapper
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ @callback_param = options.fetch(:callback_param, "_callback")
8
+ end
9
+
10
+ def call(env)
11
+ status, headers, response = @app.call(env)
12
+ callback = Utils.extract_callback(env)
13
+
14
+ if callback
15
+ prefix, suffix = "#{callback}(", ")"
16
+ response = Utils::Wrapper.new(prefix, response, suffix)
17
+ headers["Content-Length"] = (prefix.bytesize + headers["Content-Length"].to_i + suffix.bytesize).to_s
18
+ end
19
+
20
+ [status, headers, response]
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+ module Jsonp
2
+
3
+ # Allows overwriting of HTTP method also in JSONP GET HTTP requests.
4
+ class MethodOverride
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ @callback_param = options.fetch(:callback_param, "_callback")
8
+ @method_param = options.fetch(:method_param, "_method")
9
+ end
10
+
11
+ def call(env)
12
+ callback = Utils.extract_callback(env)
13
+ method = Utils.extract_method(env)
14
+
15
+ if env["REQUEST_METHOD"] == "GET" && callback && method
16
+ env["REQUEST_METHOD"] = method
17
+ end
18
+
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,37 @@
1
+ module Jsonp
2
+
3
+ # Wraps JSONP response with HTTP status code and message. If the
4
+ # response contains no JSON data, it's replaced by JSON status and
5
+ # raises no error in the browser.
6
+ class StatusWrapper
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @callback_param = options.fetch(:callback_param, "_callback")
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, response = @app.call(env)
14
+ callback = Utils.extract_callback(env, @callback_param)
15
+
16
+ if callback && ((200...300).include?(status.to_i) || (400...600).include?(status.to_i))
17
+ type = Utils.status_message(status)
18
+ prefix, suffix = %Q({"status":#{status},"type":"#{type}"), "}"
19
+
20
+ if headers["Content-Type"] =~ %r{^(?:application/json|text/json|text/x-json)}
21
+ prefix << %Q(,"content":)
22
+ response = Utils::Wrapper.new(prefix, response, suffix)
23
+ headers["Content-Length"] = (prefix.bytesize + headers["Content-Length"].to_i + suffix.bytesize).to_s
24
+ else
25
+ headers["Content-Type"] = "application/json"
26
+ response = [prefix, suffix]
27
+ headers["Content-Length"] = (prefix.bytesize + suffix.bytesize).to_s
28
+ end
29
+
30
+ status = 200
31
+ end
32
+
33
+ [status, headers, response]
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,17 @@
1
+ module Jsonp
2
+ module Utils
3
+
4
+ class Wrapper #:nodoc:
5
+ def initialize(prefix, body, suffix)
6
+ @prefix, @body, @suffix = prefix, body, suffix
7
+ end
8
+
9
+ def each(&block)
10
+ block.call(@prefix)
11
+ @body.each(&block)
12
+ block.call(@suffix)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,79 @@
1
+ module Jsonp
2
+ module Utils #:nodoc:
3
+
4
+ # Defines the standard HTTP status codes, by integer, with their
5
+ # corresponding default message texts.
6
+ # Source: http://www.iana.org/assignments/http-status-codes
7
+ STATUS_CODES = {
8
+ 100 => "Continue",
9
+ 101 => "Switching Protocols",
10
+ 102 => "Processing",
11
+
12
+ 200 => "OK",
13
+ 201 => "Created",
14
+ 202 => "Accepted",
15
+ 203 => "Non-Authoritative Information",
16
+ 204 => "No Content",
17
+ 205 => "Reset Content",
18
+ 206 => "Partial Content",
19
+ 207 => "Multi-Status",
20
+ 226 => "IM Used",
21
+
22
+ 300 => "Multiple Choices",
23
+ 301 => "Moved Permanently",
24
+ 302 => "Found",
25
+ 303 => "See Other",
26
+ 304 => "Not Modified",
27
+ 305 => "Use Proxy",
28
+ 307 => "Temporary Redirect",
29
+
30
+ 400 => "Bad Request",
31
+ 401 => "Unauthorized",
32
+ 402 => "Payment Required",
33
+ 403 => "Forbidden",
34
+ 404 => "Not Found",
35
+ 405 => "Method Not Allowed",
36
+ 406 => "Not Acceptable",
37
+ 407 => "Proxy Authentication Required",
38
+ 408 => "Request Timeout",
39
+ 409 => "Conflict",
40
+ 410 => "Gone",
41
+ 411 => "Length Required",
42
+ 412 => "Precondition Failed",
43
+ 413 => "Request Entity Too Large",
44
+ 414 => "Request-URI Too Long",
45
+ 415 => "Unsupported Media Type",
46
+ 416 => "Requested Range Not Satisfiable",
47
+ 417 => "Expectation Failed",
48
+ 422 => "Unprocessable Entity",
49
+ 423 => "Locked",
50
+ 424 => "Failed Dependency",
51
+ 426 => "Upgrade Required",
52
+
53
+ 500 => "Internal Server Error",
54
+ 501 => "Not Implemented",
55
+ 502 => "Bad Gateway",
56
+ 503 => "Service Unavailable",
57
+ 504 => "Gateway Timeout",
58
+ 505 => "HTTP Version Not Supported",
59
+ 507 => "Insufficient Storage",
60
+ 510 => "Not Extended"
61
+ }
62
+
63
+ HTTP_METHODS = %w(GET PUT POST DELETE)
64
+
65
+ def self.extract_callback(env, callback_param)
66
+ Rack::Utils.parse_query(env["QUERY_STRING"])[callback_param]
67
+ end
68
+
69
+ def self.extract_method(env, method_param)
70
+ method = Rack::Utils.parse_query(env["QUERY_STRING"])[method_param]
71
+ method if HTTP_METHODS.include?(method.to_s.upcase)
72
+ end
73
+
74
+ def self.status_message(status_code)
75
+ STATUS_CODES[status_code.to_i]
76
+ end
77
+
78
+ end
79
+ end
data/lib/jsonp.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "jsonp/utils"
2
+ require "jsonp/utils/wrapper"
3
+
4
+ require "jsonp/callback_wrapper"
5
+ require "jsonp/method_override"
6
+ require "jsonp/status_wrapper"
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'jsonp'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'helper'
2
+
3
+ class TestJsonp < Test::Unit::TestCase
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - "Jakub Ku\xC5\xBAma"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-06 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: test-unit
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ version:
25
+ description: JSONP helpers
26
+ email: qoobaa+github@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/jsonp.rb
42
+ - lib/jsonp/callback_wrapper.rb
43
+ - lib/jsonp/method_override.rb
44
+ - lib/jsonp/status_wrapper.rb
45
+ - lib/jsonp/utils.rb
46
+ - lib/jsonp/utils/wrapper.rb
47
+ - test/helper.rb
48
+ - test/test_jsonp.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/qoobaa/jsonp
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: JSONP helpers
77
+ test_files:
78
+ - test/test_jsonp.rb
79
+ - test/helper.rb