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

Inherits:
Generic
  • 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

Methods inherited from Generic

#call, #process_response, #response_for, #retry_after_duration, #wrap_response

Constructor Details

#initialize(content_type = APPLICATION_JSON) ⇒ JSON

Returns a new instance of JSON.



20
21
22
# File 'lib/async/rest/wrapper/json.rb', line 20

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

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



24
25
26
# File 'lib/async/rest/wrapper/json.rb', line 24

def content_type
  @content_type
end

Instance Method Details

#parser_for(response) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/async/rest/wrapper/json.rb', line 48

def parser_for(response)
	if content_type = response.headers["content-type"]
		if content_type.start_with? @content_type
			return Parser
		end
	end
	
	return super
end

#prepare_request(request, payload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/async/rest/wrapper/json.rb', line 30

def prepare_request(request, payload)
	request.headers["accept"] ||= @content_type
	
	if payload
		request.headers["content-type"] = @content_type
		
		request.body = ::Protocol::HTTP::Body::Buffered.new([
			::JSON.dump(payload)
		])
	end
end

#split(*arguments) ⇒ Object



26
27
28
# File 'lib/async/rest/wrapper/json.rb', line 26

def split(*arguments)
	@content_type.split
end