Module: Overture::HttpApi::Helpers

Defined in:
lib/overture/http_api/helpers.rb

Instance Method Summary collapse

Instance Method Details

#found!(object) ⇒ Object



52
53
54
# File 'lib/overture/http_api/helpers.rb', line 52

def found!(object)
  object.nil? ? halt_not_found! : object
end

#halt_error!(status, title) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/overture/http_api/helpers.rb', line 37

def halt_error!(status, title)
  halt status, json(
    errors: [{
      status: status.to_s,
      title: title
    }]
  )
end

#halt_not_acceptable!Object



21
22
23
# File 'lib/overture/http_api/helpers.rb', line 21

def halt_not_acceptable!
  halt_error!(406, 'Not Acceptable')
end

#halt_not_found!Object



13
14
15
# File 'lib/overture/http_api/helpers.rb', line 13

def halt_not_found!
  halt_error!(404, 'Not Found')
end

#halt_server_error!(error = 'Server Error') ⇒ Object



33
34
35
# File 'lib/overture/http_api/helpers.rb', line 33

def halt_server_error!(error = 'Server Error')
  halt_error!(500, error)
end

#halt_unauthorized!Object



17
18
19
# File 'lib/overture/http_api/helpers.rb', line 17

def halt_unauthorized!
  halt_error!(401, 'Unauthorized')
end

#halt_unprocessable!(error) ⇒ Object



29
30
31
# File 'lib/overture/http_api/helpers.rb', line 29

def halt_unprocessable!(error)
  halt_error!(422, error)
end

#halt_unsupported_media_type!Object



25
26
27
# File 'lib/overture/http_api/helpers.rb', line 25

def halt_unsupported_media_type!
  halt_error!(415, 'Unsupported Media Type')
end

#json(data, serializer: nil, options: {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/overture/http_api/helpers.rb', line 6

def json(data, serializer: nil, options: {})
  data = data.all if data.respond_to?(:all)
  return JSON.generate(data, options) unless serializer

  serializer.new(data, options).serialized_json
end

#require_params!(*names) ⇒ Object



46
47
48
49
50
# File 'lib/overture/http_api/helpers.rb', line 46

def require_params!(*names)
  names.each do |name|
    halt_unprocessable!("#{name} is required") unless params.key?(name)
  end
end