app_rail-airtable 0.2.13 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +12 -1
- data/lib/app_rail/airtable/authentication_helpers.rb +1 -1
- data/lib/app_rail/airtable/sinatra.rb +29 -12
- data/lib/app_rail/airtable/version.rb +1 -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: 592d97e9fc6dbe4177847fdbfbc5c003620d179caee7887efb23f91d5f736ca7
|
4
|
+
data.tar.gz: 985e3ac9e23f4fe318c4944c2c3720a656faead5b5de3e3149fe58013aaa173c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80ae0eea7fbd168f1b469a5bfc9255940f0c62875d232427fd654d123e3b02aab822646cf6fa23313ca3fe7e653d0d73935927b8c6dff74dec8a01acf5edd431
|
7
|
+
data.tar.gz: 83e8736f2dc7dc42d276c45a83d8597c98d1f38faee8cf09e581f19256f22f64de81fa34a1acf9f290f9e4638a4bc00f14e25787d29bf2507d2bc3bbba44a13b
|
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
|
|
@@ -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,52 @@ 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
|
+
@@authenticated_route = true
|
48
|
+
yield
|
49
|
+
@@authenticated_route = false
|
50
|
+
end
|
38
51
|
|
39
|
-
def self.resources(name, only: [:index, :show, :create]
|
52
|
+
def self.resources(name, only: [:index, :show, :create])
|
40
53
|
only = [only] if only.is_a?(Symbol)
|
41
54
|
|
42
|
-
index_route(name,
|
43
|
-
show_route(name,
|
44
|
-
create_route(name,
|
55
|
+
index_route(name, authenticated_route?) if only.include?(:index)
|
56
|
+
show_route(name, authenticated_route?) if only.include?(:show)
|
57
|
+
create_route(name, authenticated_route?) if only.include?(:create)
|
45
58
|
end
|
46
59
|
|
47
|
-
def self.index_route(name,
|
60
|
+
def self.index_route(name, authenticated_route)
|
48
61
|
get "/#{name.to_s}" do
|
49
|
-
authenticate! if
|
50
|
-
name.classify_constantize.index(user:
|
62
|
+
authenticate! if authenticated_route
|
63
|
+
name.classify_constantize.index(user: authenticated_route ? current_user : nil).map(&:ar_list_item_as_json).to_json
|
51
64
|
end
|
52
65
|
end
|
53
66
|
|
54
|
-
def self.show_route(name,
|
67
|
+
def self.show_route(name, authenticated_route)
|
55
68
|
get "/#{name.to_s}/:id" do
|
56
|
-
authenticate! if
|
69
|
+
authenticate! if authenticated_route
|
57
70
|
name.classify_constantize.find(params['id']).ar_stack_as_json.to_json
|
58
71
|
end
|
59
72
|
end
|
60
73
|
|
61
|
-
def self.create_route(name,
|
74
|
+
def self.create_route(name, authenticated_route)
|
62
75
|
post "/#{name.to_s}" do
|
63
|
-
authenticate! if
|
64
|
-
as_json = name.classify_constantize.create_as_json(current_user:
|
76
|
+
authenticate! if authenticated_route
|
77
|
+
as_json = name.classify_constantize.create_as_json(current_user: authenticated_route ? current_user : nil, params: params_and_body_as_json)
|
65
78
|
[201, as_json.to_json]
|
66
79
|
end
|
67
80
|
end
|
81
|
+
|
82
|
+
def self.authenticated_route?
|
83
|
+
@@authenticated_route
|
84
|
+
end
|
68
85
|
end
|
69
86
|
end
|
70
87
|
end
|
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.0
|
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-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|