class Redmine::AcceptJson

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.

Constants

ACCEPT

Value of the outgoing Accept header.

CONTENT_TYPE

Matcher for recognizing response content types.

Public Instance Methods

get(path, headers = {}) click to toggle source

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

Calls superclass method
# 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

Private Instance Methods

parse_response(response) click to toggle source
# File lib/redmine/accept_json.rb, line 32
def parse_response(response)
  JSON.parse(response.body)
rescue JSON::ParserError => e
  # TODO: log output here
  p response
  p response.to_hash
  raise e
end