sinatra-backstage 0.0.1

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: 8994f18d2816cd1b60ab0d0eaaf902fc3257734d
4
+ data.tar.gz: 96cf5456d3f53b05660fa121f6764ab1ba42cb48
5
+ SHA512:
6
+ metadata.gz: 78f4facabf69816443541ed85e4a75d4221d83e3af0a1206eb980456f677222b9bbef158cd054078877138641e38dd053669ce571189fc31405d036def6dea09
7
+ data.tar.gz: 66d9b80b2c7f6340e6950cf371f5c094d9af0277c873d26efc9a17ea5a4cf35c020c3ff2ad67174d62ff6e2fc653b5d3540ca7e632e9b90d541dc782114a5e9f
@@ -0,0 +1,18 @@
1
+ ## Rest class (DataMapper Resource)
2
+ module Sinatra
3
+ module Backstage
4
+ module REST
5
+ module Model
6
+
7
+ def form_update
8
+ "#{form_id}-update"
9
+ end
10
+
11
+ def form_delete
12
+ "#{form_id}-delete"
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+
3
+ module Sinatra
4
+ module Backstage
5
+ module REST
6
+ module Routing
7
+
8
+ def self.included(controller)
9
+ controller.extend self
10
+ end
11
+
12
+ def rest_routes(klass, namespace)
13
+
14
+ ## Routes
15
+ ### get list
16
+ get namespace do
17
+ @objects = klass.all params.inject({}){ | hash_sym, (key, val) | hash_sym[key.to_sym] = val; hash_sym }
18
+ request.accept.each do |type|
19
+ # puts "type.to_s = #{type.to_s}"
20
+ case type.to_s
21
+ when 'text/html'
22
+ halt eval "#{settings.render_engine} '#{klass.name.split('::').last.downcase}_list'"
23
+ when 'text/json', 'application/json'
24
+ halt @objects.to_a.to_json
25
+ end
26
+ end
27
+ error 406
28
+ end
29
+
30
+ ### create object
31
+ post namespace do
32
+ # puts "params = #{params}"
33
+ # puts "-- rest_routes post namespace = #{namespace}"
34
+ # puts "-- rest_routes post klass = #{klass}"
35
+ # halt
36
+ begin
37
+ object = klass.create params[:object]
38
+ redirect namespace
39
+ rescue DataMapper::SaveFailureError => e
40
+ puts e.resource.errors.inspect
41
+ puts e.message
42
+ puts e.backtrace
43
+ end
44
+ end
45
+
46
+ ### update object
47
+ put "#{namespace}/:id" do |id|
48
+ klass.get!(id).update params[:object]
49
+ redirect namespace
50
+ end
51
+
52
+ ### delete object
53
+ delete "#{namespace}/:id" do |id|
54
+ klass.get!(id).destroy
55
+ redirect namespace
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ def self.included(app)
63
+ app.include Routing
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ Dir.glob(File.join(__dir__, 'rest', '*.rb')).each {|file| require file }
2
+
3
+ # module Sinatra
4
+ # helpers Sinatra::Backstage::REST
5
+ # end
@@ -0,0 +1,68 @@
1
+ ## sinatra gems
2
+ require 'sinatra'
3
+
4
+ require_relative './user_model'
5
+
6
+ module Sinatra
7
+ module Backstage
8
+ module User
9
+ module Helper
10
+
11
+ def self.included(app)
12
+ if app.ancestors.include? Sinatra::Base
13
+ app.set(:user_routes) do |bool|
14
+ condition do
15
+ (request.path =~ ( bool ?
16
+ /^\/(login|signup)/ :
17
+ /^(?!\/(login|signup))/
18
+ )) != nil
19
+ end
20
+ end
21
+
22
+ ## Hooks
23
+ app.before :user_routes => true do
24
+ # puts "-- Backstage::User login|signup authorized? = #{authorized?}"
25
+ redirect '/' if authorized?
26
+ # settings.site_routes[request.path]['active'] = true if settings.site_routes[request.path]
27
+ end
28
+
29
+ # app.after :user_routes => true do
30
+ # settings.site_routes[request.path]['active'] = false if settings.site_routes[request.path]
31
+ # end
32
+ end
33
+ end
34
+
35
+ def set_session(user)
36
+ if user
37
+ cookies[:username] = user.username
38
+ cookies[:session] = user.session
39
+ else
40
+ cookies[:username] = nil
41
+ cookies[:session] = nil
42
+ end
43
+ end
44
+
45
+ def authorized?
46
+ authorized_user.is_a? User
47
+ end
48
+
49
+ def authorized_user
50
+ if @authorized_user.nil?
51
+ return nil unless cookies[:username] && cookies[:session]
52
+ user = settings.user_class.first :username => cookies[:username]
53
+ return nil unless user
54
+ @authorized_user = (user.session == cookies[:session] ? user : false)
55
+ end
56
+ @authorized_user
57
+ end
58
+
59
+ def user_role
60
+ authorized? ? :user : :guest
61
+ end
62
+
63
+ end
64
+
65
+ include Helper
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,78 @@
1
+ require_relative './user_helper'
2
+
3
+ module Sinatra
4
+ module Backstage
5
+ module User
6
+
7
+ def self.registered(app)
8
+
9
+ ## Helpers
10
+ app.helpers Sinatra::Cookies, Backstage::User::Helper
11
+
12
+ ## Configure
13
+ app.enable :sessions
14
+
15
+ app.set :user_class, Backstage::User::User
16
+
17
+ ## Routes
18
+ ### get login page
19
+ app.get '/login' do
20
+ eval "#{settings.render_engine} 'user/login'"
21
+ end
22
+
23
+ app.get '/signup' do
24
+ eval "#{settings.render_engine} 'user/signup'"
25
+ end
26
+
27
+ ### signup user
28
+ app.post '/signup' do
29
+ # puts "params = #{params}"
30
+ # halt
31
+ @errors = []
32
+ begin
33
+ user = settings.user_class.create params[:user]
34
+ set_session user
35
+ redirect '/'
36
+ rescue DataMapper::SaveFailureError => e
37
+ puts e.resource.errors.inspect
38
+ e.resource.errors.each do |error|
39
+ puts error.inspect
40
+ @errors << error
41
+ end
42
+ @errors.flatten!
43
+ puts @errors.inspect
44
+ end
45
+ # puts "User::Middleware post /signup ( params['user']['email'] = #{params['user']['email']} )"
46
+ eval "#{settings.render_engine} 'user/signup'"
47
+ end
48
+
49
+ ### login user
50
+ app.post '/login' do
51
+ @errors = []
52
+ password = params[:user].delete('password')
53
+ user = settings.user_class.first params[:user]
54
+ # puts "User::Middleware post '/login' ( params[:user] = #{params[:user]} )"
55
+ # puts "User::Middleware post '/login' ( user = #{user} )"
56
+ if user.nil?
57
+ @errors << "Incorrect #{params[:user].keys.join(' or ')}"
58
+ else
59
+ if user.password == password
60
+ set_session user
61
+ redirect '/'
62
+ else
63
+ @errors << "Incorrect password"
64
+ end
65
+ end
66
+ eval "#{settings.render_engine} 'user/login'"
67
+ end
68
+
69
+ ### logout user
70
+ app.get '/logout' do
71
+ set_session nil
72
+ redirect '/'
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,45 @@
1
+ ## database gems
2
+ require 'dm-core'
3
+ require 'dm-types'
4
+ require 'dm-validations'
5
+
6
+ ## Store class (DataMapper Resource)
7
+ module Sinatra
8
+ module Backstage
9
+ module User
10
+ class User
11
+
12
+ include DataMapper::Resource
13
+
14
+ ## Properties
15
+ property :id, Serial
16
+ property :username, String
17
+ property :password, BCryptHash,
18
+ :required => true
19
+ # property :salt, BCryptHash,
20
+ # :required => true,
21
+ # :default => lambda {|r, p| BCrypt::Engine.generate_salt }
22
+
23
+ # def salt
24
+ # self.salt = BCrypt::Engine.generate_salt if super.nil?
25
+ # end
26
+
27
+ # def password=(new_password)
28
+ # # self.password_hash = generate_salt_hash(new_password)
29
+ # self.password_hash = new_password
30
+ # end
31
+
32
+ def session
33
+ # generate_salt_hash(username)
34
+ BCrypt::Engine.hash_secret(username, password)
35
+ end
36
+
37
+ ## Methods
38
+ # def generate_salt_hash(check_password)
39
+ # BCrypt::Engine.hash_secret(check_password, salt)
40
+ # end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ Dir.glob(File.join(__dir__, 'user', '*.rb')).each {|file| require file }
2
+
3
+ # module Sinatra
4
+ # register Backstage::User
5
+ # end
@@ -0,0 +1 @@
1
+ Dir.glob(File.join(__dir__, 'backstage', '*.rb')).each {|file| require file }
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-backstage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra-contrib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dm-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dm-types
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dm-validations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ description: Sinatra-Backstage simplifies the use of the basic routes and models for
84
+ Sinatra and DataMapper. Currently it includes REST and User.
85
+ email: alex.wayfer@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/sinatra/backstage.rb
91
+ - lib/sinatra/backstage/rest.rb
92
+ - lib/sinatra/backstage/rest/rest_model.rb
93
+ - lib/sinatra/backstage/rest/rest_routes.rb
94
+ - lib/sinatra/backstage/user.rb
95
+ - lib/sinatra/backstage/user/user_helper.rb
96
+ - lib/sinatra/backstage/user/user_middleware.rb
97
+ - lib/sinatra/backstage/user/user_model.rb
98
+ homepage: https://bitbucket.org/AlexWayfer/sinatra-backstage
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Extension for Sinatra and DataMapper
122
+ test_files: []