Class: Async::REST::Wrapper::Form

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

Constant Summary collapse

DEFAULT_CONTENT_TYPES =
{
  JSON::APPLICATION_JSON => JSON::Parser,
  URLEncoded::APPLICATION_FORM_URLENCODED => URLEncoded::Parser,
}

Instance Method Summary collapse

Methods inherited from Generic

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

Constructor Details

#initialize(content_types = DEFAULT_CONTENT_TYPES) ⇒ Form

Returns a new instance of Form.



19
20
21
# File 'lib/async/rest/wrapper/form.rb', line 19

def initialize(content_types = DEFAULT_CONTENT_TYPES)
  @content_types = content_types
end

Instance Method Details

#parser_for(response) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/async/rest/wrapper/form.rb', line 37

def parser_for(response)
  media_type, _ = response.headers["content-type"].split(";")
  if media_type && parser = @content_types[media_type]
    return parser
  end
  
  return super
end

#prepare_request(request, payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/async/rest/wrapper/form.rb', line 23

def prepare_request(request, payload)
  @content_types.each_key do |key|
    request.headers.add("accept", key)
  end
  
  if payload
    request.headers["content-type"] = URLEncoded::APPLICATION_FORM_URLENCODED
    
    request.body = ::Protocol::HTTP::Body::Buffered.new([
      ::Protocol::URL::Encoding.encode(payload)
    ])
  end
end