stagehand 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -7,9 +7,9 @@ Usage
7
7
  =====
8
8
 
9
9
  YourApp::Application.configure do
10
- config.stagehand.client_id = 'YOUR_APP_CLIENT_ID',
11
- config.stagehand.client_secret = 'YOUR_APP_CLIENT_SECRET',
12
- config.stagehand.resource_host = 'YOUR_APP_RESOURCE_HOST',
10
+ config.stagehand.client_id = 'YOUR_APP_CLIENT_ID'
11
+ config.stagehand.client_secret = 'YOUR_APP_CLIENT_SECRET'
12
+ config.stagehand.resource_host = 'YOUR_APP_RESOURCE_HOST'
13
13
  config.stagehand.client_host = 'YOUR_APP_HOST_WITH_PORT'
14
14
  end
15
15
 
data/lib/stagehand.rb CHANGED
@@ -1,25 +1,33 @@
1
- require 'yajl'
2
- require 'stagehand/version'
1
+ require 'httparty'
2
+ require 'stagehand/client'
3
+ require 'stagehand/rack/middleware'
3
4
  require 'stagehand/railtie' if defined? ::Rails::Railtie
5
+ require 'stagehand/version'
6
+ require 'yajl'
4
7
 
5
8
  module Stagehand
6
9
  class Config
7
10
  attr_accessor :client_id, :client_secret, :client_host, :resource_host
8
11
  end
9
12
 
10
- def self.config
11
- @@config ||= Config.new
12
- end
13
-
14
- def self.configure
15
- yield self.config
16
- end
13
+ class << self
14
+ def config
15
+ @@config ||= Config.new
16
+ end
17
17
 
18
- def initialize(attributes)
19
- @attributes = attributes
20
- end
18
+ def configure
19
+ yield self.config
20
+ end
21
+
22
+ # return Stagehand::Client
23
+ def new(options={})
24
+ Stagehand::Client.new(options)
25
+ end
21
26
 
22
- def self.authorize_url
23
- config.resource_host + "/oauth/authorize?client_id=#{config.client_id}&client_secret=#{config.client_secret}&redirect_uri=#{config.client_host}/callback"
27
+ # Delegate to Stagehand::Client
28
+ def method_missing(method, *args, &block)
29
+ return super unless new.respond_to?(method)
30
+ new.send(method, *args, &block)
31
+ end
24
32
  end
25
33
  end
@@ -0,0 +1,7 @@
1
+ module Stagehand
2
+ # Wrapper for Personas REST API
3
+ class Client
4
+ require 'stagehand/client/oauth'
5
+ include Stagehand::Client::OAuth
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ module Stagehand
2
+ class Client
3
+ # Methods for OAuth 2.0 authentication
4
+ module OAuth
5
+ # URL used for initial OAuth login and authorization
6
+ def authorize_url
7
+ Stagehand.config.resource_host + "/oauth/authorize?client_id=#{Stagehand.config.client_id}&client_secret=#{Stagehand.config.client_secret}&redirect_uri=#{Stagehand.config.client_host}/callback"
8
+ end
9
+
10
+ def access_token_url
11
+ Stagehand.config.resource_host + "/oauth/access_token"
12
+ end
13
+
14
+ def redirect_uri
15
+ Stagehand.config.client_host + "/callback"
16
+ end
17
+
18
+ def logout_url
19
+ Stagehand.config.resource_host + "/logout?redirect_uri=#{Stagehand.config.client_host}"
20
+ end
21
+
22
+ def access_token
23
+ request.env['rack.session'][:access_token]
24
+ end
25
+
26
+ def get_with_access_token(path, params)
27
+ params[:oauth_token] = access_token
28
+ HTTParty.get(Stagehand.config.resource_host + path, query: params)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module Stagehand
2
+ class Client
3
+ # Methods for users
4
+ module User
5
+ def profile
6
+ response = get(Stagehand.config.resource_host + "/profile.json")
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module Stagehand::Rack
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ request = env['stagehand'] = Rack::Request.new(env)
9
+
10
+ response = catch :stagehand do
11
+ if request.path == '/callback'
12
+ token_response = HTTParty.post(Stagehand.access_token_url, :body => {
13
+ :client_id => Stagehand.config.client_id,
14
+ :client_secret => Stagehand.config.client_secret,
15
+ :redirect_uri => Stagehand.redirect_uri,
16
+ :code => request.params['code'],
17
+ :grant_type => 'authorization_code'}
18
+ )
19
+ env['rack.session'][:access_token] = token_response["access_token"]
20
+ # redirect to root
21
+ [302, {'Location'=>'/'}, []]
22
+ else
23
+ @app.call(env)
24
+ end
25
+ end
26
+ rescue InvalidRequest => e
27
+ [400, {}, e.message]
28
+ end
29
+ end
30
+
31
+ class InvalidRequest < StandardError
32
+ end
33
+ end
@@ -11,4 +11,9 @@ class Stagehand::Railtie < Rails::Railtie
11
11
  config.client_host = app.config.stagehand[:client_host]
