Sha256: 6eafd4a1f08b4485154d7bbfec9d76c65329dcbd693d52504ecc4759d47204bb

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Bullion
  # Parent class for API services
  class Service < Sinatra::Base
    register Sinatra::ActiveRecordExtension
    helpers Sinatra::CustomLogger

    configure do
      set :protection, except: :http_origin
      set :logging, true
      set :logger, Bullion::LOGGER
      set :database, Bullion.config.db_url
      set :show_exceptions, false
    end

    before do
      # Sets up a useful variable (@json_body) for accessing a parsed request body
      if request.content_type&.include?("json") && !request.body.read.empty?
        request.body.rewind
        @json_body = JSON.parse(request.body.read, symbolize_names: true)
      end
    rescue StandardError => e
      halt(400, { error: "Request must be JSON: #{e.message}}" }.to_json)
    end

    error do
      content_type :json

      e = env["sinatra.error"]
      resp = { result: "error", message: e.message }
      resp[:trace] = e.full_message if settings.environment.to_s != "production"
      resp.to_json
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bullion-0.7.3 lib/bullion/service.rb
bullion-0.7.2 lib/bullion/service.rb
bullion-0.7.1 lib/bullion/service.rb
bullion-0.7.0 lib/bullion/service.rb
bullion-0.6.2 lib/bullion/service.rb
bullion-0.6.1 lib/bullion/service.rb
bullion-0.6.0 lib/bullion/service.rb
bullion-0.5.1 lib/bullion/service.rb
bullion-0.5.0 lib/bullion/service.rb
bullion-0.4.3 lib/bullion/service.rb
bullion-0.4.2 lib/bullion/service.rb