durl-rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c35976a924894217ab9f42710a3bdfda50c0a2a6
4
+ data.tar.gz: ab0114a5ee3ec0738b44cf71cc4cf5453e6bac54
5
+ SHA512:
6
+ metadata.gz: eb96535badd1f389ccb451787bc6e89bfaabdc6534c0a6e00bbd7e3085c0082066da64ca9da936b6d841f3d3a97d64783dd35940a242113043b7cba96b72436e
7
+ data.tar.gz: a4243ae9d30727c85047c2030c7a456e12f8a7e21f2b6eb143ec4888991d22c92728906ee055b112c2411ed21720f589d067598eb5d037f6b02dcbdb7ca41a21
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ tmp
3
+ spec/support/*/Gemfile.lock
4
+ spec/support/*/public/javascripts
5
+ .ruby-version
6
+ .bundle
7
+ imports/*
8
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.0.0 (6 August 2016)
2
+
3
+ - well, I started.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,9 @@
1
+ Thanks for helping our good friend dURL!
2
+
3
+ ## Updating dURL
4
+
5
+ If dURL scripts changed, open an issue and submit a pull request
6
+
7
+ ## Tests
8
+
9
+ dURL has its own tests. Not much to test here.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2016 Tyler Kasten
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # durl-rails
2
+
3
+ dURL! For Rails!
4
+
5
+ This gem provides:
6
+
7
+ * dURL
8
+
9
+ See [VERSIONS.md](VERSIONS.md) to see which versions of durl-rails bundle which versions of dURL.
10
+
11
+ ## Versions
12
+
13
+ ```
14
+ patch version bump = updates to durl-rails, and patch-level updates to dURL
15
+ minor version bump = minor-level updates to dURL
16
+ major version bump = major-level updates to dURL and updates to rails which may be backwards-incompatible
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Gemfile
22
+
23
+ ```ruby
24
+ gem "durl-rails"
25
+ ```
26
+
27
+ And run `bundle install`. The rest of the installation depends on whether the asset pipeline is being used.
28
+
29
+ ### Rails 3.1 or greater (with asset pipeline *enabled*)
30
+
31
+ The dURL files will be added to the asset pipeline and available for you to use. add these lines to `app/assets/javascripts/application.js`:
32
+
33
+ ```js
34
+ //= require durl
35
+ ```
36
+
37
+ ### Rails 3.0 (or greater with asset pipeline *disabled*)
38
+
39
+ This gem adds a single generator: `durl:install`. Running the generator will copy dURL to the `public/javascripts` directory.
40
+
41
+ This gem will also hook into the Rails configuration process, adding dURL to the javascript files included by the `javascript_include_tag(:defaults)` call. While this gem contains the minified and un-minified versions of dURL, only the minified versions will be included in the `:defaults` when Rails is run in `production` or `test` mode (un-minified versions will be included when Rails is run in `development` mode).
42
+
43
+ To invoke the generator, run:
44
+
45
+ ```sh
46
+ rails generate durl:install
47
+ ```
48
+
49
+ You're done!
50
+
51
+ ## Contributing
52
+
53
+ Feel free to open an issue ticket if you find something that could be improved.
54
+
55
+ ## Acknowledgements
56
+
57
+ This gem's structure and technique for wrapping a JS library into a rails friendly package is based entirely on jquery-rails. Many thanks are due to all of [the jquery-rails contributors](https://github.com/rails/jquery-rails/graphs/contributors).
58
+
59
+ Copyright Tyler Kasten, released under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ # Check if versions are correct between VERSION constants and .js files
5
+ #
6
+ task :release => [:guard_version]
7
+
8
+ task :guard_version do
9
+ def check_version(file, pattern, constant)
10
+ body = File.read("vendor/assets/javascripts/#{file}")
11
+ match = body.match(pattern) or abort "Version check failed: no pattern matched in #{file}"
12
+ file_version = body.match(pattern)[1]
13
+ constant_version = Durl::Rails.const_get(constant)
14
+
15
+ unless constant_version == file_version
16
+ abort "Durl::Rails::#{constant} was #{constant_version} but it should be #{file_version}"
17
+ end
18
+ end
19
+
20
+ check_version('durl.js', /Version: ([\S]+)/, 'DURL_VERSION')
21
+ end
22
+
23
+ task :update_durl do
24
+ puts "Downloading durl.js"
25
+ puts `curl -o vendor/assets/javascripts/durl.js https://raw.githubusercontent.com/tkasten/durl/master/src/durl.js`
26
+
27
+ puts "Updating version.rb"
28
+ version = false
29
+ File.foreach('vendor/assets/javascripts/durl.js') do |line|
30
+ version = line.match(/Version: ([\S]+)/)
31
+ version = version && version[1]
32
+ break if version
33
+ end
34
+
35
+ version_path = 'lib/durl/rails/version.rb'
36
+ lines = IO.readlines(version_path).map do |line|
37
+ line.gsub(/DURL_VERSION = "([\d\.]+)"/, "DURL_VERSION = \"#{version}\"")
38
+ end
39
+ File.open(version_path, 'w') do |file|
40
+ file.puts lines
41
+ end
42
+
43
+ puts "\e[32mDone!\e[0m"
44
+ end
data/VERSIONS.md ADDED
@@ -0,0 +1,5 @@
1
+ # Bundled Versions
2
+
3
+ | Gem | dURL |
4
+ |--------|--------|
5
+ | 1.0.0 | 1.0.5 |
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/durl/rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "durl-rails"
6
+ s.version = Durl::Rails::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Tyler Kasten"]
9
+ s.email = ["tyler.kasten@gmail.com"]
10
+ s.homepage = "https://github.com/tkasten/durl-rails"
11
+ s.summary = "Use dURL with Rails 3+"
12
+ s.description = "This gem provides dURL for your Rails 3+ application."
13
+ s.license = "MIT"
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rubyforge_project = "durl-rails"
17
+
18
+ s.add_dependency "railties", ">= 3.0", "< 5.0"
19
+ s.add_dependency "thor", ">= 0.14", "< 2.0"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
23
+ s.require_path = 'lib'
24
+ end
@@ -0,0 +1,6 @@
1
+ module Durl
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Durl
2
+ module Rails
3
+ VERSION = "1.0.0"
4
+ DURL_VERSION = "1.0.5"
5
+ end
6
+ end
data/lib/durl/rails.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'durl/rails/engine' if ::Rails.version >= '3.1'
2
+ require 'durl/rails/version'
3
+
4
+ module Durl
5
+ module Rails
6
+ end
7
+ end
data/lib/durl-rails.rb ADDED
@@ -0,0 +1 @@
1
+ require 'durl/rails'
@@ -0,0 +1,37 @@
1
+ require 'rails'
2
+
3
+ # Supply generator for Rails 3.0.x or if asset pipeline is not enabled
4
+ if ::Rails.version < "3.1" || !::Rails.application.config.assets.enabled
5
+ module Durl
6
+ module Generators
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+
9
+ desc "This generator installs dURL #{Durl::Rails::DURL_VERSION}"
10
+ source_root File.expand_path('../../../../../vendor/assets/javascripts', __FILE__)
11
+
12
+ def copy_durl
13
+ say_status("copying", "dURL (#{Durl::Rails::DURL_VERSION})", :green)
14
+ copy_file "durl.js", "public/javascripts/durl.js"
15
+ copy_file "durl.min.js", "public/javascripts/durl.min.js"
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ else
22
+ module Durl
23
+ module Generators
24
+ class InstallGenerator < ::Rails::Generators::Base
25
+ desc "Just show instructions so people will know what to do when mistakenly using generator for Rails 3.1 apps"
26
+
27
+ def do_nothing
28
+ say_status("deprecated", "You are using Rails 3.1 with the asset pipeline enabled, so this generator is not needed.")
29
+ say_status("", "The necessary files are already in your asset pipeline.")
30
+ say_status("", "Just add `//= require durl` to your app/assets/javascripts/application.js")
31
+ say_status("", "If you do not want the asset pipeline enabled, you may turn it off in application.rb and re-run this generator.")
32
+ # ok, nothing
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,153 @@
1
+ /*
2
+ * File: durl.js
3
+ * Version: 1.0.5
4
+ * Desc: DURL keeps your i-frame's Deep URL stored in the hash fragment of your parent page
5
+ * Doc: https://github.com/tkasten/durl
6
+ * Author: Tyler Kasten tyler.kasten@gmail.com
7
+ */
8
+
9
+ function Durl(options){
10
+ this.options = options || {}
11
+ this.deep_url_var_name = this.options.deep_url_var_name || "durl"
12
+ this.window = this.options.window
13
+ this.pm = this.options.postMassage
14
+
15
+ if(!this.pm){
16
+ this.pm = new PostMassage({namespace: 'durl', window: this.window})
17
+ }
18
+
19
+ this.options.producer ? this.bootAsProducer() : this.bootAsConsumer()
20
+ }
21
+
22
+ Durl.prototype.bootAsProducer = function(){
23
+ if(window != parent.window){
24
+ this.log("booting: " + window.location.href)
25
+ this.pm.bind('setDURLFromConsumer', this.setDURLFromConsumer.bind(this))
26
+ this.pm.call('setDURL', encodeURIComponent(window.location.href))
27
+ var self = this
28
+ document.addEventListener("DOMContentLoaded", function(){
29
+ if("onhashchange" in window.document.body){
30
+ window.addEventListener("hashchange", function(){
31
+ self.pm.call('setDURL', encodeURIComponent(window.location.href))
32
+ })
33
+ }
34
+ })
35
+ }else{
36
+ this.log("refusing to boot, no parent window detected")
37
+ }
38
+ },
39
+
40
+ Durl.prototype.bootAsConsumer = function(){
41
+ this.log("booting")
42
+ this.pm.bind('setDURL', this.setDURL.bind(this))
43
+ if("onhashchange" in window.document.body){
44
+ var self = this
45
+ window.addEventListener("hashchange", function(){
46
+ var durl = self.getDeepPath()
47
+ self.log('sendDURLFromConsumer: ' + durl)
48
+ self.pm.call('setDURLFromConsumer', durl)
49
+ })
50
+ }
51
+ }
52
+
53
+ Durl.prototype.matchDeepPath = function(path) {
54
+ /**
55
+ *
56
+ * DURL will store its durl (deep url) in the consumer's hash fragment in the
57
+ * form of `this.deep_url_var_name=url_encoded_url`
58
+ *
59
+ * But we don't know how the consumer is using their hash fragment so we try
60
+ * to make safe, unobtrusive assumptions about how to integrate our data into
61
+ * their fragment. Consider the following potential use cases on their end:
62
+ *
63
+ * TKK TODO - spec these cases (also need more cases/clean-up etc)...
64
+ *
65
+ * Hash Bang style followed with traditional path and url variables
66
+ * www.example.com/page/path?var1=stuff#!/some/path?p1=v1&durl=value!&p2=v2z
67
+ * www.example.com/page/path?var1=stuff#!/some/path?p1=v1&durl=value!
68
+ *
69
+ * Same as above case but no consumer variables mixed with durl
70
+ * www.example.com/page/path?vars=stuff#!/some/path?durl=value!
71
+ *
72
+ * No Bang(!) just right into the variables
73
+ * www.example.com/page/path#?p1=v2&durl=value!
74
+ *
75
+ * Same as above but no consumer variables mixed with durl
76
+ * www.example.com/page/path#?durl=value!
77
+ *
78
+ */
79
+
80
+ pattern = new RegExp("(\\?|&)" + this.deep_url_var_name + "=([^&\n]*)")
81
+ return pattern.exec(this.vanillaHash()) || '' // [whole match, joiner, url]
82
+ }
83
+
84
+ Durl.prototype.setDURL = function(new_url) {
85
+ new_url = new_url || ''
86
+ hash = this.vanillaHash()
87
+
88
+ if(matches = this.matchDeepPath()){
89
+ if(matches[2] == new_url){
90
+ return
91
+ }else{
92
+ whole = matches[0]
93
+ joiner = matches[1]
94
+ hash = hash.replace(whole, joiner + this.deep_url_var_name + "=" + new_url)
95
+ }
96
+ }else{
97
+ hash += hash.indexOf('?') == -1 ? '?' : '&'
98
+ hash += this.deep_url_var_name + "=" + new_url
99
+ }
100
+
101
+ this.log('replacing location: ' + hash)
102
+ location.replace('#' + hash)
103
+ }
104
+
105
+ Durl.prototype.getDeepPath = function() {
106
+ if(matches = this.matchDeepPath())
107
+ return decodeURIComponent(matches[2])
108
+ else
109
+ return ''
110
+ }
111
+
112
+ Durl.prototype.setDURLFromConsumer = function(path) {
113
+ if(window.location.href != path){
114
+ this.log('setDURLFromConsumer: ' + path)
115
+ // this sillyness is in response to receiving a new path from the consumer
116
+ // that is a double-encoded relative path (has leading encoded '/'). It's
117
+ // already been decoded once before getting here, and if we still see what
118
+ // looks like an encoded forward flash, let's decode it again.
119
+ // Discovered b/c react-router (v 0.11.6) has a double encoding bug:
120
+ // https://github.com/petehunt/react-router-1/blob/master/CHANGELOG.md
121
+ if(path.substr(0,3) == "%2F"){
122
+ path = decodeURIComponent(path)
123
+ }
124
+ window.location.replace(path)
125
+ }else{
126
+ this.log('refusing to change frame location, path is unchanged: ' + path)
127
+ }
128
+ }
129
+
130
+ Durl.prototype.vanillaHash = function() {
131
+ // Note that we don't use location.hash because firefox alone decodes it on read.
132
+ // See http://stackoverflow.com/questions/1703552/encoding-of-window-location-hash
133
+ return location.href.split("#")[1] || ''
134
+ }
135
+
136
+ Durl.prototype.log = function(message){
137
+ if(this.options.logging && window.console){
138
+ loggerId = this.options.producer ? 'producer' : 'consumer'
139
+ console.log('[' + loggerId + '] ' + message)
140
+ }
141
+ }
142
+
143
+ function factory(){
144
+ return Durl
145
+ }
146
+
147
+ if (typeof define === 'function' && define.amd) {
148
+ define([],factory)
149
+ } else if (typeof module === 'object' && typeof module.exports === 'object') { //Node for browserfy
150
+ module.exports = factory()
151
+ } else {
152
+ window.Durl = window.Durl || factory()
153
+ }
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: durl-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Kasten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: thor
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0.14'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0.14'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ description: This gem provides dURL for your Rails 3+ application.
54
+ email:
55
+ - tyler.kasten@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - CHANGELOG.md
62
+ - CONTRIBUTING.md
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - VERSIONS.md
68
+ - durl-rails.gemspec
69
+ - lib/durl-rails.rb
70
+ - lib/durl/rails.rb
71
+ - lib/durl/rails/engine.rb
72
+ - lib/durl/rails/version.rb
73
+ - lib/generators/durl/install/install_generator.rb
74
+ - vendor/assets/javascripts/durl.js
75
+ homepage: https://github.com/tkasten/durl-rails
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+ rubyforge_project: durl-rails
95
+ rubygems_version: 2.4.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Use dURL with Rails 3+
99
+ test_files: []