12
12
  end
13
13
  end
14
+
15
+ initializer "stagehand.initialize_middleware" do |app|
16
+ app.middleware.use ::Stagehand::Rack::Middleware
17
+ end
18
+
14
19
  end
@@ -1,3 +1,3 @@
1
1
  module Stagehand
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,15 +1,44 @@
1
1
  require 'spec_helper'
2
2
 
3
+ RSpec.configure do |c|
4
+ c.before(:each) do
5
+ Stagehand.configure do |config|
6
+ config.client_id = 'test_app'
7
+ config.client_secret = 'abcdef123456'
8
+ config.resource_host = 'http://0.0.0.0:3000'
9
+ config.client_host = 'http://0.0.0.0:4000'
10
+ end
11
+ end
12
+ end
13
+
3
14
  describe Stagehand do
4
15
  describe "initialization" do
5
- it "creates an OAuth 2.0 authorize url" do
6
- Stagehand.configure do |config|
7
- config.client_id = 'test_app'
8
- config.client_secret = 'abcdef123456'
9
- config.resource_host = 'http://0.0.0.0:3000'
10
- config.client_host = 'http://0.0.0.0:4000'
11
- end
16
+ it "can create an OAuth 2.0 authorize url" do
12
17
  Stagehand.authorize_url.should == "http://0.0.0.0:3000/oauth/authorize?client_id=test_app&client_secret=abcdef123456&redirect_uri=http://0.0.0.0:4000/callback"
13
18
  end
14
19
  end
20
+
21
+ describe "url helpers" do
22
+ it "can get the access token url" do
23
+ Stagehand.access_token_url.should == "http://0.0.0.0:3000/oauth/access_token"
24
+ end
25
+
26
+ it "can get the redirect uri" do
27
+ Stagehand.redirect_uri.should == "http://0.0.0.0:4000/callback"
28
+ end
29
+
30
+ it "can get the logout url" do
31
+ Stagehand.logout_url.should == "http://0.0.0.0:3000/logout?redirect_uri=http://0.0.0.0:4000"
32
+ end
33
+
34
+ it "can get the access token" do
35
+ Stagehand.access_token == session[:access_token]
36
+ end
37
+ end
38
+
39
+ describe "current user" do
40
+ it "can see his profile" do
41
+ Stagehand.profile == { username: "Test" }
42
+ end
43
+ end
15
44
  end
data/stagehand.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency 'rspec', '~> 2.7.0'
23
23
  s.add_runtime_dependency 'yajl-ruby', '~> 1.0.0'
24
+ s.add_runtime_dependency 'httparty', '~> 0.8.1'
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stagehand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-24 00:00:00.000000000Z
12
+ date: 2011-10-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &74656340 !ruby/object:Gem::Requirement
16
+ requirement: &88349500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.7.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *74656340
24
+ version_requirements: *88349500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &74656090 !ruby/object:Gem::Requirement
27
+ requirement: &88349180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,18 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *74656090
35
+ version_requirements: *88349180
36
+ - !ruby/object:Gem::Dependency
37
+ name: httparty
38
+ requirement: &88348780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.8.1
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *88348780
36
47
  description: Client-side library for manipulating RESTful resources provided by Personas,
37
48
  a Rails + Backbone user managment service
38
49
  email:
@@ -46,6 +57,10 @@ files:
46
57
  - README.md
47
58
  - Rakefile
48
59
  - lib/stagehand.rb
60
+ - lib/stagehand/client.rb
61
+ - lib/stagehand/client/oauth.rb
62
+ - lib/stagehand/client/user.rb
63
+ - lib/stagehand/rack/middleware.rb
49
64
  - lib/stagehand/railtie.rb
50
65
  - lib/stagehand/version.rb
51
66
  - spec/spec_helper.rb
@@ -71,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
86
  version: '0'
72
87
  requirements: []
73
88
  rubyforge_project: stagehand
74
- rubygems_version: 1.8.11
89
+ rubygems_version: 1.8.10
75
90
  signing_key:
76
91
  specification_version: 3
77
92
  summary: client library for Personas (user management) service