Module: Sinatra::JSONAPI

Defined in:
lib/sinatra/jsonapi.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sinatra/jsonapi.rb', line 8

def self.registered(app)
  app.disable :show_exceptions
  app.disable :raise_errors
  app.disable :protection
  app.enable :raise_errors if test?

  app.before do
    content_type :json
  end

  app.error do
    api_error 'unexpected_error', 'An unexpected error has occured, please try your request again later'
  end

  # Disables Sinatra's internal HTML error message for development mode.
  app.error Sinatra::NotFound do
    not_found
  end

  app.not_found do
    # If 404 body is not the default "Not Found" message, we can return it without processing.
    return response if response.body.first != "<h1>Not Found</h1>"

    api_error 'not_found', "The requested method was not found: #{request.path}"
  end

  app.helpers do
    def api_error(type, message)
      payload = {:error => {:type => type, :message => message}}
      halt process_payload(payload)
    end

    def api_response(payload)
      halt process_payload(payload)
    end

    def _jsonapi_to_json(input)
      JSON.unparse input
    end

    def process_payload(payload)
      if params[:callback]
        response_body = "#{params[:callback]}({\n"+
                        "  #{_jsonapi_to_json payload}\n"+
                        "})"
      else
        response_body = _jsonapi_to_json payload
      end
    end
  end
end