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)


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

def self.authenticated_route?
  @@authenticated_route
end

.create_route(name, authenticated_route) ⇒ Object



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

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



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

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



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

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
# 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

    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



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

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



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

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