js_message 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.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.md +25 -0
- data/Rakefile +2 -0
- data/js/jquery.js_message.js +70 -0
- data/js_message.gemspec +19 -0
- data/lib/js_message/controller_methods.rb +47 -0
- data/lib/js_message/railtie.rb +18 -0
- data/lib/js_message/tasks.rake +20 -0
- data/lib/js_message/version.rb +3 -0
- data/lib/js_message.rb +3 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
jsMessage
|
2
|
+
=========
|
3
|
+
|
4
|
+
jsMessage standardizes javascript interaction between the front end (html) and a rails application.
|
5
|
+
|
6
|
+
See this blog post: http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery
|
7
|
+
|
8
|
+
Installation
|
9
|
+
------------
|
10
|
+
|
11
|
+
In your Rails 3 <code>Gemfile</code> add:
|
12
|
+
|
13
|
+
gem "js_message"
|
14
|
+
|
15
|
+
Then run the following rake task and follow the instructions:
|
16
|
+
|
17
|
+
rake js_message:install
|
18
|
+
|
19
|
+
You're all set!
|
20
|
+
|
21
|
+
Author
|
22
|
+
======
|
23
|
+
|
24
|
+
Peter Bui
|
25
|
+
peter@paydrotalks.com
|
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
(function($){
|
2
|
+
// Checks that a console arg is available before logging to it
|
3
|
+
$.log = function(msg){
|
4
|
+
if(window.console && DEBUG){
|
5
|
+
console.log(msg);
|
6
|
+
}
|
7
|
+
};
|
8
|
+
|
9
|
+
// Appends the ".jsm" to a URL. It will not append the extension
|
10
|
+
// if the URL already has it.
|
11
|
+
$.appendJSMExt = function(url){
|
12
|
+
if(url.indexOf(".jsm") !== -1){
|
13
|
+
return url;
|
14
|
+
}
|
15
|
+
|
16
|
+
var parts = url.split("?");
|
17
|
+
var request = parts[0];
|
18
|
+
var params = parts[1];
|
19
|
+
|
20
|
+
var jsmUrl = request + ".jsm";
|
21
|
+
if(params){
|
22
|
+
jsmUrl += "?" + params;
|
23
|
+
}
|
24
|
+
|
25
|
+
return jsmUrl;
|
26
|
+
};
|
27
|
+
|
28
|
+
// Send a standard structured JSON message to the server.
|
29
|
+
$.jsMessage = function(options){
|
30
|
+
var validRespFunc = options.success || function(jsonResponse){
|
31
|
+
$.log("Valid response returned");
|
32
|
+
$.log(jsonResponse);
|
33
|
+
};
|
34
|
+
|
35
|
+
var invalidRespFunc = options.error || function(jsonResponse){
|
36
|
+
$.log("Invalid response returned");
|
37
|
+
$.log(jsonResponse);
|
38
|
+
};
|
39
|
+
|
40
|
+
options.success = function(jsonResponse){
|
41
|
+
if(jsonResponse.status == "redirect"){
|
42
|
+
window.location = jsonResponse.to;
|
43
|
+
}
|
44
|
+
else{
|
45
|
+
validRespFunc(jsonResponse);
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
options.error = function(XMLHttpRequest, textStatus){
|
50
|
+
if(XMLHttpRequest.status == 400){
|
51
|
+
jsonResponse = $.parseJSON(XMLHttpRequest.responseText);
|
52
|
+
invalidRespFunc(jsonResponse);
|
53
|
+
}
|
54
|
+
else{
|
55
|
+
$.log("Request error: " + textStatus);
|
56
|
+
$.log(XMLHttpRequest);
|
57
|
+
}
|
58
|
+
};
|
59
|
+
|
60
|
+
options.dataType = "json";
|
61
|
+
options.type = options.type || "POST";
|
62
|
+
options.url = $.appendJSMExt(options.url);
|
63
|
+
|
64
|
+
$.ajax(options);
|
65
|
+
};
|
66
|
+
|
67
|
+
})(jQuery);
|
68
|
+
|
69
|
+
|
70
|
+
|
data/js_message.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "js_message/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "js_message"
|
7
|
+
s.version = JsMessage::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Peter Bui"]
|
10
|
+
s.email = ["peter@paydrotalks.com"]
|
11
|
+
s.homepage = "http://github.com/paydro/js_message"
|
12
|
+
s.summary = %q{Pass structured json messages between html and a rails application}
|
13
|
+
s.description = %q{Pass structured json messages between html and a rails application}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module JsMessage
|
2
|
+
module ControllerMethods
|
3
|
+
# Helper to render the js_message response.
|
4
|
+
#
|
5
|
+
# Pass in a type of message (:ok, :error, :redirect) and any other
|
6
|
+
# data you need. Default data attributes in the response are:
|
7
|
+
#
|
8
|
+
# * html
|
9
|
+
# * message
|
10
|
+
# * to (used for redirects)
|
11
|
+
#
|
12
|
+
# Examples:
|
13
|
+
#
|
14
|
+
# # Send a successful response with some html
|
15
|
+
# render_js_message :ok, :html => "<p>It worked!</p>"
|
16
|
+
#
|
17
|
+
# # Send a redirect
|
18
|
+
# render_js_message :redirect, :to => "http://www.google.com"
|
19
|
+
#
|
20
|
+
# # Send an error response with a message
|
21
|
+
# render_js_message :error, :message => "Something broke!"
|
22
|
+
#
|
23
|
+
# Of course, if you don't need other data sent back in the response
|
24
|
+
# this works as well:
|
25
|
+
#
|
26
|
+
# render_js_message :ok
|
27
|
+
#
|
28
|
+
def render_js_message(type, hash = {})
|
29
|
+
unless [ :ok, :redirect, :error ].include?(type)
|
30
|
+
raise "Invalid js_message response type: #{type}"
|
31
|
+
end
|
32
|
+
|
33
|
+
js_message = {
|
34
|
+
:status => type,
|
35
|
+
:html => nil,
|
36
|
+
:message => nil,
|
37
|
+
:to => nil
|
38
|
+
}.merge(hash)
|
39
|
+
|
40
|
+
render_options = {:json => js_message}
|
41
|
+
render_options[:status] = 400 if type == :error
|
42
|
+
|
43
|
+
render(render_options)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'js_message'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module JsMessage
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "js_message.initializer" do |app|
|
7
|
+
ActionController::Base.class_eval do
|
8
|
+
include JsMessage::ControllerMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
Mime::Type.register_alias "application/json", :jsm
|
12
|
+
end
|
13
|
+
|
14
|
+
rake_tasks do
|
15
|
+
load "js_message/tasks.rake"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
namespace :js_message do
|
2
|
+
desc "Install the jquery.js_message.js into your public/javascripts dir"
|
3
|
+
task :install do
|
4
|
+
gemdir = File.join(File.dirname(__FILE__), "..", "..",)
|
5
|
+
print "\n Copying 'jquery.js_message.js' to rails' public/javascripts dir ... "
|
6
|
+
FileUtils.cp(
|
7
|
+
File.join(gemdir, "js", "jquery.js_message.js"),
|
8
|
+
Rails.root.join("public", "javascripts")
|
9
|
+
)
|
10
|
+
puts "Done."
|
11
|
+
|
12
|
+
puts %q[
|
13
|
+
Now just add the following lines into your application's layout files:
|
14
|
+
|
15
|
+
<%= javascript_include_tag "jquery.js_message.js" %>
|
16
|
+
|
17
|
+
And you're all set!
|
18
|
+
]
|
19
|
+
end
|
20
|
+
end
|
data/lib/js_message.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js_message
|
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
|
+
- Peter Bui
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-14 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Pass structured json messages between html and a rails application
|
22
|
+
email:
|
23
|
+
- peter@paydrotalks.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- js/jquery.js_message.js
|
36
|
+
- js_message.gemspec
|
37
|
+
- lib/js_message.rb
|
38
|
+
- lib/js_message/controller_methods.rb
|
39
|
+
- lib/js_message/railtie.rb
|
40
|
+
- lib/js_message/tasks.rake
|
41
|
+
- lib/js_message/version.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/paydro/js_message
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Pass structured json messages between html and a rails application
|
74
|
+
test_files: []
|
75
|
+
|