app_rail-airtable 0.2.11 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +12 -1
- data/lib/app_rail/airtable/authenticatable.rb +6 -4
- data/lib/app_rail/airtable/authentication_helpers.rb +1 -1
- data/lib/app_rail/airtable/sinatra.rb +31 -12
- data/lib/app_rail/airtable/version.rb +1 -1
- data/templates/project/.gitignore +15 -4
- data/templates/project/Gemfile +11 -5
- data/templates/project/config.ru +7 -1
- data/templates/project/spec/spec_helper.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15aed8edd922ec35c782dd42d03b7beb8e6a797b4bbc68a32f7db968b905c92c
|
4
|
+
data.tar.gz: d864fd4adc4f4fdf2c96d7751e9c7ab7005d629ce0d4109f8221f3173796c7e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6876774d54ef6775fdebc8dc435a33f44ce4a298ac420031c369dcc049e7298d79a204bff276f7f19dba0e4322cce0aee7b089b142ad2d55fdbeeb477f55adc7
|
7
|
+
data.tar.gz: f77814cfdecf5792443e48cc22be7faf05a533c9f798597f39a65c2cd311763a77a1e729e906b746e6c4a7331a9fd42b1a5456b2d2205102be64badfba8ac556
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -36,7 +36,18 @@ To provide support for routes, models can implement
|
|
36
36
|
In order to support authentication you should create a class (normally `User`) and inherit from `AppRail::Airtable::AuthenticationRecord`. Your table needs `Email`, `Password Hash` and `Access Token` columns.
|
37
37
|
|
38
38
|
### Servers
|
39
|
-
You should create a single server which extends `AppRail::Airtable::Sinatra
|
39
|
+
You should create a single server which extends `AppRail::Airtable::Sinatra`.
|
40
|
+
|
41
|
+
App Rail Airtable provides two helpers to generate routes
|
42
|
+
|
43
|
+
**resources(name, only:)**
|
44
|
+
Creates routes that map to a table. It delegates from the route to a model method
|
45
|
+
* `index` to `ar_list_item`
|
46
|
+
* `show` to `ar_stack`
|
47
|
+
* `create` to `self.create_as_json`
|
48
|
+
|
49
|
+
**authenticable_resources(name, , only:)**
|
50
|
+
Acts as `resources` but also takes a block of routes. Those nested routes will all call the `authenticate!` helper method before running. The `authenticate!` helper will call a lookup helper `find_authenticatable_resource(access_token:)` with the bearer token found in the `HTTP_AUTHORIZATION` header, which should be used to look up the correct object or return nil if none is found (resulting in a 401 response).
|
40
51
|
|
41
52
|
## License
|
42
53
|
|
@@ -12,19 +12,21 @@ module AppRail
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
def create(email:, password:)
|
15
|
-
user = User.new("Email" => email, "Password Hash" => password_hash(password), "Access Token" =>
|
15
|
+
user = User.new("Email" => email, "Password Hash" => password_hash(password), "Access Token" => next_access_token)
|
16
16
|
user.create
|
17
17
|
user
|
18
18
|
end
|
19
19
|
|
20
20
|
def create_session_as_json(email:, password:)
|
21
21
|
user = find_by_email_and_password(email, password)
|
22
|
+
user["Access Token"] = next_access_token
|
23
|
+
user.save
|
22
24
|
user&.oauth_session
|
23
25
|
end
|
24
26
|
|
25
27
|
def find_by_email_and_password(email, password)
|
26
|
-
user = all(filter: "
|
27
|
-
user.valid_password? ? user : nil
|
28
|
+
user = all(filter: "{Email} = \"#{email}\"").first
|
29
|
+
user.valid_password?(password) ? user : nil
|
28
30
|
end
|
29
31
|
|
30
32
|
def find_by_access_token(access_token)
|
@@ -35,7 +37,7 @@ module AppRail
|
|
35
37
|
BCrypt::Password.create(password)
|
36
38
|
end
|
37
39
|
|
38
|
-
def
|
40
|
+
def next_access_token
|
39
41
|
SecureRandom.hex
|
40
42
|
end
|
41
43
|
end
|
@@ -6,7 +6,7 @@ module AppRail
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def find_current_user
|
9
|
-
authorization_header && bearer_token ?
|
9
|
+
authorization_header && bearer_token ? find_authenticatable_resource(access_token: bearer_token) : nil
|
10
10
|
end
|
11
11
|
|
12
12
|
def bearer_token
|
@@ -24,6 +24,7 @@ end
|
|
24
24
|
module AppRail
|
25
25
|
module Airtable
|
26
26
|
class Sinatra < Sinatra::Base
|
27
|
+
@@authenticated_route = false
|
27
28
|
|
28
29
|
helpers do
|
29
30
|
def request_body_as_json
|
@@ -35,36 +36,54 @@ module AppRail
|
|
35
36
|
request_body_as_json.merge(params)
|
36
37
|
end
|
37
38
|
end
|
39
|
+
|
40
|
+
def self.authenticatable_resources(name, only: [:index, :show, :create])
|
41
|
+
|
42
|
+
# If authentication is used then include the correct helpers
|
43
|
+
# Allowing the routes to use `authenticate!` and `current_user`
|
44
|
+
helpers AppRail::Airtable::AuthenticationHelpers
|
45
|
+
|
46
|
+
resources(name, only: only)
|
47
|
+
|
48
|
+
return unless block_given?
|
49
|
+
@@authenticated_route = true
|
50
|
+
yield
|
51
|
+
@@authenticated_route = false
|
52
|
+
end
|
38
53
|
|
39
|
-
def self.resources(name, only: [:index, :show, :create]
|
54
|
+
def self.resources(name, only: [:index, :show, :create])
|
40
55
|
only = [only] if only.is_a?(Symbol)
|
41
56
|
|
42
|
-
index_route(name,
|
43
|
-
show_route(name,
|
44
|
-
create_route(name,
|
57
|
+
index_route(name, authenticated_route?) if only.include?(:index)
|
58
|
+
show_route(name, authenticated_route?) if only.include?(:show)
|
59
|
+
create_route(name, authenticated_route?) if only.include?(:create)
|
45
60
|
end
|
46
61
|
|
47
|
-
def self.index_route(name,
|
62
|
+
def self.index_route(name, authenticated_route)
|
48
63
|
get "/#{name.to_s}" do
|
49
|
-
authenticate! if
|
50
|
-
name.classify_constantize.index(user:
|
64
|
+
authenticate! if authenticated_route
|
65
|
+
name.classify_constantize.index(user: authenticated_route ? current_user : nil).map(&:ar_list_item_as_json).to_json
|
51
66
|
end
|
52
67
|
end
|
53
68
|
|
54
|
-
def self.show_route(name,
|
69
|
+
def self.show_route(name, authenticated_route)
|
55
70
|
get "/#{name.to_s}/:id" do
|
56
|
-
authenticate! if
|
71
|
+
authenticate! if authenticated_route
|
57
72
|
name.classify_constantize.find(params['id']).ar_stack_as_json.to_json
|
58
73
|
end
|
59
74
|
end
|
60
75
|
|
61
|
-
def self.create_route(name,
|
76
|
+
def self.create_route(name, authenticated_route)
|
62
77
|
post "/#{name.to_s}" do
|
63
|
-
authenticate! if
|
64
|
-
as_json = name.classify_constantize.create_as_json(current_user:
|
78
|
+
authenticate! if authenticated_route
|
79
|
+
as_json = name.classify_constantize.create_as_json(current_user: authenticated_route ? current_user : nil, params: params_and_body_as_json)
|
65
80
|
[201, as_json.to_json]
|
66
81
|
end
|
67
82
|
end
|
83
|
+
|
84
|
+
def self.authenticated_route?
|
85
|
+
@@authenticated_route
|
86
|
+
end
|
68
87
|
end
|
69
88
|
end
|
70
89
|
end
|
@@ -2,7 +2,18 @@
|
|
2
2
|
*.swp
|
3
3
|
*.rbc
|
4
4
|
*.sass-cache
|
5
|
-
/
|
6
|
-
|
7
|
-
|
8
|
-
/
|
5
|
+
/.bundle/
|
6
|
+
/.yardoc
|
7
|
+
/_yardoc/
|
8
|
+
/coverage/
|
9
|
+
/doc/
|
10
|
+
/pkg/
|
11
|
+
/spec/reports/
|
12
|
+
/tmp/
|
13
|
+
|
14
|
+
# rspec failure tracking
|
15
|
+
.rspec_status
|
16
|
+
.byebug_history
|
17
|
+
|
18
|
+
# dotenv
|
19
|
+
.env
|
data/templates/project/Gemfile
CHANGED
@@ -7,9 +7,15 @@ ruby '2.7.4'
|
|
7
7
|
|
8
8
|
gem 'app_rail-airtable'
|
9
9
|
|
10
|
-
|
11
|
-
gem 'dotenv'
|
10
|
+
group :development do
|
11
|
+
gem 'dotenv'
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
gem "rspec"
|
15
|
-
gem "rack-test"
|
14
|
+
group :test do
|
15
|
+
gem "rspec"
|
16
|
+
gem "rack-test"
|
17
|
+
end
|
18
|
+
|
19
|
+
group :development, :test do
|
20
|
+
gem 'byebug'
|
21
|
+
end
|
data/templates/project/config.ru
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
$stdout.sync = true
|
2
2
|
|
3
|
-
|
3
|
+
ENV['RACK_ENV'] ||= 'development'
|
4
|
+
require 'dotenv/load' if ENV['RACK_ENV'] == 'development'
|
5
|
+
require 'bundler'
|
6
|
+
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
|
7
|
+
|
8
|
+
require 'dotenv/load' if ENV['RACK_ENV'] == 'development'
|
9
|
+
|
4
10
|
require './lib/server'
|
5
11
|
run Server
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_rail-airtable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Brooke-Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|