cuba-tools 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69a4bf722598190f9ae32fc765b088854e5f0d38
4
- data.tar.gz: 0b5f4a4e64dbf29b4d7c6901f286e9586e978f70
3
+ metadata.gz: e6086b272172c453acebdc0248bffa788e581a7b
4
+ data.tar.gz: 6710dda1e36ea1c6dca8167ea232b97a055d3a13
5
5
  SHA512:
6
- metadata.gz: 285cef1afca155ea4647b5c665230bda648b16e3d90883240864f06eda0341c71e2578de2189e20e50ccd46481c76fc057b8f236874d9c3ff5bdd7f510843390
7
- data.tar.gz: 33433a5e6e712ef77dea134f3849f5390411afbb979390e790aa6780ce1748a7474ec245d6cee1bd0d2165d7a22035d2506683606959eeea7a769a4fc2556bd3
6
+ metadata.gz: 255ec5eb229ccb8683a7a80addcf50a7e7668b4863feeef219483d49c0d34951326a89f90e1b8b4a4858af948c7603abc7cfcec9882152de719baba9542d1379
7
+ data.tar.gz: 920d17c304443ce6fd5d86d7ee9a941c6f8745fe7c0a0ff7843075696d4e30a8e9d54d5cc37ccfcc8e576c4643b2438440b834053188d9247c20ee93d458cddb
data/README.md CHANGED
@@ -1,29 +1,8 @@
1
- # Cuba::Tools
1
+ Cuba/Tools
2
+ ==========
2
3
 
3
- TODO: Write a gem description
4
+ ![Build Status](https://secure.travis-ci.org/cj/cuba-tools.png)
4
5
 
5
- ## Installation
6
+ _n_. tools for [cuba](https://github.com/soveran/cuba)
6
7
 
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'cuba-tools'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install cuba-tools
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
8
+ ![](http://i.imgur.com/CTJMSe8.jpg)
data/cuba-tools.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "cuba-tools"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["cj"]
9
9
  spec.email = ["cjlazell@gmail.com"]
10
10
  spec.description = %q{Contains a group of tools to extend cuba}
@@ -17,7 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency("cuba", "~> 3.1.1")
20
+ spec.add_dependency "cuba", "~> 3.1.1"
21
+ spec.add_dependency "shield", "~> 2.1.0"
22
+ spec.add_dependency "signature-acd", "~> 0.1.9"
21
23
 
22
24
  spec.add_development_dependency "bundler", "~> 1.3"
23
25
  spec.add_development_dependency "rake"
@@ -0,0 +1,86 @@
1
+ module Cuba::Tools
2
+ module Api
3
+ extend self
4
+
5
+ attr_accessor :config
6
+
7
+ def setup
8
+ yield config
9
+ end
10
+
11
+ def config
12
+ @config || reset_config
13
+ end
14
+
15
+ # Resets the configuration to the default (empty hash)
16
+ def reset_config
17
+ @config = OpenStruct.new({
18
+ path: './app/api_params',
19
+ user_class: 'User'
20
+ })
21
+ end
22
+
23
+ class ValidationError < RuntimeError
24
+ attr_reader :errors
25
+
26
+ def initialize errors
27
+ @errors = errors
28
+ end
29
+ end
30
+
31
+ module Helpers
32
+ def self.setup app
33
+ require 'yaml'
34
+ end
35
+
36
+ def api_params key, klass
37
+ params = add_attributes_for req.params[key.to_s]
38
+
39
+ @api_params ||= YAML.load_file "#{Api.config.path}/#{key}.yml"
40
+
41
+ form = klass.restrict!(current_user).new
42
+
43
+ if form.validates params, as: current_user
44
+ lambda { captures << form; captures << params }
45
+ else
46
+ raise ValidationError.new(form.errors), 'Api Validation Failed'
47
+ end
48
+ end
49
+
50
+ def api_signature
51
+ api_request = Signature::Request.new(
52
+ req.request_method,
53
+ req.path,
54
+ req.params,
55
+ req.env
56
+ )
57
+
58
+ user = false
59
+
60
+ api_request.authenticate do |key|
61
+ user = user_class.where(api_key: key).first
62
+ Signature::Token.new key, user.try(:api_secret)
63
+ end
64
+
65
+ sign_in user
66
+ end
67
+
68
+ def add_attributes_for params
69
+ params.dup.each do |key, value|
70
+ if !key[/_attributes$/] && value.is_a?(Hash)
71
+ params["#{key}_attributes"] = add_attributes_for value
72
+ params.delete key
73
+ end
74
+ end
75
+
76
+ params
77
+ end
78
+
79
+ private
80
+
81
+ def user_class
82
+ Cuba::Tools::Auth.config.user_class.constantize
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,106 @@
1
+ module Cuba::Tools
2
+ module Auth
3
+ extend self
4
+
5
+ attr_accessor :config
6
+
7
+ def setup
8
+ yield config
9
+ end
10
+
11
+ def config
12
+ @config || reset_config
13
+ end
14
+
15
+ # Resets the configuration to the default (empty hash)
16
+ def reset_config
17
+ @config = OpenStruct.new({
18
+ user_class: 'User'
19
+ })
20
+ end
21
+
22
+ class Middleware
23
+ def initialize(app)
24
+ @app = app
25
+ end
26
+
27
+ def call(env)
28
+ responder = Responder.new(@app, env)
29
+ responder.respond
30
+ end
31
+ end
32
+
33
+ class Responder
34
+ def initialize(app, env)
35
+ @app = app
36
+ @env = env
37
+ end
38
+
39
+ def respond
40
+ res.finish
41
+ end
42
+
43
+ private
44
+
45
+ def path
46
+ @env['REQUEST_PATH']
47
+ end
48
+
49
+ def req
50
+ @req ||= Rack::Request.new(@env)
51
+ end
52
+
53
+ def res
54
+ @res ||= begin
55
+ status, headers, body = @app.call(req.env)
56
+ Rack::Response.new(body, status, headers)
57
+ end
58
+ end
59
+ end
60
+
61
+ module Helpers
62
+ def self.setup app
63
+ if !defined? Devise
64
+ require 'shield'
65
+ app.plugin Shield::Helpers
66
+ app.use Shield::Middleware, "/login"
67
+ else
68
+ require 'warden'
69
+ require 'devise'
70
+ app.plugin Devise::TestHelpers
71
+ end
72
+ end
73
+
74
+ def current_user
75
+ @current_user ||= if !defined? Devise
76
+ authenticated user_class
77
+ else
78
+ req.env['warden'].authenticate(scope: :user)
79
+ end
80
+ end
81
+
82
+ def sign_in *args
83
+ if args.length > 1
84
+ user, scope = args
85
+ else
86
+ scope = :user
87
+ user = args.first
88
+ end
89
+
90
+ if !defined? Devise
91
+ session.clear
92
+ session[user_class.to_s] = user.id
93
+ else
94
+ @request = req
95
+ super scope, user
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ def user_class
102
+ Cuba::Tools::Auth.config.user_class.constantize
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/cuba/tools.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  module Cuba::Tools
2
+ autoload :Auth, "cuba/tools/auth"
2
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: shield
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: signature-acd
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.9
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.9
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: bundler
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +94,8 @@ files:
66
94
  - Rakefile
67
95
  - cuba-tools.gemspec
68
96
  - lib/cuba/tools.rb
69
- - lib/cuba/tools/.gitkeep
97
+ - lib/cuba/tools/api.rb
98
+ - lib/cuba/tools/auth.rb
70
99
  homepage: ''
71
100
  licenses:
72
101
  - MIT
File without changes