Sha256: aabe7a0b0ccb3cd5416ac4a100eb4b308ac8c054f19bbffee1d3659b74686ac3

Contents?: true

Size: 717 Bytes

Versions: 1

Compression:

Stored size: 717 Bytes

Contents

# frozen_string_literal: true

require 'json'

module Kentaa
  module Api
    class Response
      attr_reader :response, :body

      def initialize(response)
        @response = response
        @body = parse_body(response.body)
      end

      def success?
        (http_code == 200 || http_code == 201) && !message
      end

      def error?
        !success?
      end

      def http_code
        response.code.to_i
      end

      def message
        body[:message]
      end

      private

      def parse_body(body)
        JSON.parse(body, symbolize_names: true)
      rescue JSON::ParserError => e
        {
          message: "Unable to parse JSON: #{e.message}"
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kentaa-api-0.3.0 lib/kentaa/api/response.rb