Sha256: 8ed43f89bb7f906ba3e7bde85aedeb24094957f9f62f7aab6af670680dffae35

Contents?: true

Size: 950 Bytes

Versions: 6

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

module Monday
  # Encapsulates the response that comes back from
  # the Monday.com API.
  #
  # Returns status code, parsed body and headers.
  class Response
    ERROR_OBJECT_KEYS = %w[errors error_code error_message].freeze
    private_constant :ERROR_OBJECT_KEYS

    attr_reader :status, :body, :headers

    def initialize(response)
      @response = response
      @status = response.code.to_i
      @body = parse_body
      @headers = parse_headers
    end

    # Helper to determine if the response was successful.
    # Monday.com API returns 200 status code when there are formatting errors.
    def success?
      (200..299).cover?(status) && !errors?
    end

    private

    attr_reader :response

    def errors?
      (parse_body.keys & ERROR_OBJECT_KEYS).any?
    end

    def parse_body
      JSON.parse(response.body)
    end

    def parse_headers
      response.each_header.to_h
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
monday_ruby-1.0.0 lib/monday/response.rb
monday_ruby-0.6.2 lib/monday/response.rb
monday_ruby-0.6.1 lib/monday/response.rb
monday_ruby-0.6.0 lib/monday/response.rb
monday_ruby-0.4.0 lib/monday/response.rb
monday_ruby-0.3.0 lib/monday/response.rb