Class: AppRail::Airtable::Sinatra

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/app_rail/airtable/sinatra.rb

Constant Summary collapse

@@authenticated_route =
false

Class Method Summary collapse

Class Method Details

.authenticatable_resources(name, only: %i[index show create update])) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/app_rail/airtable/sinatra.rb', line 44

def self.authenticatable_resources(name, only: %i[index show create update])
  # If authentication is used then include the correct helpers
  # Allowing the routes to use `authenticate!` and `current_user`
  helpers AppRail::Airtable::AuthenticationHelpers

  (name)
  sign_out_route(name)
  resources(name, only: only)

  return unless block_given?

  @@authenticated_route = true
  yield
  @@authenticated_route = false
end

.authenticated_route?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/app_rail/airtable/sinatra.rb', line 129

def self.authenticated_route?
  @@authenticated_route
end

.create_route(name, authenticated_route) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/app_rail/airtable/sinatra.rb', line 106

def self.create_route(name, authenticated_route)
  post "/#{name}" do
    authenticate! if authenticated_route
    begin
      as_json = name.classify_constantize.create_as_json(current_user: authenticated_route ? current_user : nil,
                                                         params: params_and_body_as_json)
      [201, as_json.to_json]
    rescue AppRail::Airtable::Authenticatable::AlreadyExistsError
      [422, { error: "#{name.singularize} already exists" }.to_json]
    end
  end
end

.index_route(name, authenticated_route) ⇒ Object



92
93
94
95
96
97
# File 'lib/app_rail/airtable/sinatra.rb', line 92

def self.index_route(name, authenticated_route)
  get "/#{name}" do
    authenticate! if authenticated_route
    name.classify_constantize.index(user: authenticated_route ? current_user : nil).map(&:ar_list_item_as_json).to_json
  end
end

.resources(name, only: %i[index show create update])) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/app_rail/airtable/sinatra.rb', line 60

def self.resources(name, only: %i[index show create update])
  only = [only] if only.is_a?(Symbol)

  index_route(name, authenticated_route?) if only.include?(:index)
  show_route(name, authenticated_route?) if only.include?(:show)
  create_route(name, authenticated_route?) if only.include?(:create)
  update_route(name, authenticated_route?) if only.include?(:update)
end

.show_route(name, authenticated_route) ⇒ Object



99
100
101
102
103
104
# File 'lib/app_rail/airtable/sinatra.rb', line 99

def self.show_route(name, authenticated_route)
  get "/#{name}/:id" do
    authenticate! if authenticated_route
    name.classify_constantize.find(params['id']).ar_stack_as_json.to_json
  end
end

.sign_in_route(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/app_rail/airtable/sinatra.rb', line 69

def self.(name)
  post "/#{name}/session" do
    halt [401, { error: 'Invalid client_id' }.to_json] unless params['client_id'] == oauth_client_id
    halt [401, { error: 'Invalid client_secret' }.to_json] unless params['client_secret'] == oauth_client_secret

    resource = name.classify_constantize.authenticate_by_params(params)
    halt [401, { error: 'Invalid credentials' }.to_json] unless resource

    resource.oauth_session.to_json
  end
end

.sign_out_route(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/app_rail/airtable/sinatra.rb', line 81

def self.sign_out_route(name)
  delete "/#{name}/session" do
    authenticate!
    current_user.access_token = nil
    current_user.save

    # Assume that the client will reload and trigger a 401
    [].to_json
  end
end

.update_route(name, authenticated_route) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/app_rail/airtable/sinatra.rb', line 119

def self.update_route(name, authenticated_route)
  put "/#{name}/:id" do
    authenticate! if authenticated_route
    record = name.classify_constantize.find(params['id'])
    as_json = record.update_as_json(current_user: authenticated_route ? current_user : nil,
                                    params: params_and_body_as_json)
    [200, as_json.to_json]
  end
end