class AppRail::Airtable::Sinatra

Public Class Methods

authenticatable_resources(name, only: [:index, :show, :create, :update]) { || ... } click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 40
def self.authenticatable_resources(name, only: [: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
  
  resources(name, only: only)
  
  return unless block_given?
  @@authenticated_route = true
  yield
  @@authenticated_route = false
end
authenticated_route?() click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 94
def self.authenticated_route?
  @@authenticated_route
end
create_route(name, authenticated_route) click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 77
def self.create_route(name, authenticated_route)
  post "/#{name.to_s}" do
    authenticate! if authenticated_route
    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] 
  end
end
index_route(name, authenticated_route) click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 63
def self.index_route(name, authenticated_route)
  get "/#{name.to_s}" 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: [:index, :show, :create, :update]) click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 54
def self.resources(name, only: [: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) click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 70
def self.show_route(name, authenticated_route)
  get "/#{name.to_s}/:id" do
    authenticate! if authenticated_route
    name.classify_constantize.find(params['id']).ar_stack_as_json.to_json
  end
end
update_route(name, authenticated_route) click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 85
def self.update_route(name, authenticated_route)
  put "/#{name.to_s}/: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

Public Instance Methods

params_and_body_as_json() click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 35
def params_and_body_as_json
  request_body_as_json.merge(params)
end
request_body_as_json() click to toggle source
# File lib/app_rail/airtable/sinatra.rb, line 30
def request_body_as_json
  request.body.rewind
  JSON.parse(request.body.read)
end