restpack_web 0.0.5 → 0.2.17
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.
- checksums.yaml +14 -6
- data/.gitignore +1 -1
- data/Gemfile +0 -2
- data/LICENSE +22 -0
- data/README.md +29 -2
- data/Rakefile +39 -2
- data/lib/restpack_web/app.rb +67 -0
- data/lib/restpack_web/context.rb +90 -53
- data/lib/restpack_web/rails/controller.rb +13 -9
- data/lib/restpack_web/sinatra/app.rb +16 -9
- data/lib/restpack_web/test_support.rb +26 -0
- data/lib/restpack_web/version.rb +2 -2
- data/lib/restpack_web.rb +1 -6
- data/restpack_web.gemspec +24 -23
- data/spec/fixtures/channels.json +354 -0
- data/spec/fixtures/configurations_root.json +59 -0
- data/spec/fixtures/domains_search.json +40 -0
- data/spec/fixtures/users.json +14 -0
- data/spec/lib/restpack_web_spec.rb +39 -0
- data/spec/lib/test_support_spec.rb +23 -0
- data/spec/spec_helper.rb +8 -0
- metadata +94 -55
- data/LICENSE.txt +0 -22
- data/lib/restpack_web/configuration.rb +0 -16
- data/lib/restpack_web/rack/domain.rb +0 -36
- data/lib/restpack_web/rack/session.rb +0 -7
- data/lib/restpack_web/rack/user.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmM0NmRhNjIyNjI2MzMzNTkzMmNkZDAzZTk5ZTJmYzk3OWY2NDhjZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZDUxNjQyMzE0M2FhODlkM2Y2MDRiZGRlNzYxNThjZDQ2Mzg0ZjlhZQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjkxMWE1MTlkYjg0YTIyYWUyMTRlZGIyMGNmMmY2N2EwNzE2M2Y0MDdhYWMy
|
10
|
+
MTNkYWVlNjE3MTQ2Mjk2ODYwM2NiMTdiZDVjOWY1ZWI0ZjQ0YTU4MTQyZGM1
|
11
|
+
YjdhN2QzNjY4M2VkN2EwMjNiZjExNzQxMjk4NmEyYzA2NDc4MWQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MzU2ZGFjZDU4Mzc5NDk3ZDc5ZGZjM2RmZGMwOTUwNGQxOWNjMzA3MWUzOGEy
|
14
|
+
OTQ3MzgyYzY5NWQ4YTk4MWI4MTc3YzUyNTVhMWZmMGQxN2M4Mjc1NjIxNGQ4
|
15
|
+
YmNhZWI1MjJhNDY4ZjUzYzJlMzE2MWI2ZmVjYzYwMDg4ODYyM2M=
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
-----------
|
3
|
+
|
4
|
+
Copyright (c) 2013 Gavin Joyce and RestPack contributors
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
8
|
+
the Software without restriction, including without limitation the rights to
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
10
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
11
|
+
so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,3 +1,30 @@
|
|
1
|
-
|
1
|
+
## Install in Rails 3
|
2
|
+
Add `restpack-web` to Gemfile
|
3
|
+
```ruby
|
4
|
+
gem 'restpack-web', '~> 0.2.16', :git => 'git://github.com/RestPack/restpack-web.git'
|
5
|
+
```
|
6
|
+
|
7
|
+
Use `RestPack::Web::App` as a middleware in `application.rb`
|
8
|
+
```ruby
|
9
|
+
config.middleware.use RestPack::Web::App
|
10
|
+
```
|
11
|
+
|
12
|
+
Set necessary environment variables:
|
13
|
+
```ruby
|
14
|
+
ENV['RESTPACK_ACCESS_KEY'] = 'secret'
|
15
|
+
ENV['RESTPACK_CORE_SERVICE'] = 'restpack-core-service.restpack.org'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Testing your app
|
19
|
+
You can stub all requests from restpack_web to the core service.
|
20
|
+
```ruby
|
21
|
+
require 'restpack_web/test_support'
|
22
|
+
RestPack::Web::TestSupport.stub_restpack_web
|
23
|
+
```
|
24
|
+
|
25
|
+
## Testing
|
26
|
+
`bundle exec rake test`
|
27
|
+
|
28
|
+
## Bump to a new version
|
29
|
+
`bundle exec bump patch && rake tag`
|
2
30
|
|
3
|
-
Rack middleware for your RestPack web apps
|
data/Rakefile
CHANGED
@@ -1,2 +1,39 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'bump/tasks'
|
4
|
+
require_relative 'lib/restpack_web/version'
|
5
|
+
|
6
|
+
task :default => [:test]
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.pattern = 'spec/**/*_spec.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :gem do
|
13
|
+
["gem:bump", "gem:tag", "gem:build", "gem:push"].each do |task|
|
14
|
+
Rake::Task[task].reenable
|
15
|
+
Rake::Task[task].invoke
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
namespace :gem do
|
20
|
+
task :build do
|
21
|
+
sh "gem build restpack_web.gemspec"
|
22
|
+
end
|
23
|
+
|
24
|
+
task :push do
|
25
|
+
require 'bump'
|
26
|
+
sh "gem push restpack_web-#{Bump::Bump.current}.gem"
|
27
|
+
end
|
28
|
+
|
29
|
+
task :tag do
|
30
|
+
require 'bump'
|
31
|
+
version = Bump::Bump.current
|
32
|
+
puts "tagging v#{version}"
|
33
|
+
`git push && git tag v#{version} && git push --tags`
|
34
|
+
end
|
35
|
+
|
36
|
+
task :bump do
|
37
|
+
Rake::Task["bump:patch"].invoke
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'restpack_core_client'
|
2
|
+
|
3
|
+
module RestPack
|
4
|
+
module Web
|
5
|
+
class App
|
6
|
+
def initialize(app, options = {})
|
7
|
+
p "RestPack::Web::App.initialize"
|
8
|
+
@app = app
|
9
|
+
|
10
|
+
options[:core_domain] ||= ENV['RESTPACK_CORE_SERVICE']
|
11
|
+
options[:access_key] ||= ENV['RESTPACK_ACCESS_KEY']
|
12
|
+
|
13
|
+
raise "missing RestPack configuration" unless options[:core_domain] || options[:access_key]
|
14
|
+
|
15
|
+
@core_cache = RestPack::Core::Client::Cache.create(options[:core_domain], options[:access_key])
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
env[:restpack] ||= {}
|
20
|
+
|
21
|
+
begin #TODO: GJ: cache should handle errors gracefully
|
22
|
+
request = Rack::Request.new(env)
|
23
|
+
|
24
|
+
channel = @core_cache.get_channel(request.host)
|
25
|
+
domain = channel.get_domain_by_host(request.host)
|
26
|
+
|
27
|
+
env[:restpack][:request] = request
|
28
|
+
env[:restpack][:host] = request.host
|
29
|
+
env[:restpack][:channel] = channel
|
30
|
+
env[:restpack][:application] = domain.application
|
31
|
+
env[:restpack][:domain] = domain
|
32
|
+
env[:restpack][:configurations] = @core_cache.configurations
|
33
|
+
env[:restpack][:services] = @core_cache.get_configuration_value('services', [])
|
34
|
+
|
35
|
+
add_user env
|
36
|
+
|
37
|
+
rescue Exception => e
|
38
|
+
p "---"
|
39
|
+
p e.message
|
40
|
+
p "===="
|
41
|
+
p e.backtrace.inspect
|
42
|
+
p "---"
|
43
|
+
end
|
44
|
+
|
45
|
+
@app.call(env)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def add_user(env)
|
51
|
+
return unless env['rack.session']
|
52
|
+
|
53
|
+
user_id = env['rack.session'][:user_id]
|
54
|
+
|
55
|
+
if user_id
|
56
|
+
user_id = env['rack.session'][:user_id]
|
57
|
+
channel_domains = env[:restpack][:channel].configurations.find{ |conf| conf.key == 'domains' }.value
|
58
|
+
auth_domain = channel_domains[:auth]
|
59
|
+
json = RestClient.get("#{auth_domain}/api/v1/users/#{user_id}.json")
|
60
|
+
env[:restpack][:user] = Yajl::Parser.parse(json)['user']
|
61
|
+
end
|
62
|
+
|
63
|
+
env[:restpack][:user_id] = user_id
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/restpack_web/context.rb
CHANGED
@@ -1,70 +1,107 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
require 'public_suffix'
|
2
|
+
|
3
|
+
module RestPack
|
4
|
+
module Web
|
5
|
+
class Context
|
6
|
+
attr_accessor :domain, :application, :channel, :configurations, :services, :user, :user_id, :request
|
7
|
+
|
8
|
+
def initialize(env)
|
9
|
+
restpack = env[:restpack]
|
10
|
+
|
11
|
+
if restpack
|
12
|
+
@request = restpack[:request]
|
13
|
+
@domain = restpack[:domain]
|
14
|
+
@application = restpack[:application]
|
15
|
+
@channel = restpack[:channel]
|
16
|
+
@configurations = restpack[:configurations]
|
17
|
+
@services = restpack[:services]
|
18
|
+
@user = restpack[:user]
|
19
|
+
@user_id = @user[:id] if @user
|
20
|
+
end
|
13
21
|
end
|
14
|
-
end
|
15
22
|
|
16
|
-
|
17
|
-
|
18
|
-
|
23
|
+
def is_authenticated?
|
24
|
+
!@user.nil?
|
25
|
+
end
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
27
|
+
def get_service(name)
|
28
|
+
@services.find { |s| s[:name] == name.to_s }
|
29
|
+
end
|
23
30
|
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
def home_domain
|
32
|
+
@application.home_domain || "www.#{root_domain}"
|
33
|
+
end
|
27
34
|
|
28
|
-
|
29
|
-
|
30
|
-
|
35
|
+
def auth_domain
|
36
|
+
@application.auth_domain || "auth.#{root_domain}"
|
37
|
+
end
|
31
38
|
|
32
|
-
|
33
|
-
|
34
|
-
|
39
|
+
def root_domain
|
40
|
+
PublicSuffix.parse(@domain.host).domain
|
41
|
+
end
|
35
42
|
|
36
|
-
|
37
|
-
|
38
|
-
|
43
|
+
def logout_url(next_url = nil) #TODO: GJ: whitelist the next_url
|
44
|
+
next_url ||= "http://#{home_domain}/"
|
45
|
+
"http://#{auth_domain}/auth/logout?next=#{next_url}"
|
46
|
+
end
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
48
|
+
def login_url(provider = :twitter, next_url = nil)
|
49
|
+
next_url ||= "http://#{home_domain}/"
|
50
|
+
"http://#{auth_domain}/auth/#{provider}?next=#{next_url}"
|
51
|
+
end
|
43
52
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
53
|
+
def debug_info #TODO: GJ: move to a mixin
|
54
|
+
user_debug_info = ""
|
55
|
+
if is_authenticated?
|
56
|
+
image_debug_info = ""
|
57
|
+
image_debug_info = " * **image** : #{@user['image']} 
|
66
|
+
}
|
67
|
+
end
|
52
68
|
|
53
|
-
|
54
|
-
|
55
|
-
|
69
|
+
%{
|
70
|
+
### RestPack Context:
|
71
|
+
|
72
|
+
#### User:
|
73
|
+
|
74
|
+
* **is authenticated**: #{is_authenticated?}
|
75
|
+
#{user_debug_info}
|
76
|
+
|
77
|
+
#### Channel:
|
56
78
|
|
57
|
-
|
79
|
+
* **id**: #{@channel.id}
|
80
|
+
* **name**: #{@channel.name}
|
81
|
+
* **applications**: #{@channel.applications.map { |a| a.name }.join(', ')}
|
58
82
|
|
59
|
-
|
60
|
-
#TODO: GJ: whitelist the next_url?
|
61
|
-
#TODO: GJ: URI encode next_url?
|
62
|
-
port = RestPack::Web.config.authentication_port
|
83
|
+
#### Application:
|
63
84
|
|
64
|
-
|
65
|
-
|
85
|
+
* **id**: #{@application.id}
|
86
|
+
* **name**: #{@application.name}
|
87
|
+
* **domains**: #{@application.domains.map { |a| a.host }.join(', ')}
|
66
88
|
|
67
|
-
|
89
|
+
#### Domain:
|
90
|
+
|
91
|
+
* **id**: #{@domain.id}
|
92
|
+
* **host**: #{@domain.host}
|
93
|
+
|
94
|
+
#### Configuration:
|
95
|
+
|
96
|
+
* **keys**: #{@configurations.map { |c| c.key }.join(', ')}
|
97
|
+
|
98
|
+
#### Authentication:
|
99
|
+
|
100
|
+
* **logout**: #{logout_url}
|
101
|
+
* **twitter oauth**: #{login_url(:twitter)}
|
102
|
+
* **google oauth**: #{login_url(:google_oauth2)}
|
103
|
+
}
|
104
|
+
end
|
68
105
|
end
|
69
106
|
end
|
70
|
-
end
|
107
|
+
end
|
@@ -1,11 +1,15 @@
|
|
1
|
-
module RestPack
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module RestPack
|
2
|
+
module Web
|
3
|
+
module Rails
|
4
|
+
module Controller
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:before_filter, :include_restpack)
|
7
|
+
end
|
8
|
+
|
9
|
+
def include_restpack
|
10
|
+
@restpack = RestPack::Web::Context.new(request.env)
|
11
|
+
end
|
12
|
+
end
|
9
13
|
end
|
10
14
|
end
|
11
|
-
end
|
15
|
+
end
|
@@ -1,13 +1,20 @@
|
|
1
|
-
|
2
|
-
module App
|
3
|
-
def self.included(base)
|
4
|
-
base.use RestPack::Web::Rack::Domain
|
5
|
-
base.use RestPack::Web::Rack::Session
|
6
|
-
base.use RestPack::Web::Rack::User
|
1
|
+
require 'rack-auto-session-domain'
|
7
2
|
|
8
|
-
|
9
|
-
|
3
|
+
module RestPack
|
4
|
+
module Web
|
5
|
+
module Sinatra
|
6
|
+
module App
|
7
|
+
def self.included(base)
|
8
|
+
base.use Rack::AutoSessionDomain
|
9
|
+
base.use RestPack::Web::App
|
10
|
+
base.enable :sessions
|
11
|
+
base.set :session_secret, 'c40b83f9048c497891f0f71e4b040a3f' #TODO: GJ: from restpack config
|
12
|
+
|
13
|
+
base.before do
|
14
|
+
@restpack = RestPack::Web::Context.new(env)
|
15
|
+
end
|
16
|
+
end
|
10
17
|
end
|
11
18
|
end
|
12
19
|
end
|
13
|
-
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'webmock'
|
2
|
+
include WebMock::API
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module Test
|
6
|
+
DEFAULT_HOST = 'sinatra.restpack-sample.org'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module RestPack
|
11
|
+
module Web
|
12
|
+
class TestSupport
|
13
|
+
def self.stub_restpack_web
|
14
|
+
host = "http://:#{ENV['RESTPACK_ACCESS_KEY']}@#{ENV['RESTPACK_CORE_SERVICE']}/api/v1"
|
15
|
+
stub_request(:get, "#{host}/configurations/root.json").to_return(:body => read_fixture('configurations_root'))
|
16
|
+
stub_request(:get, "#{host}/domains/search.json?host=sinatra.restpack-sample.org").to_return(:body => read_fixture('domains_search'))
|
17
|
+
stub_request(:get, "#{host}/channels/1.json").to_return(:body => read_fixture('channels'))
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.read_fixture(name)
|
21
|
+
file = File.open("#{File.expand_path('../../../spec/fixtures', __FILE__)}/#{name}.json")
|
22
|
+
file.read
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/restpack_web/version.rb
CHANGED
data/lib/restpack_web.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
require 'restpack_web/version'
|
2
|
-
|
3
|
-
require 'restpack_web/configuration'
|
4
2
|
require 'restpack_web/context'
|
5
|
-
|
6
|
-
require 'restpack_web/rack/domain'
|
7
|
-
require 'restpack_web/rack/user'
|
8
|
-
require 'restpack_web/rack/session'
|
3
|
+
require 'restpack_web/app'
|
9
4
|
|
10
5
|
require 'restpack_web/rails/controller'
|
11
6
|
require 'restpack_web/sinatra/app'
|
data/restpack_web.gemspec
CHANGED
@@ -1,31 +1,32 @@
|
|
1
|
-
#
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'restpack_web/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
spec.license = "MIT"
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "restpack_web"
|
8
|
+
gem.version = RestPack::Web::VERSION
|
9
|
+
gem.authors = ["Gavin Joyce"]
|
10
|
+
gem.email = ["gavinjoyce@gmail.com"]
|
11
|
+
gem.description = %q{Run a web application on RestPack}
|
12
|
+
gem.summary = %q{Run a web application on RestPack}
|
13
|
+
gem.homepage = "https://github.com/RestPack"
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
spec.add_dependency "restpack_group_service"
|
20
|
+
gem.add_dependency 'rack-auto-session-domain', '~> 0.1.1'
|
21
|
+
gem.add_dependency 'restpack_core_client', '~> 0.2.2'
|
22
|
+
gem.add_dependency 'public_suffix', '~> 1.3.0'
|
23
|
+
gem.add_dependency 'yajl-ruby', '~> 1.1.0'
|
26
24
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
gem.add_development_dependency 'rake'
|
26
|
+
gem.add_development_dependency 'rack'
|
27
|
+
gem.add_development_dependency 'bump', '~> 0.4'
|
28
|
+
gem.add_development_dependency 'minitest', '~> 4.7'
|
29
|
+
gem.add_development_dependency 'rack-test', '~> 0.6'
|
30
|
+
gem.add_development_dependency 'webmock', '~> 1.11'
|
31
|
+
gem.add_development_dependency 'rest-client'
|
31
32
|
end
|