Module: Itch::Request

Included in:
Bundles, Purchases, Reviews, Rewards
Defined in:
lib/itch/request.rb

Overview

Mix in for request validation and response parsing

Instance Method Summary collapse

Instance Method Details

#validate_response(page, action: "making request", content_type: "text/html") ⇒ Object

rubocop:disable all

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/itch/request.rb', line 7

def validate_response(page, action: "making request", content_type: "text/html")
  response_type = page.response["content-type"]
  return if page.code == "200" && response_type == content_type

  unless response_type == "application/json"
    raise Error, "Unexpected error occurred while #{action}: Response code #{page.code}"
  end

  error_data = nil
  begin
    data = JSON.parse(page.content)
    if data["errors"]
      error_data = data["errors"].respond_to?(:join) ? "\n#{data["errors"].join("\n")}" : data["errors"]
    end
  rescue StandardError
    raise Error, "Error parsing response while #{action}"
  end

  raise Error, "Unexpected error occurred while #{action}: #{error_data}"
end