app_rail-airtable 0.2.5 → 0.2.9

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
  SHA256:
3
- metadata.gz: bfc0c7a8abdd811b4b70a4ef1bf1d5a42a9212712cfdf35fada50d1db81c670b
4
- data.tar.gz: 133f6b44dc502297d1bdfeb085f896a62eb155d0483ce8cf60c493ee2db84c7d
3
+ metadata.gz: 3e09f5f21d40b2186a9b4d985d85305c0f4e54e4066d74ee45a5c43a32a303ad
4
+ data.tar.gz: 0ab6b313e24780b1f83e2359638e783ff3d7ca7b6958b14c7c80ba479262e07c
5
5
  SHA512:
6
- metadata.gz: 482b3617a8e4ee7d2628e1efb7d6a4674ecf258c77dbd1594aaa1246dcfa0d06d56846f0606e75de39c76902b3101c2e10646e6587ccf98ef06965f5eca8eca8
7
- data.tar.gz: 2d3fdcd70ed7f14166fa8c301a5745b5aa8aeaaef6dd0202da440570dfe8d537ffad48eb6debd1d4fa05092a560bd8bdd396aad7c523a9aefbda23b8abb1c769
6
+ metadata.gz: 5cdd4ff73e660d69000ac91e1204047f7637a7d58e351e1253639a60423828ccf3c8417083aeaadf1d4d3660584302cbfab9a39f5f737a5b501257fdf6f0359e
7
+ data.tar.gz: b9e534d2132bf753fd8cdf8d1126cdf629921177bbaf164e6ca21cabd64b9b207830dcbc9087a09f480c03d0c5ebb8e0101a8ae1d6625da851dc8bc8e589eee3
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .byebug_history
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_rail-airtable (0.2.5)
4
+ app_rail-airtable (0.2.9)
5
5
  activesupport
6
6
  airrecord
7
+ bcrypt
7
8
  sinatra
8
9
  thor
9
10
 
@@ -19,6 +20,7 @@ GEM
19
20
  airrecord (1.0.7)
20
21
  faraday (>= 0.10, < 2.0)
21
22
  net-http-persistent
23
+ bcrypt (3.1.16)
22
24
  byebug (11.1.3)
23
25
  concurrent-ruby (1.1.9)
24
26
  connection_pool (2.2.5)
data/README.md CHANGED
@@ -31,7 +31,9 @@ You should create a model class for each table you want to use in your App. Mode
31
31
  To provide support for routes, models can implement
32
32
  * ar_list_item (index)
33
33
  * ar_stack (show)
34
- * self.create_from_params (create)
34
+ * self.create_as_json (create)
35
+
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.
35
37
 
36
38
  ### Servers
37
39
  You should create a single server which extends `AppRail::Airtable::Sinatra`, then add routes with the `resources` macro to add `index`, `show` and `create` routes. You can also provide your own routes.
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'sinatra'
25
25
  spec.add_dependency 'airrecord'
26
26
  spec.add_dependency 'thor'
27
+ spec.add_dependency 'bcrypt'
27
28
 
28
29
  spec.add_development_dependency 'rspec'
29
30
  spec.add_development_dependency 'rack-test'
data/bin/ara_generator CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "bundler/setup"
3
- require 'app_rail/airtable'
3
+ require 'app_rail/airtable/generator'
4
4
  require 'byebug'
5
5
 
6
6
  AppRail::Airtable::Generator.start(ARGV)
@@ -43,7 +43,7 @@ module AppRail
43
43
  # Customisable behaviour
44
44
 
45
45
  # Override to provide custom sorting or filtering for index
46
- def self.index
46
+ def self.index(user:)
47
47
  all
48
48
  end
49
49
 
@@ -0,0 +1,27 @@
1
+ require 'bcrypt'
2
+
3
+ module AppRail
4
+ module Airtable
5
+ class AuthenticationRecord < ApplicationRecord
6
+ include BCrypt
7
+ airtable_attr "Email", "Password Hash", "Access Token"
8
+
9
+ def self.create_session_as_json(email:, password:)
10
+ user = find_by_email_and_password(email, Password.create(password))
11
+ user&.oauth_session
12
+ end
13
+
14
+ def self.find_by_email_and_password(email, password_hash)
15
+ all(filter: "AND({Email} = \"#{email}\",{Password Hash} = \"#{password_hash}\")").first
16
+ end
17
+
18
+ def self.find_by_access_token(access_token)
19
+ all(filter: "{Access Token} = \"#{access_token}\"").first
20
+ end
21
+
22
+ def oauth_session
23
+ { access_token: self["Access Token"], scope: :user, refresh_token: "", token_type: :bearer, expires_in: 60000 }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,7 @@
1
1
  require 'active_support/core_ext/string/inflections'
2
+ require 'app_rail/airtable/string_ext'
2
3
  require 'thor'
