xdomain-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: aabb3d0316915e2568b0570f28ddb8b717b347ab
4
+ data.tar.gz: 95a98c54896fd63983f12a42eee6792b4c16215d
5
+ SHA512:
6
+ metadata.gz: 078fdf4317362d0a9e632d60b2f1d792f147806236802412d255a64b4c07da235b7225f0e5bae1de20b4f95d027b4429111acdea7c758f30c052a92f706d78c0
7
+ data.tar.gz: b5c6ec59e2525b7be0f923e6e607a5b16d18df5c62b3b6e4ab849ac01958df474e18a2b6969e488a82088efeba2896e50698b552fa5ce25c76996b7fb2cc25fb
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ tmp
2
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in foundation-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Lucjan Suski lucjansuski at gmail
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # XDomain Rails
2
+
3
+ XDomain Rails is a gem which simplifies usage of
4
+ [XDomain](https://github.com/jpillora/xdomain) within Rails application.
5
+
6
+ ## Installation
7
+
8
+ Add following line to your application's Gemfile:
9
+
10
+ $ gem 'xdomain-rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ ## Usage
17
+
18
+ The gem provides very simple API.
19
+
20
+ Consulting [XDomain README](https://github.com/jpillora/xdomain)
21
+ before using this gem is a good idea.
22
+
23
+ I highly recommend and assume that you are using
24
+ [dotenv-rails](https://github.com/bkeepers/dotenv).
25
+
26
+ Let's say we want to configure http://master.example.com
27
+ (the domain which you are making requests from) as master domain
28
+ and http://slave.example.com (which receives requests) as slave domain.
29
+
30
+ ### Slave domain
31
+
32
+ Add following line to your routes:
33
+
34
+ ```ruby
35
+ mount XdomainRails::Engine, at: '/xdomain'
36
+ ```
37
+
38
+ Add following ENV variable to your .env:
39
+
40
+ ```
41
+ XDOMAIN_MASTERS=http://master.example.com
42
+ ```
43
+
44
+ Note: you can also limit scope, use wildcards and add few domains,
45
+ consult [configuration section](#configuration) for more informations:
46
+ ```
47
+ XDOMAIN_MASTERS=*.example.com/api/*.json *.another.com
48
+ ```
49
+
50
+ ### Master domain
51
+
52
+ Add following line **before** any other Javascripts ([why?](https://github.com/jpillora/xdomain#quick-usage))
53
+ to your main application layout:
54
+
55
+ ```erb
56
+ <%= xdomain_slaves %>
57
+ ```
58
+
59
+ Add following ENV variable:
60
+
61
+ ```
62
+ XDOMAIN_SLAVES='["http://slave.example.com"]'
63
+ ```
64
+
65
+ From now on, XHR requests will automagically work:
66
+
67
+ ```javascript
68
+ $.get('http://slave.example.com/secret.json')
69
+ ```
70
+
71
+ ## Configuration
72
+
73
+ More sophisticated configuration is supported.
74
+
75
+ By default, gem will load domain configuration as JSON
76
+ from following ENV variables:
77
+
78
+ ```
79
+ XDOMAIN_MASTERS='{"http://a.example.com": "*", "http://*.b.com": "api/*"}'
80
+ XDOMAIN_SLAVES='["http://first.example.com", "http://another.example.com/another/proxy"]'
81
+ ```
82
+
83
+ As in XDomain, you can use wildcards in your master domains setup.
84
+
85
+ To change path for proxy, which by default is `/xdomain/proxy`
86
+ you can just add it in ENV variable:
87
+
88
+ ```
89
+ XDOMAIN_SLAVES=http://slave.example.com/custom/proxy
90
+ ```
91
+
92
+ If you don't like having configuration in ENV variables,
93
+ or you want to customize variable names, just create file
94
+ `config/initializers/xdomain_rails.rb` (**server restart required**):
95
+
96
+ ```ruby
97
+ Xdomain.configure do |config|
98
+ # path to default xdomain proxy
99
+ # default: '/xdomain/proxy'
100
+
101
+ # config.proxy_path =
102
+
103
+ # hash / json string with master domains
104
+ # default: ENV['XDOMAIN_MASTERS']
105
+
106
+ # config.master_domains =
107
+
108
+ # array / json string of slave domains
109
+ # default: ENV['XDOMAIN_SLAVES']
110
+
111
+ # config.slave_domains =
112
+ end
113
+ ```
114
+
115
+ ### Helpers
116
+
117
+ Both `xdomain_slaves` and `xdomain_masters` helpers are available.
118
+
119
+ You can pass domains as an argument:
120
+
121
+ ```erb
122
+ <%= xdomain_slaves ['http://slave.example.com'] %>
123
+ <%= xdomain_masters {'http://master.example.com': '/*.json'] %>
124
+ ```
125
+
126
+ ## Changelog
127
+
128
+ #### v1.0.0
129
+ - first version
130
+ - XDomain 0.6.15
131
+
132
+ ## Tests
133
+
134
+ [WIP] :grinning:
135
+
136
+ ## Contributing
137
+
138
+ - Fork it (http://github.com/methyl/xdomain-rails/fork)
139
+ - Create your feature branch (git checkout -b my-new-feature)
140
+ - Commit your changes (git commit -am 'Add some feature')
141
+ - Push to the branch (git push origin my-new-feature)
142
+ - Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ module XdomainRails
2
+ class XdomainController < ApplicationController
3
+ layout false
4
+
5
+ def proxy
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ module XdomainRails
2
+ module XdomainHelper
3
+ def xdomain_masters(domains=nil)
4
+ domains = (domains || master_domains).to_json.html_safe
5
+ javascript_include_tag("xdomain") + javascript_tag("xdomain.masters(#{domains})")
6
+ end
7
+
8
+ def xdomain_slaves(domains=nil)
9
+ domains = (domains || slave_domains).to_json.html_safe
10
+ javascript_include_tag("xdomain") + javascript_tag("xdomain.slaves(#{domains})")
11
+ end
12
+
13
+ private
14
+
15
+ def config
16
+ Rails.configuration.xdomain
17
+ end
18
+
19
+ def master_domains
20
+ config.master_domains
21
+ end
22
+
23
+ def slave_domains
24
+ config.slave_domains
25
+ end
26
+ end
27
+ end
@@ -0,0 +1 @@
1
+ <%= xdomain_masters %>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ XdomainRails::Engine.routes.draw do
2
+ get 'proxy' => 'xdomain#proxy'
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'xdomain_rails/engine'
4
+ require 'xdomain_rails/config'
5
+ require 'xdomain_rails/version'
6
+
7
+ module XdomainRails
8
+ def self.config
9
+ @config ||= Config.new
10
+ end
11
+
12
+ def self.configure
13
+ yield(config)
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require 'uri'
2
+
3
+ module XdomainRails
4
+ class Config
5
+ attr_reader :master_domains, :slave_domains
6
+ attr_accessor :proxy_path
7
+
8
+ def initialize
9
+ self.proxy_path = '/xdomain/proxy'
10
+ self.master_domains = ENV["XDOMAIN_MASTERS"]
11
+ self.slave_domains = ENV["XDOMAIN_SLAVES"]
12
+ end
13
+
14
+ def master_domains=(domains)
15
+ domains = parse_domains domains
16
+ @master_domains = domains if domains.any?
17
+ end
18
+
19
+ def slave_domains=(domains)
20
+ domains = domains_to_hash parse_domains(domains)
21
+ @slave_domains = domains if domains.any?
22
+ end
23
+
24
+ private
25
+
26
+ def parse_domains(domains)
27
+ (domains.is_a?(String) ? JSON.parse(domains) : domains) || []
28
+ end
29
+
30
+ def domains_to_hash(domains)
31
+ domains.inject({}) { |result, domain|
32
+ path = URI(domain).path
33
+ if path.present?
34
+ result[domain.sub(path, '')] = path
35
+ else
36
+ result[domain] = proxy_path
37
+ end
38
+ result
39
+ }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ module XdomainRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace XdomainRails
4
+
5
+ initializer "xdomain_rails.config" do |app|
6
+ app.config.xdomain = XdomainRails.config
7
+ end
8
+
9
+ initializer "xdomain_rails.assets.precompile" do |app|
10
+ app.config.assets.precompile += %w(xdomain.js)
11
+ end
12
+
13
+ initializer 'xdomain_rails.action_controller' do |app|
14
+ ActiveSupport.on_load :action_controller do
15
+ helper XdomainRails::XdomainHelper
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module XdomainRails
2
+ VERSION = "1.0.0"
3
+ end