Class: PR::Pin::Adapter::Handlers::JSONResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/pr/pin/adapter/handlers/json_response.rb

Constant Summary collapse

SUCCESS_CODES =
%w(200 201).freeze

Instance Method Summary collapse

Instance Method Details

#call(response, dataset) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pr/pin/adapter/handlers/json_response.rb', line 8

def call(response, dataset)
  result = JSON.parse(
    response.body,
    symbolize_names: true
  )

  pagination_result = result.fetch(:pagination, false)

  if SUCCESS_CODES.include?(response.code)
    if %i(post put patch).include?(dataset.request_method)
      response = result[:response]
    else
      response = Array([result[:response]]).flatten(1)
    end

    if pagination_result
      PaginatedResponse.new(response, pagination_result)
    else
      Response.new(response)
    end
  else
    raise(ResponseError.new(response, result))
  end
end