3
4
  require 'airrecord'
4
- require 'app_rail/airtable/string_ext'
5
5
 
6
6
  module AppRail
7
7
  module Airtable
@@ -2,6 +2,7 @@ require 'sinatra'
2
2
  require 'json'
3
3
  require 'active_support'
4
4
 
5
+ # TODO: MBS - move to configure block or other
5
6
  Airrecord.api_key = ENV.fetch("AIRTABLE_API_KEY")
6
7
 
7
8
  class Symbol
@@ -23,24 +24,47 @@ end
23
24
  module AppRail
24
25
  module Airtable
25
26
  class Sinatra < Sinatra::Base
27
+
28
+ helpers do
29
+ def request_body_as_json
30
+ request.body.rewind
31
+ JSON.parse(request.body.read)
32
+ end
33
+
34
+ def params_and_json_body
35
+ request_body_as_json.merge(params)
36
+ end
37
+ end
26
38
 
27
- def self.resources(name)
39
+ def self.resources(name, only: [:index, :show, :create], authenticated: false)
40
+ only = [only] if only.is_a?(Symbol)
41
+
42
+ index_route(name, authenticated) if only.include?(:index)
43
+ show_route(name, authenticated) if only.include?(:show)
44
+ create_route(name, authenticated) if only.include?(:create)
45
+ end
46
+
47
+ def self.index_route(name, authenticated)
28
48
  get "/#{name.to_s}" do
29
- name.classify_constantize.index.map(&:ar_list_item_as_json).to_json
49
+ authenticate! if authenticated
50
+ name.classify_constantize.index(user: authenticated ? current_user : nil).map(&:ar_list_item_as_json).to_json
30
51
  end
31
-
52
+ end
53
+
54
+ def self.show_route(name, authenticated)
32
55
  get "/#{name.to_s}/:id" do
56
+ authenticate! if authenticated
33
57
  name.classify_constantize.find(params['id']).ar_stack_as_json.to_json
34
58
  end
35
-
59
+ end
60
+
61
+ def self.create_route(name, authenticated)
36
62
  post "/#{name.to_s}" do
37
- request.body.rewind # in case someone already read it
38
- data = JSON.parse(request.body.read)
39
- new_record = name.classify_constantize.create_from_params(data.merge(params))
40
- [201, {id: new_record.id}.to_json]
63
+ authenticate! if authenticated
64
+ as_json = name.classify_constantize.create_as_json(current_user: authenticated ? current_user : nil, params: params_and_json_body)
65
+ [201, as_json.to_json]
41
66
  end
42
67
  end
43
-
44
68
  end
45
69
  end
46
70
  end
@@ -1,5 +1,5 @@
1
1
  module AppRail
2
2
  module Airtable
3
- VERSION = "0.2.5"
3
+ VERSION = "0.2.9"
4
4
  end
5
5
  end
@@ -1,8 +1,9 @@
1
1
  require "app_rail/airtable/version"
2
+ require "app_rail/airtable/string_ext"
2
3
  require "app_rail/airtable/application_record"
4
+ require "app_rail/airtable/authentication_record"
3
5
  require "app_rail/airtable/sinatra"
4
6
  require "app_rail/airtable/generator"
5
- require "app_rail/airtable/string_ext"
6
7
 
7
8
  module AppRail
8
9
  module Airtable
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.2.5
4
+ version: 0.2.9
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-09-20 00:00:00.000000000 Z
11
+ date: 2021-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bcrypt
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -102,7 +116,6 @@ extensions: []
102
116
  extra_rdoc_files: []
103
117
  files:
104
118
  - ".DS_Store"
105
- - ".byebug_history"
106
119
  - ".gitignore"
107
120
  - ".rspec"
108
121
  - ".travis.yml"
@@ -117,6 +130,7 @@ files:
117
130
  - bin/setup
118
131
  - lib/app_rail/airtable.rb
119
132
  - lib/app_rail/airtable/application_record.rb
133
+ - lib/app_rail/airtable/authentication_record.rb
120
134
  - lib/app_rail/airtable/generator.rb
121
135
  - lib/app_rail/airtable/sinatra.rb
122
136
  - lib/app_rail/airtable/string_ext.rb
data/.byebug_history DELETED
@@ -1,14 +0,0 @@
1
- exit
2
- @table_definitions.first.all.first.fields.keys
3
- @table_definitions.first.all.first.fields
4
- @table_definitions.first.all.first
5
- @table_definitions.first.first
6
- @table_definitions.first.instance_variable_get("@fields")
7
- @table_definitions.first.instance_variable_get("fields")
8
- @table_definitions.first.instance_variable_get("@fields")
9
- @table_definitions.first.fields
10
- @table_definitions.first.table_name
11
- @table_definitions.first.name
12
- @table_definitions.first.all
13
- @table_definitions.first
14
- @table_definitions