Class: Async::REST::Wrapper::URLEncoded

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

Defined Under Namespace

Classes: Parser

Constant Summary collapse

APPLICATION_FORM_URLENCODED =
"application/x-www-form-urlencoded".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type = APPLICATION_FORM_URLENCODED) ⇒ URLEncoded

Returns a new instance of URLEncoded.



34
35
36
# File 'lib/async/rest/wrapper/url_encoded.rb', line 34

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

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



38
39
40
# File 'lib/async/rest/wrapper/url_encoded.rb', line 38

def content_type
  @content_type
end

Instance Method Details

#prepare_request(payload, headers) ⇒ Object



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

def prepare_request(payload, headers)
  headers['accept'] ||= @content_type
  
  if payload
    headers['content-type'] = @content_type
    
    ::Protocol::HTTP::Body::Buffered.new([
      ::Protocol::HTTP::URL.encode(payload)
    ])
  end
end

#process_response(request, response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/async/rest/wrapper/url_encoded.rb', line 68

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

#split(*args) ⇒ Object



40
41
42
# File 'lib/async/rest/wrapper/url_encoded.rb', line 40

def split(*args)
  @content_type.split
end

#wrap_response(response) ⇒ Object



62
63
64
65
66
# File 'lib/async/rest/wrapper/url_encoded.rb', line 62

def wrap_response(response)
  if body = response.body
    response.body = Parser.new(body)
  end
end