Class: Async::REST::Wrapper::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/async/rest/wrapper/json.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

APPLICATION_JSON =
"application/json".freeze
APPLICATION_JSON_STREAM =
"application/json; boundary=NL".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type = APPLICATION_JSON) ⇒ JSON

Returns a new instance of JSON.



33
34
35
# File 'lib/async/rest/wrapper/json.rb', line 33

def initialize(content_type = APPLICATION_JSON)
	@content_type = content_type
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



37
38
39
# File 'lib/async/rest/wrapper/json.rb', line 37

def content_type
  @content_type
end

Instance Method Details

#prepare_request(payload, headers) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/async/rest/wrapper/json.rb', line 43

def prepare_request(payload, headers)
	headers['accept'] ||= @content_type
	
	if payload
		headers['content-type'] = @content_type
		
		# TODO dump incrementally to IO?
		HTTP::Body::Buffered.new([
			::JSON.dump(payload)
		])
	end
end

#process_response(request, response) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/async/rest/wrapper/json.rb', line 56

def process_response(request, response)
	if content_type = response.headers['content-type']
		if content_type.start_with? @content_type
			if body = response.body
				response.body = Parser.new(body)
			end
		else
			warn "Unknown content type: #{content_type}!"
		end
	end
	
	return response
end

#split(*args) ⇒ Object



39
40
41
# File 'lib/async/rest/wrapper/json.rb', line 39

def split(*args)
	@content_type.split
end