restpack_web 0.0.5 → 0.2.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8830af231f1d83fb48ceae33fd6e664421a7decd
4
- data.tar.gz: f6a3bfb0fc99b289934bc476abb95c0b0b573663
5
- SHA512:
6
- metadata.gz: fda6cf1ebece0df58b0b0aeadb91b4680c905686bd6d5fc86466c89a605b9f23c7c373cddd302eb7e97d7b69a0a2b344defda1157359f41629d9f8c2b32d3dda
7
- data.tar.gz: 66f2cd98d722c66ab32b4c59ed5a5fc2b1b12553b1b29455b963e343d7a0178bad5071af552b830640cbc16dc551a9d9d86b942d5ac7a155c49e4dfd145a95fb
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
@@ -14,4 +14,4 @@ rdoc
14
14
  spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
- tmp
17
+ tmp
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
-
5
- gem 'encrypted_cookie', path: '../encrypted_cookie'
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
- # restpack_web
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 "restpack_gem"
2
- RestPack::Gem::Tasks.load_tasks
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
@@ -1,70 +1,107 @@
1
- module RestPack::Web
2
- class Context
3
- attr_accessor :domain, :application, :user, :account
4
-
5
- def initialize(env)
6
- restpack = env['restpack']
7
-
8
- if restpack
9
- @domain = restpack[:domain]
10
- @application = restpack[:application]
11
- @user = restpack[:user]
12
- @account = restpack[:account]
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
- def authenticated?
17
- !@user.nil?
18
- end
23
+ def is_authenticated?
24
+ !@user.nil?
25
+ end
19
26
 
20
- def user_id
21
- authenticated? ? @user[:id] : nil
22
- end
27
+ def get_service(name)
28
+ @services.find { |s| s[:name] == name.to_s }
29
+ end
23
30
 
24
- def account_id
25
- authenticated? ? @account[:id] : nil
26
- end
31
+ def home_domain
32
+ @application.home_domain || "www.#{root_domain}"
33
+ end
27
34
 
28
- def application_id
29
- @application[:id]
30
- end
35
+ def auth_domain
36
+ @application.auth_domain || "auth.#{root_domain}"
37
+ end
31
38
 
32
- def domain_id
33
- @domain[:id]
34
- end
39
+ def root_domain
40
+ PublicSuffix.parse(@domain.host).domain
41
+ end
35
42
 
36
- def home_domain
37
- "www.#{@domain[:identifier]}"
38
- end
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
- def auth_domain
41
- "auth.#{@domain[:identifier]}"
42
- end
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
- def logout_url(next_url = nil)
45
- next_url ||= "http://#{home_domain}/"
46
- auth_url('/auth/logout', next_url)
47
- end
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']} ![Image](#{@user['image']}" unless @user['image'].nil?
48
58
 
49
- def login_url(provider = :twitter, next_url = nil)
50
- auth_url("/auth/#{provider}", next_url)
51
- end
59
+ user_debug_info = %{
60
+ * **id** : #{@user['id']}
61
+ * **name** : #{@user['name']}
62
+ * **nickname** : #{@user['nickname']}
63
+ * **location** : #{@user['location']}
64
+ * **description** : #{@user['description']}
65
+ #{image_debug_info})
66
+ }
67
+ end
52
68
 
53
- def debug_info
54
- "todo"
55
- end
69
+ %{
70
+ ### RestPack Context:
71
+
72
+ #### User:
73
+
74
+ * **is authenticated**: #{is_authenticated?}
75
+ #{user_debug_info}
76
+
77
+ #### Channel:
56
78
 
57
- private
79
+ * **id**: #{@channel.id}
80
+ * **name**: #{@channel.name}
81
+ * **applications**: #{@channel.applications.map { |a| a.name }.join(', ')}
58
82
 
59
- def auth_url(path, next_url = nil)
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
- port_part = port ? ":#{port}" : ""
65
- next_part = next_url ? "?next=#{next_url}" : ""
85
+ * **id**: #{@application.id}
86
+ * **name**: #{@application.name}
87
+ * **domains**: #{@application.domains.map { |a| a.host }.join(', ')}
66
88
 
67
- "http://#{auth_domain}#{port_part}#{path}#{next_part}"
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::Web::Rails
2
- module Controller
3
- def self.included(base)
4
- base.send(:before_filter, :setup_restpack_context)
5
- end
6
-
7
- def setup_restpack_context
8
- @restpack = RestPack::Web::Context.new(request.env)
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
- module RestPack::Web::Sinatra
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
- base.before do
9
- @restpack = RestPack::Web::Context.new(env)
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
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Web
3
- VERSION = "0.0.5"
3
+ VERSION = "0.2.17"
4
4
  end
5
- end
5
+ end
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
- # coding: utf-8
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 |spec|
7
- spec.name = "restpack_web"
8
- spec.version = RestPack::Web::VERSION
9
- spec.authors = ["Gavin Joyce"]
10
- spec.email = ["gavinjoyce@gmail.com"]
11
- spec.description = %q{Rack middleware for your RestPack web apps}
12
- spec.summary = %q{Rack middleware}
13
- spec.homepage = "http://restpack.org/"
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
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
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
- spec.add_dependency "restpack_gem"
22
- spec.add_dependency "restpack_core_service"
23
- spec.add_dependency "restpack_user_service"
24
- spec.add_dependency "restpack_account_service"
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
- spec.add_development_dependency "bundler", "~> 1.3"
28
- spec.add_development_dependency "rake"
29
- spec.add_development_dependency "rspec"
30
- spec.add_development_dependency "bump"
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