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 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 Method Details

#prepare_request(payload, headers) ⇒ Object



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

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

#process_response(response) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/async/rest/wrapper/json.rb', line 53

def process_response(response)
  # We expect all responses to be valid JSON.
  # I tried inspecting content-type, but too many servers do the wrong thing.
  if body = response.body
    response.body = Parser.new(body)
  end
  
  return response
end

#split(*args) ⇒ Object



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

def split(*args)
  @content_type.split
end