Class: SnapImage::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/snapimage/rack/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
# File 'lib/snapimage/rack/response.rb', line 5

def initialize(options = {})
  @content_type = options[:content_type] || "text/json"
  @template = options[:template] || "{{json}}"
  @json = {}
  super()
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



3
4
5
# File 'lib/snapimage/rack/response.rb', line 3

def content_type
  @content_type
end

#jsonObject

Returns the value of attribute json.



3
4
5
# File 'lib/snapimage/rack/response.rb', line 3

def json
  @json
end

#templateObject

Returns the value of attribute template.



3
4
5
# File 'lib/snapimage/rack/response.rb', line 3

def template
  @template
end

Instance Method Details

#finishObject



37
38
39
40
41
# File 'lib/snapimage/rack/response.rb', line 37

def finish
  write(@template.gsub(/{{json}}/, @json.to_json))
  self["Content-Type"] = @content_type
  super
end

#set_bad_requestObject



17
18
19
# File 'lib/snapimage/rack/response.rb', line 17

def set_bad_request
  @json = { status_code: 400, message: "Bad Request" }
end

#set_file_too_largeObject



29
30
31
# File 'lib/snapimage/rack/response.rb', line 29

def set_file_too_large
  @json = { status_code: 405, message: "File Too Large" }
end

#set_internal_server_errorObject



33
34
35
# File 'lib/snapimage/rack/response.rb', line 33

def set_internal_server_error
  @json = { status_code: 500, message: "Internal Server Error" }
end

#set_invalid_directoryObject



25
26
27
# File 'lib/snapimage/rack/response.rb', line 25

def set_invalid_directory
  @json = { status_code: 404, message: "Invalid Directory" }
end

#set_invalid_filenameObject



21
22
23
# File 'lib/snapimage/rack/response.rb', line 21

def set_invalid_filename
  @json = { status_code: 403, message: "Invalid Filename" }
end

#set_success(info = {}) ⇒ Object



12
13
14
15
# File 'lib/snapimage/rack/response.rb', line 12

def set_success(info = {})
  info[:message] ||= "Success"
  @json = { status_code: 200 }.merge(info)
end