Class: Redmine::AcceptJson

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/redmine/accept_json.rb

Overview

A decorator object for RestClient that provides immediate JSON-parsing capabilities. Rather than dealing with raw response objects, this decorator helps us get at parsed JSON data immediately.

This decorator works by intercepting outgoing requests and adding an Accept header to indicate we would like to receive JSON data back. Responses will then be parsed, given they actually are JSON, and both the parsed response body and the original response are returned.

Constant Summary collapse

ACCEPT =

Value of the outgoing Accept header.

'application/json'.freeze
CONTENT_TYPE =

Matcher for recognizing response content types.

%r{application/json}

Instance Method Summary collapse

Instance Method Details

#get(path, headers = {}) ⇒ Object

Wrap requests to add an ‘Accept` header to ask for JSON, and parse response bodies as JSON data.



22
23
24
25
26
27
28
# File 'lib/redmine/accept_json.rb', line 22

def get(path, headers = {})
  response = super(path, { 'Accept' => ACCEPT }.merge(headers.to_h))
  case response.content_type
  when CONTENT_TYPE then [parse_response(response), response]
  else raise "Unknown content type #{response.content_type.inspect}"
  end
end